myslq常用日期相關函式

2021-10-08 13:39:48 字數 2411 閱讀 8777

今天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 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(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 下月第一天

查詢當前月份的資料

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

MySQL 中常用的日期相關函式

第二部分 日期 時間戳 字串互相轉換 語法 timestampdiff interval,datetime1,datetime2 結果 返回 時間2 時間1 的時間差,結果單位由 interval 引數給出。interval frac second 毫秒 低版本不支援,用second,再除於1000...

MySQL日期相關函式

1.獲取當前日期 curdate current date current date mysql select curdate mysql select curtime 另外獲取utc 全球吧標準時間,原先也被稱作格林威治標準時間或gmt 時間,本地時間 utc 時間 8 小時,相應的函式是 utc...

Oracle 日期函式相關

1 獲取當前日期 不要想著用now 那是mysql裡面用的,oracle需要使用sysdate select sysdate from dual2 格式轉化 字串 日期 select to date 2017 01 12 yyyy mm dd hh24 mi from dual日期 字串 selec...