Retreive more than one id

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
bdeonline
Forum Commoner
Posts: 42
Joined: Sun Jul 18, 2004 10:45 am

Retreive more than one id

Post by bdeonline »

I need a query that can retreieve more than one id.

Code: Select all

SELECT color FROM color WHERE id='2, 4, 1'
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Code: Select all

SELECT color FROM color WHERE id=2 or id=4 or id = 1
bdeonline
Forum Commoner
Posts: 42
Joined: Sun Jul 18, 2004 10:45 am

Post by bdeonline »

Thanks I knew it was something super simple but the simple answers are the hardest ones to find on google.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Or:
SELECT color FROM color WHERE FIND_IN_SET(id, '1,2,4');
fractalvibes
Forum Contributor
Posts: 335
Joined: Thu Sep 26, 2002 6:14 pm
Location: Waco, Texas

Post by fractalvibes »

Or
select color from color where id in ('1','2','3')
(standard sql)

Depending on DBMS, you could also have the where clause reference the return value of a subselect if you need to qualify that group of ids rather than hard-coding them...

fv
Post Reply