Mysql學習筆記 12 常用日期時間運算

2021-08-20 18:18:01 字數 1788 閱讀 5585

今天

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');
近6個月

select * from 表名 where 時間字段 between date_sub(now(),interval 6 month) and now();

MySql 學習筆記《日期》

昨天 select date add curdate interval 1 day select date sub curdate interval 1day 今天 select curdate select current date 明天 select date add curdate inter...

mysql學習筆記 日期

1.獲取當前日期時間 select sysdate from dual select current date from dual 獲取時間戳 select current timestamp from dual 2,日期的擷取 獲取到當前日期時間所在的年份 select extract year ...

MySQL學習筆記12 變數

說明 系統變數由系統定義,不是使用者定義,屬於伺服器層面 注意 全域性變數需要新增global關鍵字,會話變數需要新增session關鍵字,如果不寫,缺省會話級別 1.1 全域性變數 作用域 針對於所有會話 連線 有效,但不能跨重啟 檢視所有全域性變數 show global variables 檢...