您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

MYSQL SQL怎么查询近7天一个月的数据

2024/3/28 22:08:12发布4次查看
mysql sql查询近7天,一个月的数据//今天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')
近一个月统计sql
select user_id, user_name, createtimefrom t_userwhere date_sub(curdate(), interval 1 month) <= date(createtime);
同理,近一个星期为: interval 7 day。
补充:mysql 中sql 语句查询今天、昨天、近7天、近30天、一个月内、上一月数据1、几个小时内的数据
date_sub(now(), interval 5 hour)
2、今天
select * from 表名 where to_days(时间字段名) = to_days(now());
3、昨天
select * from 表名 where to_days( now( ) ) - to_days( 时间字段名) <= 1;
4、7天
select * from 表名 where date_sub(curdate(), interval 7 day) <= date(时间字段名);
5、近30天
select * from 表名 where date_sub(curdate(), interval 30 day) <= date(时间字段名);
6、本月
select * from 表名 where date_format( 时间字段名, '%y%m' ) = date_format( curdate( ) , '%y%m' );
7、上一月
select * from 表名 where period_diff( date_format( now( ) , '%y%m' ) , date_format( 时间字段名, '%y%m' ) ) =1;
以上就是mysql sql怎么查询近7天一个月的数据的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录