$SQL = "SELECT pc.dist_id, pc.route, pc.types,
COUNT( pc.types = '$type' ) total,
COUNT( pc.types = '$type' AND pc.options = '1') callbacks
FROM per_call pc
WHERE pc.types = '$type'
GROUP BY pc.dist_id, pc.route, pc.types
ORDER BY pc.dist_id, pc.route";
简而言之,我想我是在问 COUNT 如何与 () 内的多个列一起使用。
最佳答案
Count
也可以工作:
SELECT pc.dist_id, pc.route, pc.types,
COUNT( CASE WHEN pc.types = '$type' THEN 1 END) total,
COUNT( CASE WHEN pc.types = '$type' AND pc.options = '1' THEN 1 END) callbacks
FROM per_call pc
WHERE pc.types = '$type'
GROUP BY pc.dist_id, pc.route, pc.types
ORDER BY pc.dist_id, pc.route
注意:它之所以有效,是因为 COUNT
返回非 NULL 值的数量,而 CASE
具有默认的 ELSE NULL
...
关于php - 为什么这两个 Count 返回相同的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13994911/