1. 程式人生 > >MySQL—查詢某時間範圍的數據

MySQL—查詢某時間範圍的數據

數據 寫到 day pos from mys rom post sql

-- 查詢今天的數據

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

MySQL—查詢某時間範圍的數據