PDA

View Full Version : mysql select array id's, i.e. multiple id's



jonas-e
08-19-2007, 03:28 PM
I'm looking for a mysql select statement which lets me select several id's in one go, not just one - like this:

SELECT * FROM mytable
WHERE id = [2, 5, 11]
(obviously that statement does not work).
I would prefer to do it in one statement rather than creating a loop doing it in multiple statemnts.
Can someone help me out?
Kindly,
Jonas

Twey
08-19-2007, 03:50 PM
SELECT * FROM mytable WHERE id IN (2, 5, 11);

jonas-e
08-19-2007, 04:28 PM
Thanks a bundle!

Null
12-07-2007, 08:39 PM
SELECT * FROM mytable WHERE id IN (2, 5, 11);
Ok and what to do if I want to get these id's AND order them in the way they are given, so like:

SELECT * FROM mytable WHERE id IN (11, 2, 5);
So it shows the id's in this order to. Currently the output will be 2, 5, 11 and not 11, 2, 5 like I need

Thx

boogyman
12-07-2007, 09:37 PM
having it jump like that doesnt make sense unless it was a typo and you meant
11,5,2.

Starting at the "end" then jumping to the beginning to me would appear that it would cause a problem, and as far as I know, there isnt a way to make it jump, however maybe someone else knows of a technique.

The only way I know of would be to perform 2 queries, one for the 11 and one for the 2,5

Null
12-08-2007, 11:28 AM
Nope, I want to order it the same way as I select the id's.

Sometimes it's 2, 4, 1 and sometimes 4, 2, 5 it's dynamic and that order must be presented.. but how to do that?

Twey
12-08-2007, 02:09 PM
SELECT * FROM mytable WHERE id IN (11, 2, 5) ORDER BY FIELD(id, 11, 2, 5);

Null
12-11-2007, 07:44 PM
Thx that worked!