是否可以更改此查詢,以便我可以像COUNT(WHERE type_id = 2) AS success, COUNT(WHERE type_id = 1) AS error
一樣來防止使用子查詢?MySQL優化 - 防止子查詢
SELECT
(SELECT COUNT(id) FROM log AS success_log WHERE type_id = 2 AND site_id = site.id AND DAYOFYEAR(success_log.created)) AS success,
(SELECT COUNT(id) FROM log AS success_log WHERE type_id = 1 AND site_id = site.id AND DAYOFYEAR(success_log.created)) AS error,
(SELECT COUNT(id) FROM log AS success_log WHERE site_id = site.id AND DAYOFYEAR(success_log.created)) AS total,
DATE_FORMAT(log.created, "%m-%d-%y") AS `day`
FROM log
WHERE site_id = ?
GROUP BY DAYOFYEAR(created)`
看看sum(if ...或sum(case when when syntax) –