| 按天查询 | |
| 查看当天日期 | select current_date(); |
| 查看当天时间 | select current_time(); |
| 查看当天时间日期 | current_timestamp(); |
| 查询当天记录 | select * from 表名 where to_days(时间字段名) = to_days(now()); |
| 查询昨天记录 | SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) – TO_DAYS( 时间字段名) <= 1 |
| 查询10天的记录 | SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 10 DAY) <= date(时间字段名) |
| 按月查询 | |
| 查询当前月份的数据 | select * from 表名 where date_format(submittime,\'%Y-%m\')=date_format(now(),\'%Y-%m\') |
| 查询上一月的记录 | SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , ‘%Y%m’ ) , date_format( 时间字段名, ‘%Y%m’ ) ) =1 |
| 查询距离当前现在6个月的数据 | select name,submittime from enterprise where submittime between date_sub(now(),interval 6 month) and now(); |
| 上个月今天的当前时间 | select date_sub(now(),interval 1 month) |
| 获取本月最后一天 | select last_day(curdate()); |
| 获取本月第一天 | select date_add(curdate(), interval - day(curdate()) + 1 day); |
| 获取下个月第一天 | select date_add(curdate() - day(curdate()) + 1, interval 1 month); |
| 获取本月天数 | select day(last_day(curdate())); |
| 获取一个月前那一天 | select date_sub(curdate(), interval 1 month); |
| 按星期查询 | |
| 查询当前这周的数据 | SELECT * FROM 表名 WHERE YEARWEEK(date_format(submittime,\'%Y-%m-%d\')) = YEARWEEK(now()); |
| 查询上周的数据 | SELECT * FROM 表名 WHERE YEARWEEK(date_format(submittime,\'%Y-%m-%d\')) = YEARWEEK(now())-1; |
| 按季度查询 | |
| 查询本季度数据 | select * from 表名 where QUARTER(create_date)=QUARTER(now()); |
| 查询上季度数据 | select * from 表名 where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER)); |
| 按年查询 | |
| 查询本年数据 | select * from 表名 where YEAR(create_date)=YEAR(NOW()); |
| 查询上年数据 | select * from 表名 where year(create_date)=year(date_sub(now(),interval 1 year)); |
相关文章:
- MySQL 分时间段查询 2021-12-22
- mysql时间段内查询 2021-12-28
- MYSQL时间查询相关 2021-10-14
- mysql各种时间查询 2021-12-28
- mysql 常用时间查询 2021-11-22
- mysql时间条件查询 2021-12-12
- Mysql查询时间范围 2021-12-18
- mysql 查询 时间作为查询条件 2021-11-12