Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using "AND" with "IN" - is this possible?

Status
Not open for further replies.

miahmiah900

Programmer
Sep 19, 2003
38
US
This is, I think, a strange question. I have solved the problem using a different method, but I would like to know if anyone has any input on the following:

I am performing a SELECT similar to the following:

SELECT inventory_id FROM tbl_option_key WHERE data_id IN (3,7)

What this gives me is an inventory_id for each row that contains EITHER 3 or 7 in the data_id column. What I would like to do, is to have a query that returns the inventory_id only if it matches ALL entries in the "IN" list.

This may seem illogical, but the reason I want to do this is because there needs to be 0 or many entries in tbl_option_key for each inventory_id.

The method I used to solve this method is a GROUP BY on the inventory_id field and counting the number of matches to see if the total is the same as the number of entries in the "IN" list.

Any suggestions?

Thanks!

-miah
 
Code:
select inventory_id 
FROM tbl_option_key 
WHERE data_id IN (3,7)
group by inventory_id
having count(distinct data_id) = 2
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top