doushang7209 2012-11-02 09:00
浏览 83
已采纳

mysql日期显示今天/昨天/周的结果

I am retrieving data from a table and show the total SUM of entries. What I want to do is to show the total SUM of entries made on today's date, yesterday and this month. The table is using the unix timestamp format (e.g. 1351771856 for example).

Currently I am using this line to show todays results:

AND comment_date > UNIX_TIMESTAMP() - 24 * 3600";

but that gives me just the entries for the last 24 hours.

Example: So let's say its Friday, 17:00 PM - it gives me the count from Thursday 17:00 PM to Friday 17:00 PM

What I want is to get the results for

  • Thursday 00:00:00 - 23:59:59 (yesterday in this case)
  • the results for today (00:00:00 - 23:59:59)
  • and last week, results that start on Monday, 00:00:00 until "today" (in this case Friday).

I couldn't find a way in the MySQL documentation to achieve this.

  • 写回答

3条回答 默认 最新

  • dongnao2048 2012-11-02 09:11
    关注

    This mysql code should work for you:

    // Today
    AND DATE(from_unixtime(comment_date)) = CURRENT_DATE
    
    // Yesterday
    AND DATE(from_unixtime(comment_date)) =  DATE_SUB(CURRENT_DATE,INTERVAL 1 DAY)
    
    // This week
    AND YEARWEEK(from_unixtime(comment_date), 1) =  YEARWEEK(CURRENT_DATE, 1)
    
    // This month
    AND YEAR(from_unixtime(comment_date)) = YEAR(CURRENT_DATE)
    AND MONTH(from_unixtime(comment_date)) = MONTH(CURRENT_DATE)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数