Search code examples
sqlsql-serverdatabaserdbms

How to select multiple rows based on AND condition on a column?


I am trying something like:

select name from myTable where id in (1056,1066,1069,1080,1404,1406,1407,3018)

But this is acting like OR condition and if I use AND between these values it does not give any result as it is trying to match in one cell.

Sample Data:

Name ID :   abc 1 |abc 2| abc 3| def 2| fgh 3

My expected output is:

Name: abc 

I want to find all the distinct names having all corresponding ID's Any pointer on this?


Solution

  • For the problem of this type, the best solution I could is use divide feature of Relational Algebra. It will give the expected result. Sample Example:- http://coronet.iicm.tugraz.at/Dbase1/scripts/rdbh06.htm