mightyspaj3
mightyspaj3

Reputation: 471

MySQL - How to order by two columns consecutively?

I have this simple query that orders the products on my web application in descending order, based on the price/discount.

The problem is that I want to order items by the discount first, and then order items by the cost. At the moment it's jumbling the two together.

Here's my PHP:

$products = DB::fetch("SELECT SQL_CALC_FOUND_ROWS * FROM `products` ORDER BY discount DESC, cost DESC");

Upvotes: 0

Views: 69

Answers (2)

Paolo
Paolo

Reputation: 15827

It seem by your query that you're paginating results (before executing the query posted in the question).

In that case you have to sort with the desired ordering before you paginate.

Otherwise you'll get a page with randomly sorted items and then you'll sort just that page not getting the expected result.

Upvotes: 1

Danyal Sandeelo
Danyal Sandeelo

Reputation: 12391

Try:

SELECT SQL_CALC_FOUND_ROWS * FROM `products` ORDER BY discount,cost DESC

That's all

Upvotes: 0

Related Questions