Skip to content
Advertisement

SQL get ids with minim 3 common values

Can someone help me to get all ids with the same color from this table? Thank you

enter image description here

Should return 1, 3, 9

Advertisement

Answer

You can try next query:

SELECT 
    p1.product_id p1
FROM `product_colors` p1
JOIN `product_colors` p2 ON p1.product_id <> p2.product_id AND  p1.color_id = p2.color_id
GROUP BY p1.product_id, p2.product_id 
HAVING COUNT(distinct p1.color_id) > 2
order by p1.product_id
;

Check it on SQLize.online

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement