MySql日期查詢語句詳解

2021-07-30 11:19:46 字數 3052 閱讀 6322

使用date_format方法

select * from `ler_items` where date_format(posttime,'%y-%m')='2013-03'

注意:日期一定要用'',否則沒有效果

其它的一些關於mysql日期查詢語句

mysql> select date_format(date_sub(curdate(), interval 7 day),'%y%m%d');

+———————————————————–+

| date_format(date_sub(curdate(), interval 7 day),'%y%m%d') |

+———————————————————–+

| 120416 |

+———————————————————–+

1 row in set (0.00 sec)

可以獲取7天前的日期,

使用date_sub(curdate(), interval 7 day) 來獲得7天前的時間,用date_format 來指定輸出格式。

今天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(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日期查詢語句詳解

使用date format方法 select from ler items where date format posttime,y m 2013 03 注意 日期一定要用 否則沒有效果 其它的一些關於mysql日期查詢語句 mysql select date format date sub cur...

MySQL日期相關查詢語句

日期字串轉時間戳,10位,資料庫儲存的是13位,記得除以1000 select unix timestamp 2019 09 01 00 00 00 時間戳轉日期 select from unixtime 1567267200,y m d h i s 查詢當天,格式為yyyy mm dd hh mm...

mysql日期查詢 mysql 查詢日期

檢視本月資料 select from content publish where date format publish time,y m date format date sub curdate interval 0 month y m 檢視上個月資料 select from content pu...