简体   繁体   English

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

[英]mysql date show results today/yesterday/week

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 (eg 1351771856 for example). 该表使用unix时间戳格式(例如1351771856)。

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. 但这给了我过去24小时的参赛作品。

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 例如 :让我们说它的星期五,下午17:00 - 它给我从星期四下午17:00到星期五下午17:00的计数

What I want is to get the results for 我想要的是获得结果

  • Thursday 00:00:00 - 23:59:59 (yesterday in this case) 周四00:00:00 - 23:59:59(昨天在这种情况下)
  • the results for today (00:00:00 - 23:59:59) 今天的结果(00:00:00 - 23:59:59)
  • and last week, results that start on Monday, 00:00:00 until "today" (in this case Friday). 上周,结果从星期一00:00:00开始,直到“今天”(在这种情况下是星期五)。

I couldn't find a way in the MySQL documentation to achieve this. 我无法在MySQL文档中找到实现此目的的方法。

This mysql code should work for you: 这个mysql代码应该适合你:

// 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)

只需使用:

AND comment_date > date_sub(current_date, interval 1 day)

See my answer here, I think it's quite related. 在这里看到我的答案,我认为这是非常相关的。

Pull records from orders table for the current week 从当前周的订单表中提取记录

Consider getting intimate with MySQL's GROUP BY . 考虑与 MySQL的GROUP BY 密切相关 You will most likely need to know this if you use MySQL. 如果您使用MySQL,您很可能需要知道这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM