MySql日期查询语句详解
程序员文章站
2024-02-25 08:06:40
使用date_format方法select * from `ler_items` where date_format(posttime,'%y-%m')='2013-03'...
使用date_format方法
select * from `ler_items` where date_format(posttime,'%y-%m')='2013-03'
注意:日期一定要用'',否则没有效果
其它的一些关于mysql日期查找语句
mysql> select date_format(date_sub(curdate(), interval 7 day),'%y%m%d');
+———————————————————–+
| date_format(date_sub(curdate(), interval 7 day),'%y%m%d') |
+———————————————————–+
| 120416 |
+———————————————————–+
1 row in set (0.00 sec)
可以获取7天前的日期,
使用date_sub(curdate(), interval 7 day) 来获得7天前的时间,用date_format 来指定输出格式。
今天
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(now(),'%y-%m')
查询距离当前现在6个月的数据
select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();
查询上个月的数据
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 下月第一天
where date(regdate) = curdate();
select * from test where year(regdate)=year(now()) and month(regdate)=month(now()) and day(regdate)=day(now())
select date( c_instime ) ,curdate( )
from `t_score`
where 1
limit 0 , 30
select * from `ler_items` where date_format(posttime,'%y-%m')='2013-03'
注意:日期一定要用'',否则没有效果
其它的一些关于mysql日期查找语句
mysql> select date_format(date_sub(curdate(), interval 7 day),'%y%m%d');
+———————————————————–+
| date_format(date_sub(curdate(), interval 7 day),'%y%m%d') |
+———————————————————–+
| 120416 |
+———————————————————–+
1 row in set (0.00 sec)
可以获取7天前的日期,
使用date_sub(curdate(), interval 7 day) 来获得7天前的时间,用date_format 来指定输出格式。
今天
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(now(),'%y-%m')
查询距离当前现在6个月的数据
select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now();
查询上个月的数据
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 下月第一天
where date(regdate) = curdate();
select * from test where year(regdate)=year(now()) and month(regdate)=month(now()) and day(regdate)=day(now())
select date( c_instime ) ,curdate( )
from `t_score`
where 1
limit 0 , 30