mysql 中對time欄位的處理

2021-10-14 17:11:02 字數 2381 閱讀 2083

1、利用to_days函式查詢今天的資料: select * from 表名 where to_days(時間欄位名) = to_days(now()); to_days函式:返回從2023年(公元1年)至當前日期的總天數。

2、昨天 select * from 表名 where to_days( now( ) ) – to_days( 時間欄位名) <= 1

3.7天 select * from 表名 where date_sub(curdate(), interval 7 day) <= date(時間欄位名)

4.近30天 select * from 表名 where date_sub(curdate(), interval 30 day) <= date(時間欄位名)

5.本月 select * from 表名 where date_format( 時間欄位名, 『%y%m' ) = date_format( curdate( ) , 『%y%m' )

6.上一月 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 對Explain結果中各字段的解釋

explain作為常用的sql語句優化工具,可根據其結果對sql語句進行分析優化。explain的作用 方便我們對sql語句的優化 explain的使用方式 explain sql語句 explain的結果 id select type table type possible keys key ke...

mysql 字段操作 mysql如何對欄位進行操作

mysql如何對欄位進行操作 閱讀 104 1 mysql修改欄位的預設值 alter table tb mer team column drop constraint df tb mer team column columnorder alter table tb mer team column ...

mysql 列舉字段 MySQL欄位中的列舉

mysql欄位相信大家都有一些了解,下面將為您介紹的是mysql欄位中的列舉,希望對您學習mysql欄位方面能夠有所幫助。mysql欄位中的列舉 mysql create table meiju f1 enum 1 2 3 4 5 6 query ok,0 rows affected 0.92 se...