| Author |
Topic |
|
smckechnie
Starting Member
7 Posts |
Posted - 2006-09-12 : 01:26:35
|
| Hi All.Is there a way to have multiple AND conditions on the same field in a database.EXAMPLESELECT * FROM tblWHERE field1 = 1 AND field1 = 2 AND field1 = 5Thanks |
|
|
timmy
Master Smack Fu Yak Hacker
1242 Posts |
Posted - 2006-09-12 : 01:34:05
|
| Not the way you've done it. I'm assuming you're trying to find out if the given field contains all the characters 1, 2 & 5?Try using the LIKE function for this.SELECT * from tblWHERE field1 LIKE '%1%' AND field1 LIKE '%2%' etc etc |
 |
|
|
smckechnie
Starting Member
7 Posts |
Posted - 2006-09-12 : 01:46:13
|
| Hi Timmy thanks for the quick response, however what I am trying to do is. I have a table with contact details like 1 = Home, 2 = work, 3 = Fax, 4 = email, 5 = Cell. Now these are captured into a table with the clientID number on individual rows.Now I am trying to retreave clients that have Home,work and Cell contact number stored in the tables.Thanks |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-09-12 : 01:50:08
|
Try ORing the conditions:Select * from TblWhere Field1 = 1 or Field1 = 2 or Field1 = 3 or Field1 = 4 or Field1 = 5 Another solution is to use IN clause:Select * from Tblwhere Field1 in (1,2,3,4,5) Harsh AthalyeIndia."Nothing is Impossible" |
 |
|
|
smckechnie
Starting Member
7 Posts |
Posted - 2006-09-12 : 01:52:24
|
| Thanks Harsh.But using IN and OR will return clients with any one of those values I am looking for clients with all the values |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-09-12 : 02:15:16
|
| Select ClientID from TblGroup by ClientIDHaving Count(ContactDetails) = 5Of course, I am assuming here that each clientID can't have any of the contact detail type (1 = home, 2 = work etc.) repeated !Harsh AthalyeIndia."Nothing is Impossible" |
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-09-12 : 02:20:35
|
| How are you saving the ClientId in the column field1? is it Comma Seperated?Chirag |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-09-12 : 02:22:33
|
add a distinct if you have duplicate contact detail type Select ClientIDfrom TblGroup by ClientIDHaving Count(distinct ContactDetails) = 5 KH |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-09-12 : 02:44:50
|
Another solution can be this, although costly performance-wise:SELECT DISTINCT X.ClientID FROM TBL XJOIN(SELECT ClientID FROM TBL WHERE ContactDetails = 1) AS X1ON X.ClientID = X1.ClientIDJOIN(SELECT ClientID FROM TBL WHERE ContactDetails = 2) AS X2ON X.ClientID = X2.ClientIDJOIN(SELECT ClientID FROM TBL WHERE ContactDetails = 3) AS X3ON X.ClientID = X3.ClientIDJOIN(SELECT ClientID FROM TBL WHERE ContactDetails = 4) AS X4ON X.ClientID = X4.ClientIDJOIN(SELECT ClientID FROM TBL WHERE ContactDetails = 5) AS X5ON X.ClientID = X5.ClientID Harsh AthalyeIndia."Nothing is Impossible" |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-09-13 : 09:31:07
|
| Select ClientID from TblWhere contactDetails in(1,2,5)Group by ClientIDHaving Count(distinct ContactDetails) = 3MadhivananFailing to plan is Planning to fail |
 |
|
|
|