Check to see if there are any matching ids before trying to use in SQL

This fixes a MySQL error