binnathon
binnathon

Reputation: 121

Reorder results from another query

SELECT * FROM (
   SELECT * FROM cars WHERE site = '5' 
   ORDER BY cost DESC LIMIT 0 , 10
)
ORDER BY time

How would I execute a sql query like this? So first it selects the 10 cars with the highest cost, THEN it reorders those 10 cars by what time they were added to the DB.

I tried to figure it out but I just cannot get a grip on the syntax :P

Upvotes: 0

Views: 394

Answers (2)

Chintan Udeshi
Chintan Udeshi

Reputation: 352

This query will give you the desired results
SELECT * FROM ( SELECT * FROM cars WHERE site = 5 
ORDER BY cost DESC LIMIT 0 , 10 ) as t ORDER BY time

Upvotes: 1

Ullas
Ullas

Reputation: 11566

Just give an alias to the sub-query.

SELECT * FROM (
   SELECT * FROM `cars` WHERE `site` = '5' 
   ORDER BY `cost` DESC LIMIT 0 , 10
)t
ORDER BY `time`;

Upvotes: 2

Related Questions