MySQL之查詢指定時間的資料

2022-06-17 21:12:21 字數 2803 閱讀 6902

**

--

今天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 7

day) <=

date(時間欄位名)

--近30天

select

*from 表名 where date_sub(curdate(), interval 30

day) <=

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 表名 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

));--

查詢當前這週的資料

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(date_sub(curdate(), interval 1

month),'

%y-%m')

select

*from 表名 where date_format(時間欄位名,'

%y%m

') = date_format(curdate(),'

%y%m

') ;

select

*from 表名 where weekofyear(from_unixtime(時間欄位名,'

%y-%m-%d

')) =

weekofyear(now())

select

*from 表名 where

month(from_unixtime(時間欄位名,'

%y-%m-%d

')) =

month

(now())

select

*from 表名 where

year(from_unixtime(時間欄位名,'

%y-%m-%d

')) =

year(now()) and

month(from_unixtime(pudate,'

%y-%m-%d

')) =

month

(now())

select

*from 表名 where 時間欄位名 between 上月最後一天 and

下月第一天

--查詢當前月份的資料

select

*from 表名 where date_format(時間欄位名,'

%y-%m

')=date_format(now(),'

%y-%m')

--查詢距離當前現在6個月的資料

select

*from 表名 where 時間欄位名 between date_sub(now(),interval 6

month) and

now(); --

查詢某個月的資料(查詢18年10月份資料)

select

*from 表名 where date_format(時間欄位名,'

%y-%m

')='

2018-10

'select

*from 表名 where date_format(時間欄位名,'

%y-%m

')=date_format('

2018-10-05

','%y-%m

')

mysql 指定時間 MySQL查詢指定時間的資料

mysql查詢指定時間的資料 user event 使用者事件表 create time 表中儲存時間的字段 獲取當月資料 select from user event where date format create time,y m date format now y m 獲取3月份資料 sel...

查詢指定時間段的資料

一 select from searchdetails where searchdatetime between 2014 10 06 01 40 50.123 and 2014 12 07 23 12 and datepart hour,searchdatetime between 1 and 1...

恢復指定時間誤刪的資料

恢復指定時間誤刪的資料 如果刪除的資料要找回,只要不是truncate table,一般都可以閃回,只要快閃儲存器足夠,快閃儲存器不夠的話是查不了的。舉兩個例子 1.閃回三個小時前的資料 select from 表名 as of timestamp sysdate 3 24 where aae062...