There is a table
item_id, division_id 1 1 1 2 2 1 3 1 4 1 How to choose item_id, which go to division_id = 1 and division_id = 2? THOSE. Should there be a unique item_id that only applies to 2 or more sections?
There is a table
item_id, division_id 1 1 1 2 2 1 3 1 4 1 How to choose item_id, which go to division_id = 1 and division_id = 2? THOSE. Should there be a unique item_id that only applies to 2 or more sections?
Using DISTINCT, you can select unique values:
SELECT DISTINCT(`item_id`) FROM `table` WHERE `division_id` IN(1,2) item_id by item_id , count in how many division_id each enters, take only those that are 2 or more.
SELECT item_id FROM `table` GROUP BY item_id HAVING COUNT(division_id) > 1 For a sample by specific division_id you can add
WHERE division_id IN (1, 2) Brute force method:
SELECT item_id FROM `table` t WHERE EXISTS(SELECT * FROM `table` a where a.item_id=t.item_id AND a.division_id IN (1,2,3,4,5)) AND EXISTS(SELECT * FROM `table` a where a.item_id=t.item_id AND a.division_id = 6) Source: https://ru.stackoverflow.com/questions/637295/
All Articles