mysql 查询当天、本周,本月,上一个月的数据

今天

select * from 表名 where to_days(时间字段名) = to_days(now());

昨天

SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) - TO_DAYS( 时间字段名) <= 1

近7天

SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(时间字段名)

近30天

SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(时间字段名)

本月

SELECT * FROM 表名 WHERE DATE_FORMAT( 时间字段名, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )

上一月

SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( 时间字段名, '%Y%m' ) ) =1

查询本季度数据

select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(now());
查询上季度数据
select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));

查询本年数据

select * from `ht_invoice_information` where YEAR(create_date)=YEAR(NOW());

查询上年数据

select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));

查询当前这周的数据

SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now());

查询上周的数据

SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now())-1;

查询上个月的数据

select name,submittime from enterprise where date_format(submittime,'%Y-%m')=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m')
select * from user where DATE_FORMAT(pudate,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m') ; 
select * from user where WEEKOFYEAR(FROM_UNIXTIME(pudate,'%y-%m-%d')) = WEEKOFYEAR(now()) 
select * from user where MONTH(FROM_UNIXTIME(pudate,'%y-%m-%d')) = MONTH(now()) 
select * from user where YEAR(FROM_UNIXTIME(pudate,'%y-%m-%d')) = YEAR(now()) and MONTH(FROM_UNIXTIME(pudate,'%y-%m-%d')) = MONTH(now()) 
select * from user where pudate between 上月最后一天 and 下月第一天
查询当前月份的数据
select name,submittime from enterprise  where date_format(submittime,'%Y-%m')=date_format(now(),'%Y-%m')

查询距离当前现在6个月的数据

select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();

PS:下面看下mysql如何查询当天信息?

原来不是太熟悉SQL查询语句,什么都是用到了再去查去找,还好网络提供给我们很多支持。今天又用到了一个语句,一时间真想不出怎么解决,到网上看了看,感觉就有一个,怎么那么简单啊。需要积累的东西真是太多了。

今天就把我这个简单的问题记录下来吧!算是一个积累:

mysql查询当天的所有信息:

select * from test where year(regdate)=year(now()) and month(regdate)=month(now()) and day(regdate)=day(now())

这个有一些繁琐,还有简单的写法:

select * from table where date(regdate) = curdate();

查询本年的所有月份

select concat((select year(now())),'-01') as `date`
union select concat((select year(now())),'-02')
union select concat((select year(now())),'-03')
union select concat((select year(now())),'-04')
union select concat((select year(now())),'-05')
union select concat((select year(now())),'-06')
union select concat((select year(now())),'-07')
union select concat((select year(now())),'-08')
union select concat((select year(now())),'-09')
union select concat((select year(now())),'-10')
union select concat((select year(now())),'-11')
union select concat((select year(now())),'-12')

1.查询本月第一天

select date_add(curdate(),interval-day(curdate())+1 day) as date;

2.查询本月最后一天

SELECT last_day(curdate()) as date;

3.查询当前日期

select curdate();

4.查询下个月的第一天

select date_add(curdate() - day(curdate()) +1,interval 1 month );

5.查询当前月已过了几天

select day(curdate());

6.获取当前月的天数(先加一个月,再减今天是第几天,得到当前月的最后一天,最后求最后一天是几号)

select day(date_add( date_add(curdate(),interval 1 month),interval -day(curdate()) day ));

7.查询上个月的第一天

select date_sub(date_sub(date_format(now(),‘%y-%m-%d’),interval extract(day from now())-1 day),interval 1 month);

8.查询上个月的最后一天

select date_sub(date_sub(date_format(now(),’%y-%m-%d’),interval extract(day from now()) day),interval 0 month) as date;

9.查询当月所有日期

select date from (
SELECT DATE_FORMAT(DATE_SUB(last_day(curdate()), INTERVAL xc-1 day), '%Y-%m-%d') as date
FROM ( 
            SELECT @xi:=@xi+1 as xc from 
            (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6) xc1, 
            (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6) xc2,  
            (SELECT @xi:=0) xc0 
) xcxc) x0 where x0.date >= (select date_add(curdate(),interval-day(curdate())+1 day));

10.查询某段时间段内的所有月份

SELECT
            DATE_FORMAT( DATE_ADD( CONCAT( '2023-04', '-01' ), INTERVAL -( CAST( help_topic_id AS SIGNED INTEGER )) MONTH ), '%Y-%m' ) sdate 
        FROM
            mysql.help_topic 
        WHERE
            help_topic_id <= TIMESTAMPDIFF(MONTH,CONCAT( '2022-04', '-01' ),CONCAT( '2023-04', '-01' )) 
ORDER BY  sdate
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容