这个整理了完整的MySQL常用的时间范围查找!方便大家使用!

-- 今天
select * from 表名 where to_days(时间字段名) = to_days(now());
-- 昨天
select * from 表名 where to_days( now( ) ) - to_days(时间字段名) <= 1;
-- 本周
select * from 表名 where yearweek( date_format(时间字段名,'%y-%m-%d' ) ) = yearweek(now());
-- 上一周
select * from 表名 where yearweek(date_format(时间字段名, '%y-%m-%d')) = yearweek(now())-1;
-- 本月
select * from 表名 where date_format(时间字段名,'%y-%m')=date_format(now(),'%y-%m');
-- 上一月
select * from 表名 where period_diff(date_format(now(), '%y%m'), date_format(时间字段名, '%y%m')) = 1;
-- 本季度
select * from 表名 where quarter(时间字段名)=quarter(now());
-- 上一季度
select * from 表名 where quarter(时间字段名)=quarter(date_sub(now(),interval 1 quarter));
-- 本年
select * from 表名 where year(时间字段名)=year(now());
-- 上一年
select * from 表名 where year(时间字段名)=year(date_sub(now(),interval 1 year));
-- 查询距离当前现在6个月的数据
select * from 表名 where 时间字段名 between date_sub(now(),interval 6 month) and now();

-- 周 日期数据
-- 获取昨天日期
select DATE_SUB( curdate( ), INTERVAL 1 DAY );
-- 获取上周第一天
select date_sub(now(),interval dayofweek(now())-1+6 day);
-- 获取上周最后一天
select date_sub(now(),interval dayofweek(now())-1 day);
-- 获取本周第一天
select date_sub(now(),interval dayofweek(now())-2 day);
-- 获取本周最后一天
select date_add(now(),interval 7-dayofweek(now())+1 day);
-- 获取下周第一天
select date_add(now(),interval 7-dayofweek(now())+1+1 day);
-- 获取下周最后一天
select date_add(now(),interval 7-dayofweek(now())+1+7 day);

-- 月 日期数据
-- 本月天数
select day(last_day(curdate()));
-- 获取当前月已过了几天
select day(curdate());
-- 上月今天的当前日期
select date_sub(curdate(), interval 1 month);
-- 上月今天的当前时间(时间戳)
select unix_timestamp(date_sub(now(), interval 1 month));
-- 获取当前时间与上个月之间的天数
select datediff(curdate(), date_sub(curdate(), interval 1 month));
-- 获取本月的第一天
select date_add(curdate(), interval - day(curdate()) + 1 day);
-- 获取本月的最后一天
select last_day(curdate());
-- 获取上个月的第一天
select date_add(curdate() - day(curdate()) + 1, interval -1 month);
-- 上个月的最后一天
select last_day(date_sub(now(), interval 1 month));
-- 获取下个月的第一天
select date_add(curdate() - day(curdate()) + 1, interval 1 month);
-- 获取下个月最后一天
select last_day(date_sub(now(), interval -1 month));
-- 获取当前月的天数(先加一个月,再减今天是第几天,得到当前月的最后一天,最后求最后一天是几号)
select day(date_add(date_add(curdate(), interval 1 month), interval -day(curdate()) day));
-- 获取当前月的天数(先加一个月,再减今天是第几天,得到当前月的最后一天,最后求最后一天是几号)
select day(date_add(date_add(curdate(), interval 1 month), interval -day(curdate()) day));

-- 季度 日期数据
-- 返回上季度第一天
select concat(year(now()),'-',lpad((quarter(now())-1)*3-2,2,'0'),'-01');
-- 返回上季度最后一天
select date_sub(concat(year(now()),'-',lpad((quarter(now())-1)*3+1,2,'0'),'-01'),interval 1 day);
-- 返回本季度第一天
select concat(year(now()),'-',lpad((quarter(now())-1)*3+1,2,'0'),'-01');
-- 返回本季度最后一天
select last_day(concat(year(now()),'-',lpad(quarter(now())*3,2,0),'-01'));
-- 返回下季度第一天
select concat(year(now()),'-',lpad((quarter(now()))*3+1,2,'0'),'-01');
-- 返回下季度最后一天
select last_day(concat(year(now()),'-',lpad((quarter(now())+1)*3,2,0),'-01'));

-- 年 日期数据
-- 返回上年第一天
select concat(year(now())-1,'-01-01');
-- 返回上年最后一天
select concat(year(now())-1,'-12-31');
-- 返回本年第一天
select date_sub(now(),interval dayofyear(now())-1 day);
-- 返回本年最后一天
select concat(year(now()),'-12-31');
-- 返回下年第一天
select date_add(concat(year(now()),'-12-31'),interval 1 day);
-- 返回下年最后一天
select date_add(concat(year(now()),'-12-31'),interval 1 day);

-- 查询表结构包含注释
select * from information_schema.columns
where table_schema = 'brio' #表所在数据库
and table_name = 'user' ; #你要查的表

-- 数据库相差8小时解决
select now();
show variables like '%time_zone%'; -- 查询当前时区
set global time_zone = '+8:00'; ##修改mysql全局时区为北京时间,即我们所在的东8区
set time_zone = '+8:00'; ##修改当前会话时区
flush privileges; #立即生效

-- 查询重复 记录
select order_id, count(*) as count
from od_order_item
group by order_id
having count > 1;

-- 查询数据大小
select concat(round(sum(DATA_LENGTH/1024/1024),2),'M') from tables where table_schema='risk_fxjc' AND table_name='hft_machine_logs';
-- 查询表结构
desc hft_machine_logs;

特殊说明:
上述文章均是作者实际操作后产出。烦请各位,请勿直接盗用!转载记得标注原文链接:www.zanglikun.com
第三方平台不会及时更新本文最新内容。如果发现本文资料不全,可访问本人的Java博客搜索:标题关键字。以获取全部资料 ❤