【问题标题】:MySQL ordering a date range to the beginning, then ordering everything else by other criteriaMySQL 将日期范围排序到开头,然后按其他条件排序其他所有内容
【发布时间】:2011-11-10 05:20:20
【问题描述】:

我有一个包含“创建日期”字段的项目表,我正在尝试找出一种对它们进行排序的方法,以便在过去两周内创建的项目首先出现,而不是在最后两周出现在那之后,按字母顺序排序。

我知道如何使用多个 sql 查询来做到这一点,但如果可能的话,我不希望这样做。可以这样做吗?

【问题讨论】:

    标签: php mysql date select


    【解决方案1】:
    select * from table
    order by
    case when date_created > curdate() - interval 2 week then 1 else 2 end,item
    

    更新答案

    (select * from table
    where date_created > curdate() - interval 2 week 
    order by date_created desc limit 0,10000000000)
    union all
    (select * from table
    where date_created < curdate() - interval 2 week 
    order by item
    limit 0,10000000000)
    

    LIMIT 的使用是必要的,当您必须在联合中同时应用 asc 和 desc 排序时。

    【讨论】:

    • 实际上,我必须添加什么才能对第一部分进行排序?就像我想按创建日期对 2 周日期范围内的项目进行排序一样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    • 2017-03-18
    • 2015-04-20
    相关资源
    最近更新 更多