MySql按條件統計數量
有如下裝置資訊表:
要求按status統計數量,查詢結果返回一條資料,如下圖所示:
可是使用如下SQL語句:
SELECT
COUNT(*) AS total,
SUM(IF(status = 1, 1, 0)) AS status1,
SUM(IF(status = 2, 1, 0)) AS status2,
SUM(IF(status = 3, 1, 0)) AS status3,
SUM(IF(status = 4, 1, 0)) AS status4
FROM
dev_device_info