MySQL查詢 每年 每月 每日 訂單數和訂單金額

2021-10-06 22:33:59 字數 4260 閱讀 6179

mysql函式

1. from_unixtime()函式時間戳轉換時間

select from_unixtime(1588238359) as 時間;

2. year()獲取時間的年份

select year('2020-04-30 17:19:19') as 年;

3. month()獲取時間的月份

select month('2020-04-30 17:19:19') as 月;

4. day()獲取時間的日

select day('2020-04-30 17:19:19') as 日;

下面查詢統計 "每年" 的訂單數和訂單總金額(createtime在資料庫為時間戳)

-- 下面查詢統計每年的訂單(createtime在資料庫為時間戳)

-- 訂單數量

select year(from_unixtime(createtime)) 年,count(*) from `order` where 1 group by year(from_unixtime(createtime));

-- 總金額

select year(from_unixtime(createtime)) 年,sum(price) from `order` where 1 group by year(from_unixtime(createtime));

下面查詢統計 "每月" 的訂單數和訂單總金額(createtime在資料庫為時間戳)

-- 下面查詢統計每月的訂單(createtime在資料庫為時間戳)

-- 訂單數量

select year(from_unixtime(createtime)) 年,month(from_unixtime(createtime)) 月,count(*) from `order` where 1 group by year(from_unixtime(createtime)),month(from_unixtime(createtime));

-- 總金額

select year(from_unixtime(createtime)) 年,month(from_unixtime(createtime)) 月,sum(price) from `order` where 1 group by year(from_unixtime(createtime)),month(from_unixtime(createtime));

下面查詢統計 "每日" 的訂單數和訂單總金額(createtime在資料庫為時間戳)

-- 下面查詢統計每日的訂單(createtime在資料庫為時間戳)

-- 訂單數量

select year(from_unixtime(createtime)) 年,month(from_unixtime(createtime)) 月,day(from_unixtime(createtime)) 日,count(*) from `order` where 1 group by year(from_unixtime(createtime)),month(from_unixtime(createtime)),day(from_unixtime(createtime));

-- 總金額

select year(from_unixtime(createtime)) 年,month(from_unixtime(createtime)) 月,day(from_unixtime(createtime)) 日,sum(price) from `order` where 1 group by year(from_unixtime(createtime)),month(from_unixtime(createtime)),day(from_unixtime(createtime));

例1:統計每月銷售總金額說明:欄位createtime 當前系統時間格式為yyyy-mm-dd hh:mm:ss;

字段 price 銷售額;

case month(createtime) when '8' then price else 0 end:獲取欄位createtime時間的月份,當月份為8月,獲取8月所有的銷售額,若沒有銷售額銷售額則為0;

sum(case month(createtime) when '8' then price else 0 end):將8月所有的銷售額進行合計;

ifnull(sum(case month(createtime) when '8' then price else 0 end), 0) as 八月份  :類似三元運算子,ifnull(當月總銷售額,0),假如銷售額不為 null,則 ifnull() 的返回值為當月總銷售額,否則為0,as別名為'八月份';

#:要查詢的年份;

select 

ifnull(sum(case month(createtime) when '1' then price else 0 end), 0) as 一月份,

ifnull(sum(case month(createtime) when '2' then price else 0 end), 0) as 二月份,

ifnull(sum(case month(createtime) when '3' then price else 0 end), 0) as 三月份,

ifnull(sum(case month(createtime) when '4' then price else 0 end), 0) as 四月份,

ifnull(sum(case month(createtime) when '5' then price else 0 end), 0) as 五月份,

ifnull(sum(case month(createtime) when '6' then price else 0 end), 0) as 六月份,

ifnull(sum(case month(createtime) when '7' then price else 0 end), 0) as 七月份,

ifnull(sum(case month(createtime) when '8' then price else 0 end), 0) as 八月份,

ifnull(sum(case month(createtime) when '9' then price else 0 end), 0) as 九月份,

ifnull(sum(case month(createtime) when '10' then price else 0 end), 0) as 十月份,

ifnull(sum(case month(createtime) when '11' then price else 0 end), 0) as 十一月份,

ifnull(sum(case month(createtime) when '12' then price else 0 end), 0) as 十二月份

from `order`

where year(createtime)=#;

當年每月銷售總額結果為:

例2:統計每月銷售總金額

-- 統計每月銷售總金額

select

sum(price),

ctime

from

(select

oid,

price,

date_format(createtime, '%y-%c') ctime

from

`order`) as o

group by ctime ;

當年每月銷售總額結果為:

MySQL查詢 每年 每月 每日 訂單數和訂單金額

1.from unixtime 格式化mysql時間戳函式 select from unixtime 1610620290 y m d h i s as 時間 year 獲取時間的年份 select year 2021 01 14 18 31 30 as 年 month 獲取時間的月份 select...

Mysql查詢每天 每週 每月 每年的資料

查詢每天的資料 select count 1 as total,date format create time,y m d as time from op endor info group by date format create time,y m d 查詢每週的資料 select count 1...

mysql查詢每天每週每月每年的資料方法

查詢每天的資料 select count 1 as countnumber,date format createtime,y m d as datetime from testtable group by date format createtime,y m d 查詢每週的資料 select cou...