mysql date显示结果今天/昨天/周 [英] mysql date show results today/yesterday/week

查看:77
本文介绍了mysql date显示结果今天/昨天/周的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从表中检索数据,并显示条目的总SUM。我想做的是显示今天,昨天和本月的作品总SUM。该表正在使用unix时间戳格式(例如,1351771856)。



目前我正在使用此行显示今天的结果:

  AND comment_date> UNIX_TIMESTAMP() -  24 * 3600; 

但这只给了我最近24小时的条目



示例:所以让我们说,星期五下午17:00 - 它给我从星期四17:00 PM到星期五17 :00 PM



我想要是获取



    $的结果b $ b
  • 星期四00:00:00 - 23:59:59(昨天在这种情况下)

  • 今天的结果(00:00:00 - 23:59: 59)

  • 和上周的结果,从星期一00:00:00开始,直到今天(本例中为星期五)。



我在MySQL文档中找不到方法来实现这一点。

解决方案

p>这个mysql代码应该适合你:

  //今天
AND DATE(from_unixtime(comment_date))= CURRENT_DATE

//昨天
AND DATE(from_unixtime(comment_date))= DATE_SUB(CURRENT_DATE,INTERVAL 1 DAY)

// This周
AND YEARWEEK(from_unixtime(comment_date),1)= YEARWEEK(CURRENT_DATE,1)

//本月
AND YEAR(from_unixtime(comment_date))= YEAR(CURRENT_DATE) )
AND MONTH(from_unixtime(comment_date))= MONTH(CURRENT_DATE)


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.

解决方案

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)

这篇关于mysql date显示结果今天/昨天/周的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆