時間的查詢,比如本月,本年,本季度(mysql)

2021-09-29 10:16:59 字數 2546 閱讀 3918

-- mysql查詢本季度

-- 今天

select * from ticket_order_detail where to_days(use_time) = to_days(now());

-- 7天

select *from ticket_order_detail where date_sub(curdate(), interval 7 day) <= date( use_time)

-- 近30天

select *from ticket_order_detail where date_sub(curdate(), interval 30 day) <= date( use_time)

-- 本月

select *from ticket_order_detail where date_format( use_time, '%y%m' ) = date_format( curdate( ) , '%y%m' )

-- 上一月

select *from ticket_order_detail where period_diff( date_format( now( ) , '%y%m' ) , date_format( use_time, '%y%m' ) ) =1

#查詢本季度資料

select * from `ticket_order_detail` where quarter(use_time)=quarter(now());

#查詢上季度資料

select * from `ticket_order_detail` where quarter(use_time)=quarter(date_sub(now(),interval 1 quarter));

#查詢本年資料

select * from `ticket_order_detail` where year(use_time)=year(now());

#查詢上年資料

select * from `ticket_order_detail` where year(use_time)=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`wheredate_format(pudate,『%y%m『)=date_format(curdate(),『%y%m『) ;

select * from user where weekofyear(from_unixtime(pudate,』%y-%m-%d』)) = weekofyear(now())

select*

fromuser

wheremonth(from_unixtime(pudate,『%y-%m-%d『))=month(now())

select*

from[user]

whereyear(from_unixtime(pudate,『%y-%m-%d『))=year(now())

andmonth(from_unixtime(pudate,『%y-%m-%d『))=month(now())

select*

from[user]

-- wherepudatebetween上月最後一天

-- 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

獲取 當前時間 本週,本月,本季度,本年的起始時間

因為專案中需要用時間作為條件進行統計,所以需要獲取 當前時間 本週,本月,本季度,本年的起始時間,如下 1 region 獲取 本週 本月 本季度 本年 的開始時間或結束時間 2 3 統計時間型別 4 5public enum enumtimetype612 13 獲取結束時間 14 15 week...

MySql查詢當天 本週 本月 本季度 本年的資料

select from 表名 where to days 時間欄位名 to days now select from 表名 where to days now to days 時間欄位名 1 select from 表名 where yearweek date format 時間欄位名,y m d ...

MySql查詢當天 本週 本月 本季度 本年的資料

一 建立測試資料 二 sql查詢及示例 1.今天 select from 表名 where to days 時間欄位名 to days now select from demo s where to days s.create time to days now 2.昨天 select from 表名...