1. 程式人生 > >mysql查詢

mysql查詢

總量 sum reat 金額 必須 group by 數量 表達式 app

例子:

SELECT
FROM_UNIXTIME (created, "%Y-%m-%d") as created_date,
count(*) as `all`,
sum(
if(order_status = 1, 1, 0)
) as new,
sum(
if(order_status = 2, 1, 0)
) as approved,
sum(
if (order_status = 3, 1, 0)
) as picked
FROM
sale_order
group by
created_date

講解:可以把if()裏面的看成三元表達式

正確的選擇第二個統計 不正確統計第三個

只統計數量可以不寫後兩個參數,統計總量比如金額的必須寫

mysql查詢