mysql 日期操作

2021-10-05 21:07:46 字數 2748 閱讀 6207

--今天

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 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 year( 時間欄位名 ) = year( now( ) )

--上一月

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 1 year));

--查詢當前這週的資料

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 6 month) and now();

--查詢上個月的資料

select name,submittime from enterprise where date_format(submittime,'%y-%m')=date_format(date_sub(curdate(), interval 1 month),'%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 下月第一天

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 1

limit 0 , 30

mysql日期操作

一 獲取當前日期時間 1.1 獲得當前日期 時間 date time 函式 now 1.2 獲取當前日期 時間 date time 函式 sysdate 注 二者類擬,不同在於now 在執行時就得到了,sysdate 執行時動態得到。2 獲取當前日期 curdate 與current date 等同...

mysql time 操作 MySql 日期操作

1 獲取日期的函式 select now 獲取當前日期與時間 同義詞 localtime localtimestamp current timestamp select curdate 獲取當前日期 select sysdate 動態獲取當前日期與時間 select curtime 獲取當前時間 2...

mysql日期操作 DATE FORMAT

y m d h i s date format date,format 根據format字串格式化date值。下列修飾符可以被用在format字串中 m 月名字 january december w 星期名字 sunday saturday d 有英語字首的月份的日期 1st,2nd,3rd,等等。...