MySQL - 查询今天的数据(以及昨天、本月、上个月、今年...)

超、凢脫俗 2021-09-15 08:40 531阅读 0赞

建表语句如下:










1


2


3


4


5


6



CREATE 
TABLE 
</code> <code style="font-size:14px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;line-height:1.5em;color:rgb(0,102,153);background:none;border:0px;float:none;margin:0px;padding:1px 0px;vertical-align:baseline;font-weight:bold;min-height:auto;">order</code> <code style="font-size:14px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;line-height:1.5em;background:none;border:0px;float:none;margin:0px;padding:1px 0px;vertical-align:baseline;min-height:auto;"> (


  
id 
INTEGER 
UNSIGNED 
NOT 
NULL 
AUTO_INCREMENT,


  
order_name 
VARCHAR
(45) 
NOT 
NULL
,


  
order_time DATETIME 
NOT 
NULL
,


  
PRIMARY 
KEY 
(id)


)

下面根据 order_time 字段来查询各个时间段内的所有记录。

1,查询当天(今天)的数据










1



SELECT 

FROM 
</code> <code style="font-size:14px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;line-height:1.5em;color:rgb(0,102,153);background:none;border:0px;float:none;margin:0px;padding:1px 0px;vertical-align:baseline;font-weight:bold;min-height:auto;">order</code> <code style="font-size:14px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;line-height:1.5em;background:none;border:0px;float:none;margin:0px;padding:1px 0px;vertical-align:baseline;min-height:auto;"> 
WHERE 
TO_DAYS(order_time) = TO_DAYS(NOW())

2,查询昨天的数据










1



SELECT 

FROM 
</code> <code style="font-size:14px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;line-height:1.5em;color:rgb(0,102,153);background:none;border:0px;float:none;margin:0px;padding:1px 0px;vertical-align:baseline;font-weight:bold;min-height:auto;">order</code> <code style="font-size:14px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;line-height:1.5em;background:none;border:0px;float:none;margin:0px;padding:1px 0px;vertical-align:baseline;min-height:auto;"> 
WHERE 
TO_DAYS(NOW()) - TO_DAYS(order_time) = 1

3,查询最近7天的数据(包括今天一共7天)










1



SELECT 

FROM 
</code> <code style="font-size:14px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;line-height:1.5em;color:rgb(0,102,153);background:none;border:0px;float:none;margin:0px;padding:1px 0px;vertical-align:baseline;font-weight:bold;min-height:auto;">order</code> <code style="font-size:14px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;line-height:1.5em;background:none;border:0px;float:none;margin:0px;padding:1px 0px;vertical-align:baseline;min-height:auto;"> 
where 
DATE_SUB(CURDATE(), INTERVAL 7 
DAY
) < 
date
(order_time)

4,查询最近30天的数据(包括今天一共30天)










1



SELECT 

FROM 
</code> <code style="font-size:14px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;line-height:1.5em;color:rgb(0,102,153);background:none;border:0px;float:none;margin:0px;padding:1px 0px;vertical-align:baseline;font-weight:bold;min-height:auto;">order</code> <code style="font-size:14px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;line-height:1.5em;background:none;border:0px;float:none;margin:0px;padding:1px 0px;vertical-align:baseline;min-height:auto;"> 
where 
DATE_SUB(CURDATE(), INTERVAL 30 
DAY
) < 
date
(order_time)

5,查询当月(本月)的数据










1



SELECT 

FROM 
</code> <code style="font-size:14px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;line-height:1.5em;color:rgb(0,102,153);background:none;border:0px;float:none;margin:0px;padding:1px 0px;vertical-align:baseline;font-weight:bold;min-height:auto;">order</code> <code style="font-size:14px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;line-height:1.5em;background:none;border:0px;float:none;margin:0px;padding:1px 0px;vertical-align:baseline;min-height:auto;"> 
WHERE 
DATE_FORMAT(order_time, 
‘%Y%m’
) = DATE_FORMAT(CURDATE(), 
‘%Y%m’
)

6,查询上个月的数据










1



SELECT 

FROM 
</code> <code style="font-size:14px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;line-height:1.5em;color:rgb(0,102,153);background:none;border:0px;float:none;margin:0px;padding:1px 0px;vertical-align:baseline;font-weight:bold;min-height:auto;">order</code> <code style="font-size:14px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;line-height:1.5em;background:none;border:0px;float:none;margin:0px;padding:1px 0px;vertical-align:baseline;min-height:auto;"> 
WHERE 
PERIOD_DIFF(DATE_FORMAT(NOW(),
‘%Y%m’
), DATE_FORMAT(order_time,
‘%Y%m’
)) =1

7,查询本季度的数据










1



SELECT 

FROM 
</code> <code style="font-size:14px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;line-height:1.5em;color:rgb(0,102,153);background:none;border:0px;float:none;margin:0px;padding:1px 0px;vertical-align:baseline;font-weight:bold;min-height:auto;">order</code> <code style="font-size:14px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;line-height:1.5em;background:none;border:0px;float:none;margin:0px;padding:1px 0px;vertical-align:baseline;min-height:auto;"> 
WHERE 
QUARTER(order_time)=QUARTER(NOW())

8,查询上季度的数据










1



SELECT 

FROM 
</code> <code style="font-size:14px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;line-height:1.5em;color:rgb(0,102,153);background:none;border:0px;float:none;margin:0px;padding:1px 0px;vertical-align:baseline;font-weight:bold;min-height:auto;">order</code> <code style="font-size:14px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;line-height:1.5em;background:none;border:0px;float:none;margin:0px;padding:1px 0px;vertical-align:baseline;min-height:auto;"> 
WHERE 
QUARTER(order_time)=QUARTER(DATE_SUB(NOW(),INTERVAL 1 QUARTER))

9,查询当年(今年)的数据










1



SELECT 

FROM 
</code> <code style="font-size:14px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;line-height:1.5em;color:rgb(0,102,153);background:none;border:0px;float:none;margin:0px;padding:1px 0px;vertical-align:baseline;font-weight:bold;min-height:auto;">order</code> <code style="font-size:14px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;line-height:1.5em;background:none;border:0px;float:none;margin:0px;padding:1px 0px;vertical-align:baseline;min-height:auto;"> 
WHERE 
YEAR
(order_time)=
YEAR
(NOW())

10,查询去年的数据










1



SELECT 

FROM 
</code> <code style="font-size:14px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;line-height:1.5em;color:rgb(0,102,153);background:none;border:0px;float:none;margin:0px;padding:1px 0px;vertical-align:baseline;font-weight:bold;min-height:auto;">order</code> <code style="font-size:14px;font-family:Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;line-height:1.5em;background:none;border:0px;float:none;margin:0px;padding:1px 0px;vertical-align:baseline;min-height:auto;"> 
WHERE 
YEAR
(order_time)=
YEAR
(DATE_SUB(NOW(),INTERVAL 1 
YEAR
))

发表评论

表情:
评论列表 (有 0 条评论,531人围观)

还没有评论,来说两句吧...

相关阅读