简体   繁体   English

按多列对行进行排序

[英]Sort rows by multiple columns

A the title says, I'm looking for a way to sort a set of rows from a MySQL table.标题说,我正在寻找一种方法来对 MySQL 表中的一组行进行排序。

I've simplified my example a bit, but the resulting solution should apply to my exact setup as well.Here's my setup:我已经稍微简化了我的示例,但最终的解决方案也应该适用于我的确切设置。这是我的设置:

events
----------
event_name
created
updated


Event A 
12-09-2012
14-09-2012

Event B
12-09-2012
-

Event C
09-09-2012
13-09-2012  

I'm looking for a way to sort the rows based on most recent 'activity', so whichever date is the newest.我正在寻找一种根据最近的“活动”对行进行排序的方法,因此无论哪个日期都是最新的。 So with the above values, the result should be A, C, B. (from most recent to older).因此,对于上述值,结果应该是 A、C、B.(从最近到较旧)。 It is worth noting that I can't change anything about the tables.值得注意的是,我无法更改表格的任何内容。

How would I go around doing this?我该怎么做呢?

Regards, Wouter问候, 沃特

I think I might be on to something here...我想我可能会在这里做点什么......

SELECT *, GREATEST(created, updated) AS last_activity FROM events ORDER BY last_activity DESC

This does seem to work, but I'll have to do some testing to see if it is foolproof.这似乎确实有效,但我必须进行一些测试以查看它是否万无一失。

Try this, should be this simple试试这个,应该就这么简单

 SELECT event_Name From Events
 ORDER created DESC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM