How do I sort mysql table result in ORDER BY 2 columns

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
adsegzy
Forum Contributor
Posts: 184
Joined: Tue Jul 28, 2009 9:26 am

How do I sort mysql table result in ORDER BY 2 columns

Post by adsegzy »

Hello Friends,

I have a products table with several columns. On my Home page I want to display 30 items at random but all new Items to come first. Below are the codes I tried.

Code: Select all

$sql = mysql_query("SELECT * FROM products WHERE status='Online' && nature='Yes' ORDER BY RAND() LIMIT 5"); //FOR ONLY NEW ITEMS

$sql = mysql_query("SELECT * FROM products WHERE status='Online' && nature!='Yes' ORDER BY RAND() LIMIT 35"); //FOR OTHER ITEMS
How do I make them into one line.

Thanks
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How do I sort mysql table result in ORDER BY 2 columns

Post by requinix »

If "nature" is either Yes or No (not a great scheme, by the way) then you can sort by nature descending first and rand() second. That will order the "Yes"es before the "No"s.
Post Reply