mysql日期查詢 mysql 查詢日期

2021-10-25 14:07:58 字數 2968 閱讀 9309

//檢視本月資料

select

from

content_publish

where

date_format(publish_time, '%y %m') = date_format(date_sub(curdate(), interval 0 month),'%y %m')

//檢視上個月資料

select

from

content_publish

where

date_format(publish_time, '%y %m') = date_format(date_sub(curdate(), interval 1 month),'%y %m')

//查詢上上個月資料

select

from

content_publish

where

date_format(publish_time, '%y %m') = date_format(date_sub(curdate(), interval 2 month),'%y %m')

//查詢當前月份

select date_format(date_sub(curdate(), interval 0 month), '%m')

//查詢上個月月份

select date_format(date_sub(curdate(), interval 1 month), '%m')

//查詢上上個月月份

select date_format(date_sub(curdate(), interval 0 month), '%m')

今天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 日期查詢 Mysql日期查詢list

當前week的第一天 select date sub curdate interval weekday curdate 1 day 當前week的最後一天 select date sub curdate interval weekday curdate 5 day 前一week的第一天 select...

mysql關於日期 MySQL關於日期的查詢sql

查詢今天的記錄 select from wmacc.cash trade record where to days create date to days now select from wmacc.cash trade record where date format create date,y ...

MySQL 日期查詢

在mysql使用過程中,日期一般都是以datetime timestamp等格式進行儲存的,但有時會因為特殊的需求或歷史原因,日期的儲存格式是varchar,那麼我們該如何處理這個varchar格式的日期資料呢 時間欄位為greens data 型別為 varchar 1 第一種 函式str to ...