MySQL按日期查詢資料SQL語句

2021-09-26 09:14:57 字數 1351 閱讀 8552

查詢往前7天的資料:

select * from 資料表  where  date_sub(curdate(), interval 7 day) <=  你要判斷的時間欄位名
查詢往前30天的資料:

select * from 資料表  where  date_sub(curdate(), interval 30 day) <=  你要判斷的時間欄位名
查詢在某段日期之間的資料:

select * from 資料表  where 時間欄位名 between '2016-02-01' and '2016-02-05'
查詢往前3個月的資料:

select * from 資料表  where 時間欄位名 between date_sub(now(),interval 3 month) and now()
查詢往前一年的資料:

select * from 資料表  where 時間欄位名 between date_sub(now(),interval 1 year) and now()
查詢本月的資料:

select * from 資料表 where date_format(時間欄位名,'%y-%m')=date_format(now(),'%y-%m')
查詢上月的資料:

select * from 資料表 where date_format(時間欄位名,'%y-%m')=date_format(date_sub(curdate(), interval 1 month),'%y-%m')
查詢本週的資料:

select * from 資料表 where yearweek(date_format(時間欄位名,'%y-%m-%d')) = yearweek(now())
查詢上週資料:

select * from 資料表 where yearweek(date_format(create_time,'%y-%m-%d')) = yearweek(now())-1
mysql 獲取當前時間加上乙個月

update user set leverstart=now(),leverover=date_add(now(), interval 1 month) where id=1;

#date_add() 增加

#date_sub()減少

#month 月份

#minute 分鐘

#second 秒

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

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

sql按日期查詢

查詢當天 1 select from shoporder where datediff day,ordtime,getdate 1 0 查詢當天 2 select from info where datediff dd,datetime,getdate 0 前30天 select from a wh...

sql中按日期查詢

查詢當天 1 select from shoporder where datediff day,ordtime,getdate 1 0 查詢當天 2 select from info where datediff dd,datetime,getdate 0 前30天 select from a wh...