---------------------------
|Status |Application Date|
|New |2019-01-02 |
|New |2019-01-01 |
|Updated |2019-01-15 |
|Deleted |2019-01-20 |
|Updated |2019-01-16 |
---------------------------
1. Sort in-order from New,Updated, and Deleted
a. Sort by New ASC - to see the first entry for first come first serve
b. Sort by Updated Desc - to see the latest update first
c. Sort by Deleted Desc - see the latest deleted
我已经尝试了 3 个查询和联合,但您可以将它们排序在一起,而不是按查询排序。
最佳答案
试试这个逻辑:
SELECT *
FROM yourTable
ORDER BY
FIELD(Status, 'New', 'Updated', 'Deleted'),
CASE WHEN Status = 'New' THEN UNIX_TIMESTAMP(app_date) ELSE -1.0*UNIX_TIMESTAMP(app_date) END;
第一级排序将新记录放在更新记录之前,将更新记录放在删除记录之前。第二级对新记录按日期升序排序,对所有其他记录按降序排序。
关于mysql - 如何在 mysql/mariadb 中实现这一点 - 排序和排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59608034/