表结构:

id(PK)(int)
name(varchar)
time1(int10) unixtime
time2(int10) unixtime

SELECT * FROM `user` ORDER BY `time1` DESC,`time2` DESC


 +------------------------------------------------------+
 | id        | name   |     time1     |     time2       |
 +------------------------------------------------------+
 | 12345     | Joe    |  1405605785   |   1406733506    |
 | 12346     | John   |  1406733506   |                 |
 | 12347     | David  |               |   1405684190    |
 +------------------------------------------------------+

我正在使用以下sql:
SELECT * FROM `user` ORDER BY `time1` DESC,`time2` DESC

如何组合两列时间来排序描述?
============
sagi的更多示例回复
 +------------------------------------------------------+
 | id        | name   |     time1     |     time2       |
 +------------------------------------------------------+
 | 12345     | Joe    |  1            |   2             |
 | 12346     | John   |  5            |                 |
 | 12347     | David  |               |   4             |
 +------------------------------------------------------+

我想这样排序(DESC)
John (time = 5)
David (time = 4)
Joe (time  max value = 2)

最佳答案

这称为条件排序,使用CASE EXPRESSION

SELECT * FROM `user` t
ORDER BY CASE WHEN t.time1 is null THEN t.time2 ELSE t.time1 END DESC

也可以写成:
ORDER BY coalesce(t.time1,t.time2)

您没有提供任何预期的结果,也没有说如果time1,time2NULL都不是time1的话,结果应该按哪个列排序,所以我假设您希望按第一列排序。如果不是这样,则用time2替换。
试试这个:
ORDER BY GREATEST(t.time1,t.time2) DESC

关于mysql - mysql命令由两个unixtime列合并,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37786336/

10-13 03:23