Ebad ghafoory Ebad ghafoory - 3 months ago 26
MySQL Question

SQL Order By - Show certain records last

I want to list my products in sorted by price from low to high, but I also want out of stock products show after products that have quantity. I'm not sure how to do that with an


Here is my current query, I want to add something to show out of stock products last:

SELECT * FROM products WHERE cat_id='$cat_id' ORDER BY price LIMIT $start,$record

Answer Source

Use a CASE statement in your ORDER BY:

ORDER BY CASE WHEN quantity <> 0 THEN 0 ELSE 1 END, price
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download