mysql時間查詢 MySQL按時間查詢

2021-10-25 22:26:38 字數 2032 閱讀 8833

mysql:

今天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(create_date)=quarter(now());

查詢上季度資料

select * from 表名 where quarter(create_date)=quarter(date_sub(now(),interval 1 quarter));

查詢本年資料

select * from 表名 where year(create_date)=year(now());

查詢上年資料

select * from 表名 where year(create_date)=year(date_sub(now(),interval 1 year));

查詢當前這週的資料

select name,submittime from 表名 where yearweek(date_format(submittime,'%y-%m-%d')) = yearweek(now());

查詢上週的資料

select name,submittime from 表名 where yearweek(date_format(submittime,'%y-%m-%d')) = yearweek(now())-1;

查詢上個月的資料

select name,submittime from 表名 where date_format(submittime,'%y-%m')=date_format(date_sub(curdate(), interval 1 month),'%y-%m')

select * from 表名 where date_format(pudate,'%y%m') = date_format(curdate(),'%y%m') ;

select * from 表名 where weekofyear(from_unixtime(pudate,'%y-%m-%d')) = weekofyear(now())

select * from 表名 where month(from_unixtime(pudate,'%y-%m-%d')) = month(now())

select * from 表名 where year(from_unixtime(pudate,'%y-%m-%d')) = year(now()) and month(from_unixtime(pudate,'%y-%m-%d')) = month(now())

select * from 表名 where pudate between 上月最後一天 and 下月第一天

查詢當前月份的資料

select name,submittime from 表名 where date_format(submittime,'%y-%m')=date_format(now(),'%y-%m')

查詢距離當前現在6個月的資料

select name,submittime from 表名 where submittime between date_sub(now(),interval 6 month) and now()

PHP處理,mysql按日期 時間查詢

date default timezone set prc 預設時區 echo 今天 date y m d time echo 今天 date y m d strtotime 18 june 2008 echo 昨天 date y m d strtotime 1 day echo 明天 date y...

mysql按日期查詢資料 mysql按日期查詢資料

問題 mysql按日期查詢乙個月內的資料,查詢返回時結果集中只顯示有資料的結果 需求 查詢結果中假如當天沒有資料,自動填零並輸出 事件描述 sql語句 select date format date added,m.d as day,count product id as total from ht...

位 mysql 查詢 Mysql 查詢 按位運算

前言 雖說這是件小事兒,但本寶寶思前想後,還是為它留下一筆,嘿嘿。反正寫部落格不浪費紙和筆!好久沒有開啟我的逗比模式了,我親愛的乖徒弟dba,dbb,dbaa等,好久不見你們,遙祝幸福快樂 db。整個事情其實使這樣的,最近的專案中,有乙個表,最終是這樣的 一共 位,每一位的取值是 和 額,後來還有 ...