I have a system which stores peoples outfits. Each outfit contains 1 to many items.
Thus my tables are like this
outfits
+-----------+--------+
| outfit_id | cus_id |
+-----------+--------+
| 1 | 5 |
| 2 | 92 |
+-----------+--------+
items
+---------+-------+-------+
| item_id | name | sku |
+---------+-------+-------+
| 1 | hat | 1111 |
| 2 | pants | 2222 |
| 3 | shirt | 3333 |
| 4 | shoes | 4444 |
+---------+-------+-------+
items_in_outfit
+--------+-----------+---------+
| ino_id | outfit_id | item_id |
+--------+-----------+---------+
| 1 | 1 | 3 |
| 2 | 1 | 2 |
| 3 | 1 | 4 |
| 4 | 2 | 1 |
| 4 | 2 | 3 |
+--------+-----------+---------+
I am given this information:
cus_id=92
wants to create another outfit with thesku
's1111
and3333
However, this is a duplicate of outfit_id=2
. So I want mysql to return me the outfit_id
of 2. If its not a duplicate, return nothing. It only counts as a duplicate if the same cus_id
is trying to make an outfit which has the exact same sku
's already.
If cus_id=5
wants to create another outfit with the sku
's 1111
and 3333
. It should return nothing (as its not a duplicate) because cus_id=5
doesn't have an outfit with just 1111
and 3333
SELECT o.outfit_id FROM
outfits o JOIN
items_in_outfit iio
ON o.outfit_id = iio.outfit_id JOIN
items i
ON iio.item_id = i.item_id
WHERE cus_id = 92 AND ...