Identifying Missing Matches in Mysql DB

Is there a way to identify the missing combination in the join table. for example: Three tabs

animals

food

animals_food

can determine if a particular food is related to the animal, or vice versa?

0


a source to share


1 answer


You can use left join:

SELECT animals.id FROM animals LEFT JOIN animals_food ON animals.id = animals_food.animals_id WHERE animals_food.food_id IS NULL;

      



A left join contains all rows from the left table (animals), even if the join condition does not find any matching rows on the right table (animals_food). When no match is found, the columns in the right table are replaced with NULL.

The WHERE clause in my query deletes all rows that have matches, leaving only the orphan axes from the left table in the result.

+3


a source







All Articles