Advanced Search

Results 1 to 8 of 8

Thread: mysql select array id's, i.e. multiple id's

  1. #1
    Join Date
    Feb 2007
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default mysql select array id's, i.e. multiple id's

    I'm looking for a mysql select statement which lets me select several id's in one go, not just one - like this:
    Code:
    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

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    SELECT * FROM mytable WHERE id IN (2, 5, 11);
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Feb 2007
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks a bundle!

  4. #4
    Join Date
    Aug 2005
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey View Post
    Code:
    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:
    Code:
    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

  5. #5
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    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

  6. #6
    Join Date
    Aug 2005
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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?

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    SELECT * FROM mytable WHERE id IN (11, 2, 5) ORDER BY FIELD(id, 11, 2, 5);
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Aug 2005
    Posts
    94
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thx that worked!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •