Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    161

    Unanswered: ORDER BY value of index - not position

    I have a MySQL query
    Code:
    SELECT `Id`, `Info` 
    FROM `tbl1` 
    WHERE `Id` = 5 OR `Id` = 1 OR `Id` = 3
    I want it returned as
    Code:
    5, info5
    1, info1
    3, info3
    Im not looking for position - position looks at the order of Id - Im looking for the value of Id.
    Is there any way to accomplish this ? Or would I need to execute 3 separate queries ?

    Thanks

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Code:
    select id
         , info 
      from tbl1 
     where id in (5,1,3)
    order
        by field(id,5,1,3)
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    161
    Cannot believe I skipped this from the manual.
    Thanks r937 - you've always been prmopt and helpful. Your time here is truly appreciated.

    Strange though - it works in phpMyAdmin but not my PHP script
    FUNCTION db1.FIELD does not exist
    I have in m script
    Code:
    $Res = mysql_query($SQL) or die("\n<!--$SQL-->\n".mysql_error());
    I exectued what was inbetween <!-- and --> and worked !
    Last edited by anjanesh; 03-05-06 at 08:08.

Posting Permissions

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