开发者

How to ORDER mysql rows by multiple columns?

开发者 https://www.devze.com 2022-12-13 17:49 出处:网络
such as \"order by col_location\", if location is the same, then \"order by co开发者_如何学Gol_time\"SELECT * FROM something

such as "order by col_location", if location is the same, then "order by co开发者_如何学Gol_time"


SELECT * FROM something
ORDER BY col_location, col_time DESC;


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消