關於時間週期內資料查詢,每次用每次查,今天看到一篇不錯的文章,總結很全面,立馬收藏
to_days(date)函式
用法:給定乙個日期,返回乙個天數(年為0以來的天數)
示例:select to_days(20170101);
返回:736695
now()函式
用法:函式以"yyyy-mm-dd hh:mm:dd"或"yyyymmddhhmmss.uuuuuuu"格式的字串或數字返回配置的時區中的當前日期和時
間。函式的返回型別取決於使用它的上下文
示例:select
now();
返回:2018-01-23
11:59:25
示例: select
now() + 0;
返回:20180123115953
今天select * from 表名 where to_days(時間欄位名) = to_days(now());
昨天select * from 表名 where to_days( now( ) ) - to_days( 時間欄位名) <= 1
近7天select * from 表名 where date_sub(curdate(), interval
7day) <= date(時間欄位名)
近30天select * from 表名 where date_sub(curdate(), interval
30day) <= date(時間欄位名)
本月select * from 表名 where date_format( 時間欄位名, '%y%m' ) = date_format( curdate( ) , '%y%m' )
上一月select * from 表名 where period_diff( date_format( now( ) , '%y%m' ) , date_format( 時間欄位名, '%y%m' ) ) =1
查詢本季度資料select * from
`ht_invoice_information`
where quarter(create_date)=quarter(now());
查詢上個季度資料select * from
`ht_invoice_information`
where quarter(create_date)=quarter(date_sub(now(),interval
1 quarter));
查詢本年資料select * from
`ht_invoice_information`
where
year(create_date)=year(now());
查詢上年資料select * from
`ht_invoice_information`
where
year(create_date)=year(date_sub(now(),interval
1year));
查詢當前這周資料select name,submittime from enterprise where yearweek(date_format(submittime,'%y-%m-%d')) = yearweek(now());
查詢上週資料select name,submittime from enterprise where yearweek(date_format(submittime,'%y-%m-%d')) = yearweek(now())-1;
查詢上個月資料select name,submittime from enterprise where date_format(submittime,'%y-%m')=date_format(date_sub(curdate(), interval
1month),'%y-%m')
select * from
user
where date_format(pudate,'%y%m') = date_format(curdate(),'%y%m') ;
select * from
user
where weekofyear(from_unixtime(pudate,'%y-%m-%d')) = weekofyear(now())
select * from
user
where
month(from_unixtime(pudate,'%y-%m-%d')) = month(now())
select * from
user
where
year(from_unixtime(pudate,'%y-%m-%d')) = year(now()) and
month(from_unixtime(pudate,'%y-%m-%d')) = month(now())
select * from
user
where pudate between 上月最後一天 and 下月第一天
查詢當前月份資料select name,submittime from enterprise where date_format(submittime,'%y-%m')=date_format(now(),'%y-%m')
查詢距離當前6個月的資料select name,submittime from enterprise where submittime between date_sub(now(),interval
6month) and now();
mysql 查詢當天 本週 本月 的記錄
今天 select from 表名 where to days 時間欄位名 to days now 昨天 select from 表名 where to days now to days 時間欄位名 1近7天 select from 表名 where date sub curdate interva...
mySql 查詢當天 本週 本月等語句
當天 select from 表名 where date format e.createtime,y m d date format curdate y m d 昨日 select column name s from 表名 where date format 時間字段,y m d date for...
Sql 查詢當天 本週 本月記錄
sql powered by chenjiazi 查詢當天 select from info where datediff dd,datetime,getdate 0 查詢24小時內的 select from info where datediff hh,datetime,getdate 24 in...