MySQL—查询某时间范围的数据

-- 查询今天的数据

select * from `user` where to_days(birthday) = to_days(CURDATE());

-- 查询昨天的数据

select * from `user` where to_days(CURDATE()) - to_days(birthday)<=1;

-- 查询最近7天的数据

select * from `user` where birthday > DATE_SUB(CURDATE(),INTERVAL 7 DAY);

-- 查询最近一个季度的数据

select * from `user` where birthday > DATE_SUB(CURDATE(), INTERVAL 3 MONTH)

-- 最近一年的数据

select * from `user` where birthday > DATE_SUB(CURDATE(), INTERVAL 1 YEAR);

 

-- 本季度的数据

select * from `user` where quarter(birthday) = quarter(CURDATE());

-- 上季度的数据

select * from `user` where quarter(birthday) = quarter(DATE_SUB(CURDATE(), INTERVAL 1 QUARTER));

 

-- 查询今年的数据

select * from `user` where year(CURDATE()) - year(birthday) = 28 ;

 

-- 查询第几月的数据

select * from `user` where month(birthday) = 8 ;

 

-- 查询某年某月某日的数据

select * from `user` where date_format(birthday,'%Y-%m-%d')='2017-07-07';

 

-- 查询制定时间段内的数据(只写到月,会出错)

select * from `user` where birthday between '1888-5-1 00:00:00' and '2017-9-3 00:00:00';

 

-- 查询制定时间段内的数据(只写到月,会出错)

select * from `user` where birthday > '1989-5-1' and birthday < '2017-5-1';

转载于:https://www.cnblogs.com/domi22/p/8059330.html

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值