使用mysql的時間段查詢

2021-08-20 06:04:39 字數 2986 閱讀 8032

今天

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(now(),'%y-%m')
查詢距離當前現在6個月的資料

select name,submittime from enterprise where submittime between date_sub(now(),interval

6month) and now();

查詢上個月的資料

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())

andmonth (from_unixtime(pudate, ' %y-%m-%d ' )) = month (now())

select *

from [ user ]

where pudate between 上月最後一天

and 下月第一天

where

date(regdate) = curdate();

select * from test where

year(regdate)=year(now()) and

month(regdate)=month(now()) and

day(regdate)=day(now())

select

date( c_instime ) ,curdate( )

from

`t_score`

where

1limit 0 , 30

MySql時間段查詢語句

今天 select fullname,addedtime from t user where to days addedtime to days now 昨天 select fullname,addedtime from t user where to days now to days addedt...

mysql查詢時間段

閱讀需要 3 分鐘 內容 mysql 時間段查詢 一些時間的型別 及常用時間函式 日期型別 儲存空間 日期格式 日期範圍 datetime 8 bytes yyyy mm dd hh mm ss 1000 01 01 00 00 00 9999 12 31 23 59 59 timestamp 4 ...

mysql 查詢日期 MySQL查詢時間段

之前說過mysql的時間欄位有date,time,datetime,timestamp等,在實際查詢時間段時,我們經常將整個字段儲存進去,例如像2011 03 10 00 00 00,也有分開儲存年,月,日的方式,或者使用right 等字元擷取函式來進行查詢操作。anyway,我們都是要對一段時間進...