Mysql按天分組(按日分組)

2021-09-09 05:54:35 字數 3679 閱讀 6760

select

date_format(date_created,"%y-%m-%d")as day,

sum(order_price) as sumtol

from

dn_sale

where sale_person_id=1

group by

date_format(date_created,"%y-%m-%d")

根據format字串格式化date值。下列修飾符可以被用在format字串中:

%m 月名字(january……december)

%w 星期名字(sunday……saturday)

%d 有英語字首的月份的日期(1st, 2nd, 3rd, 等等。)

%y 年, 數字, 4 位

%y 年, 數字, 2 位

%a 縮寫的星期名字(sun……sat)

%d 月份中的天數, 數字(00……31)

%e 月份中的天數, 數字(0……31)

%m 月, 數字(01……12)

%c 月, 數字(1……12)

%b 縮寫的月份名字(jan……dec)

%j 一年中的天數(001……366)

%h 小時(00……23)

%k 小時(0……23)

%h 小時(01……12)

%i 小時(01……12)

%l 小時(1……12)

%i 分鐘, 數字(00……59)

%r 時間,12 小時(hh:mm:ss [ap]m)

%t 時間,24 小時(hh:mm:ss)

%s 秒(00……59)

%s 秒(00……59)

%p am或pm

%w 乙個星期中的天數(0=sunday ……6=saturday )

%u 星期(0……52), 這裡星期天是星期的第一天

%u 星期(0……52), 這裡星期一是星期的第一天

%% 乙個文字「%」。

按條件求和

條件更新

update dn_product as p

set p.product_num = case

when p.id<=10 then 3

when 10<=p.id then 4

when 1030 then 5

end

按時間端查詢資料的方法

今天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 fromenterprise where yearweek(date_format(submittime,'%y-%m-%d')) = yearweek(now());

查詢上週的資料

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

查詢上個月的資料

select name,submittime fromenterprise 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 fromenterprise wheredate_format(submittime,'%y-%m')=date_format(now(),'%y-%m')

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

select name,submittime fromenterprise where submittime betweendate_sub(now(),interval 6 month) and now();

MySQL 按天分組統計

select date format c.consumecjtime y m d as day sum c.consumetotalprice as totalprice from consume as c where c.consumecjtime 2018 03 27 and c.consume...

mysql 按日期分組

select date format now y m d days,count caseid count from tc case group by days date format是可以把一些時間格式轉化為你所需的時間格式,now 是2015 09 05 12 33 33,然後變為20150905...

mysql 日期間 分組 mysql 按日期分組

select date format now y m d days,count caseid count from tc case group by days date format是可以把一些時間格式轉化為你所需的時間格式,now 是2015 09 05 12 33 33,然後變為20150905...