Can I Order by two fields, so it shows order by one first?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Can I Order by two fields, so it shows order by one first?

Post by simonmlewis »

Code: Select all

$result = mysql_query ("SELECT * FROM products WHERE pause = 'off' AND (preorder = 'yes' OR comingsoon = 'yes') ORDER BY preorder='yes' ASC");
I need to show all those that are "preorder='yes'" first, and show comingsoon = 'yes' after.

What I've tried, doesn't work. It's showing Comingsoon first.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Can I Order by two fields, so it shows order by one firs

Post by Celauran »

Maybe this?

Code: Select all

SELECT * FROM products WHERE pause = 'off' AND (preorder = 'yes' OR comingsoon = 'yes') ORDER BY preorder DESC, comingsoon DESC
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Can I Order by two fields, so it shows order by one firs

Post by simonmlewis »

Ahh that's it, thanks.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply