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

2021-10-14 18:46:04 字數 3183 閱讀 2519

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

month

('2021-01-14 18:31:30'

)as 月;

day()獲取時間的日

select

day(

'2020-04-30 17:19:19'

)as 日;

5.查詢統計 「每年」 的訂單數和訂單總金額(createat在資料庫為時間戳)

統計每年的訂單總數量(createat在資料庫為時間戳)

select

year

(from_unixtime(createat)

) 年,

count(*

)from

`sl_order`

where

1group

byyear

(from_unixtime(createat)

);

統計每年的訂單總金額(createat在資料庫為時間戳)

select

year

(from_unixtime(createat)

) 年,

sum(price)

from

`sl_order`

where

1group

byyear

(from_unixtime(createat)

);

6.查詢統計 「每月」 的訂單數和訂單總金額(createat在資料庫為時間戳)

統計每月的訂單總數量(createat在資料庫為時間戳)

select

year

(from_unixtime(createat)

) 年,

month

(from_unixtime(createat)

) 月,

count(*

)from

`sl_order`

where

1group

byyear

(from_unixtime(createat)),

month

(from_unixtime(createat)

);

統計每月的訂單總金額(createat在資料庫為時間戳)

select

year

(from_unixtime(createat)

) 年,

month

(from_unixtime(createat)

) 月,

sum(price)

from

`sl_order`

where

1group

byyear

(from_unixtime(createat)),

month

(from_unixtime(createat)

);

6.查詢統計 「每日」 的訂單數和訂單總金額(createat在資料庫為時間戳)

統計每日的訂單總數量(createat在資料庫為時間戳)

select

year

(from_unixtime(createat)

) 年,

month

(from_unixtime(createat)

) 月,

day(from_unixtime(createat)

) 日,

count(*

)from

`order

`where

1group

byyear

(from_unixtime(createat)),

month

(from_unixtime(createat)),

day(from_unixtime(createat)

);

統計每日的訂單總金額(createat在資料庫為時間戳)

select

year

(from_unixtime(createat)

) 年,

month

(from_unixtime(createat)

) 月,

day(from_unixtime(createat)

) 日,

sum(price)

from

`order

`where

1group

byyear

(from_unixtime(createat)),

month

(from_unixtime(createat)),

day(from_unixtime(createat)

);

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

mysql函式 1.from unixtime 函式時間戳轉換時間 select from unixtime 1588238359 as 時間 2.year 獲取時間的年份 select year 2020 04 30 17 19 19 as 年 3.month 獲取時間的月份 select mon...

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