Skip to content
Advertisement

SQL ManyToOne condition

But if I add one more for my query (that not related to this), for example:

I should get nothing

(but it needs to be workable :D)

Advertisement

Answer

You can do a GROUP BY, and use HAVING to make sure all desired tag_id’s are there:

SELECT sp.perfume_id
FROM sp_tag_to_perfume sp
WHERE sp.tag_id IN (2070, 127)
GROUP BY sp.perfume_id
HAVING COUNT(DISTINCT sp.tag_id) = 2; -- number of tag_id values (in this case 2070 and 127)
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement