gpt4 book ai didi

mysql : How to get data from last July to Current date

转载 作者:行者123 更新时间:2023-11-29 10:16:04 24 4
gpt4 key购买 nike

我知道如何获取 7 月份的数据

SELECT * FROM tableName 
WHERE MONTH(created) = 7

但是我怎样才能获得去年七月至今的数据呢?

最佳答案

根据您的查询,我会选择:

SELECT * FROM tableName 
WHERE MONTH(created) = 7
AND (
( MONTH(CURDATE()) <= 7 AND YEAR(created) = YEAR(CURDATE()) - 1 )
OR
( MONTH(CURDATE()) > 7 AND YEAR(created) = YEAR(CURDATE()) )
)

如果您想要(就像您在评论中所要求的那样)自那​​时起的数据,请选择:

SELECT * FROM tableName 
WHERE created <= CURDATE()
AND (
(
MONTH(CURDATE()) <= 7
AND created >= DATE_SUB(DATE_FORMAT(CURDATE(), '%Y-07-01'), INTERVAL 1 YEAR)
)
OR
(
MONTH(CURDATE()) > 7
AND created >= DATE_FORMAT(CURDATE(), '%Y-07-01')
));

关于mysql : How to get data from last July to Current date,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50096132/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com