lovespring
lovespring

Reputation: 19569

How to ORDER mysql rows by multiple columns?

such as "order by col_location", if location is the same, then "order by col_time"

Upvotes: 3

Views: 216

Answers (3)

duffymo
duffymo

Reputation: 308763

You can string columns in an ORDER BY, separated by commas.

Upvotes: 1

Henrik Adolfsson
Henrik Adolfsson

Reputation: 609

SELECT * FROM something
ORDER BY col_location, col_time DESC;

Upvotes: 4

davek
davek

Reputation: 22915

As others have said,listing the different columns in order of priority. You can also go one step further and build logic into your ORDER BY so that it becomes conditional e.g.

order by case when col_location = col_something_else then 
   col_location 
else 
   col_time 
end

Upvotes: 0

Related Questions