这是我当前正在处理的查询:
SELECT COUNT(approved) AS 'approve'
, COUNT(approved) AS 'deny'
, COUNT(approved) AS 'unset'
FROM `approval`
WHERE 'approve' = 'Approved'
AND 'deny' = 'Denied'
AND 'unset' = 'Unset
我希望计数记录枚举approved=approved、Denied和Unset的所有时间
最佳答案
在mysql中,有一种很好的方法来完成所谓的apivot:
SELECT
SUM(approved = 'Approved') approve,
SUM(approved = 'Denied') deny,
SUM(approved = 'Unset') unset
FROM approval
WHERE approved in ('Approved', 'Denied', 'Unset')
这是因为在mysql中,true是1,false是0,所以求和条件将计算条件为true的次数。
where子句是可选的。
关于mysql - 选择条件有效的某些列的计数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26198925/