【问题标题】:how to achieve this in mysql/mariadb - Order and Sorting如何在 mysql/mariadb 中实现这一点 - 排序和排序
【发布时间】:2020-01-06 07:12:57
【问题描述】:
---------------------------
|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 个查询和联合,但您可以将它们一起排序,而不是每个查询。

【问题讨论】:

    标签: mysql database mariadb


    【解决方案1】:

    试试这个逻辑:

    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;
    

    第一级排序将新记录放在更新记录之前,将更新记录放在删除记录之前。第二级对新记录按日期升序排序,对所有其他记录按降序排序。

    【讨论】:

    • 感谢您的回复。但这不起作用,因为有旧数据需要排序。
    • 我不明白你的评论。我的回答对你不起作用怎么办?
    • 很抱歉,但是。它是正确的并且按预期工作。谢谢
    猜你喜欢
    • 2018-03-02
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 2022-01-06
    • 2019-11-20
    • 1970-01-01
    相关资源
    最近更新 更多