View Full Version : mysql select array id's, i.e. multiple id's
jonas-e
08-19-2007, 02: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
SELECT * FROM mytable WHERE id IN (2, 5, 11);
jonas-e
08-19-2007, 03:28 PM
Thanks a bundle!
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
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?
SELECT * FROM mytable WHERE id IN (11, 2, 5) ORDER BY FIELD(id, 11, 2, 5);
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.