I have two tables:
- orders (id);
- order_items(id, order_id, item_id, quantity)
Let’s say I have already selected an order X with 3 different items (A – 1 unit, B – 2 units and C – 1 unit).
I need to get all the orders that have same items and exactly the same quantity of items (in this case: A – 1 unit, B – 2 units and C – 1 unit and no more or no less :)), excluding an order X.
i made a dump of test tables, so, I hope, for You would be easier to understand, what I’d like to get:) http://nopaste.info/44eb93ae3d.html
lets assume order X = 1, so desired output would be only the order 2, because order 3 has only one item and order 4 has same items as #1, but it has also one item extra, so it is not good 🙂
Count the same occurence of each orders in the order_items table, and then compare to the number of item of the searched order:
Note that this query assume that
(order_id, item_id)is a Key of theorder_itemstable.I have made a sample test here.