How to query mysql rows by multiple columns?

for example, "order by col_location", if the location is the same, then "order by col_time"

+3
source share
3 answers

You can enter columns in ORDER BY, separated by commas.

+1
source
SELECT * FROM something
ORDER BY col_location, col_time DESC;
+4
source

As others have said, listing different columns in order of priority. You can also take one more step and build logic in your own ORDER BY, so that it becomes conditional, for example.

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

Source: https://habr.com/ru/post/1725478/


All Articles