duandong7980 2018-07-05 20:30
浏览 106
已采纳

在MySQL和PHP中按日期排序,并重视今天的日期

I want to make an order by date to get the comments of user, but I want to give the importance to today date

if there's a comment today show it first and make the ordering with date for the rest.

I try to make this but always give me an error in syntax

SELECT *
FROM comment
ORDER BY IF(`DATE_TIME_COMMENT` = CURRENT_DATE())

is there any solution ?

  • 写回答

2条回答 默认 最新

  • dongxun2089 2018-07-05 20:56
    关注

    First sort by a CASE returning "something lower", if date_time_comment is equal to current_date() and "something higher" else. Then, second, sort by date_time_comment.

    SELECT *
           FROM comment
           ORDER BY CASE date_time_comment
                      WHEN current_date()
                        THEN 0
                      ELSE
                        1
                    END,
                    date_time_comment;
    

    (Possibly add DESC after date_time_comment in the ORDER BY clause, if you want to have newest comments first.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序