Welcome to WebmasterWorld Guest from 35.171.45.91

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

multiple id's?

     
7:48 pm on Dec 18, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 29, 2003
posts:94
votes: 0


Hi all, just a quick question, is it possible to select an id from a field containing more than one id number?

example:
select * from table where id=3

id field contains

1 blue
2,3 green
3 orange
4 yellow

this will select Orange but not Green, is it possible to select green aswell? and id=2 will still select Green?

many thanks

8:35 pm on Dec 18, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:May 22, 2002
posts:1001
votes: 0


If id is a list, then use the IN selector.

SELECT * FROM whatever WHERE id IN (2,3)

9:27 pm on Dec 18, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 29, 2003
posts:94
votes: 0


Thanks, i tried it without luck

ill try and explain better
i have a table with id field
1
2
3
etc
but i have a few rows that need to have 2 or 3 ids for example
1
2
3,34,65
4
i am selecting with where id=$id and need an id of 3 to select that row as well as an id of 34 etc

i tried IN ($id,$id) but this found nothing, im probably doing it wrong somewhere, still learning thank you for your patience.

9:35 pm on Dec 18, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:May 22, 2002
posts:1001
votes: 0


So... if ID is 3, then it should match rows..

1,3,5
3
3,5,6
1,34,5
30,6,9

Right? So use the wildcard % :
SELECT * FROM bananas WHERE id = '%3%'

% means "match ANYTHING", so it will match 3, 23, 32, 223,232,a3a, etc.

You can also use *, which means "Match ONE OF".

Exact wildcards may change from DB to DB, so if you get a strange error double check to make sure it works on your ODBC.

10:01 pm on Dec 18, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 29, 2003
posts:94
votes: 0


nearly

1,3,5 yes
3 yes
3,5,6 yes
1,34,5 NO
30,6,9 No

i tried the %xx% but that give the wrong results

id=3 needs to match 3, not with 3 in it.

Thanks for your help,

11:24 pm on Dec 18, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 29, 2003
posts:94
votes: 0


Got the problem sorted with a bit of trial and error

solution:

WHERE id RLIKE '[[:<:]]".$id."[[:>:]]'

Thanks