SQL: many-to-many relationship, IN condition

Posted by Maarten on Stack Overflow See other posts from Stack Overflow or by Maarten
Published on 2010-06-03T10:26:17Z Indexed on 2010/06/03 10:34 UTC
Read the original article Hit count: 336

Filed under:
|
|
|
|

I have a table called transactions with a many-to-many relationship to items through the items_transactions table.

I want to do something like this:

SELECT "transactions".* 
FROM "transactions" 
INNER JOIN "items_transactions" ON "items_transactions".transaction_id = "transactions".id 
INNER JOIN "items" ON "items".id = "items_transactions".item_id 
WHERE (items.id IN (<list of items>))

But this gives me all transactions that have one or more of the items in the list associated with it and I only want it to give me the transactions that are associated with all of those items.

Any help would be appreciated.

© Stack Overflow or respective owner

Related posts about sql

Related posts about select