Jump to content


Photo

Mysql ordering


  • Please log in to reply
2 replies to this topic

#1 Tandem

Tandem
  • Members
  • PipPipPip
  • Advanced Member
  • 251 posts

Posted 14 August 2006 - 10:36 PM

If i want to order some query results by a factor that has no sort of reasonable order, but i know which order they should be in e.g

1. Dog
2. Cat
3. Lion

or something like that, how would i go about doing that?

If you need more information or i don't make sense, please ask/say.

Thanks in advance for any help.

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 14 August 2006 - 10:47 PM

You could use a SQL CASE statement
SELECT animal FROM mytable
ORDER BY 
CASE 
     WHEN animal='Dog' THEN 1
     WHEN animal='Cat' THEN 2
     WHEN animal='Lion' THEN 3
     ELSE 99
END

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 Tandem

Tandem
  • Members
  • PipPipPip
  • Advanced Member
  • 251 posts

Posted 14 August 2006 - 10:48 PM

Awesome, thats exactly what i need, thanks.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users