• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
返回主页

老人与JAVA

https://share.weiyun.com/bce1b8f78c154269be42a9e4289229b6/aPhM3p

  • 博客园
  • 首页
  • 联系
  • 订阅
  • 管理

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';

posted @ 2017-12-18 20:00  老人与JAVA  阅读(36989)  评论(2)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3