tags:

views:

29

answers:

1

Hi All,

Suppose I have a table like:

id............value
```````````````````
A............1
A............2
A............3
B............1

and so on.

so, If I want to select rows with Value either 1 OR 2 OR 3, I do this:

select id From table where value in (1,2,3)

But, what I want to do is, select id with AND instead of OR. I want it like

so If I want to select rows where Id has value 1, 2 AND 3, (in this case, resultant = A), how do I select that?

Thanks!
(I tried to do it myself, no luck, I tried to search on google, I realized I dint even know how to phrase the question! so if the question here doesnt agree with my actual doubt, feel free to change it)

+6  A: 
select id 
from MyTable
where value in (1, 2, 3) 
group by id
having count(distinct value) = 3
RedFilter
the value will be passed from codebehind. its not a constant, but I Get the idea... i guess i will split the CSV values and count(*) it and place that variable instead of 3, would that work?
iamserious
@iamserious: that will work, as long as the number for 3 matches the number of distinct values - make sure your value passed from codebehind has no duplicates in it.
RedFilter
I simplified the case here, but my querry was way more complicated.. and finally after trial-error method, i got it to work, but, all thanks to you, your querry works flawlessly, without any change! cheers! thanks a ton! you just saved me a lot of while loop debugging and coding!
iamserious
@iamserious: np - keep naming your tables MyTable and most queries on SO will work ;)
RedFilter
hahaha!! thanks for the tip, btw my table name is really not "table" ! I just renamed it here!! but i like your sense of humour! (now that my problem is solved!!)
iamserious