我想计算特定字段 = 'Q1' 的行数。
我通常使用 count(particular_field),但这不允许我仅在该字段 = 'Q1' 时才计数。
查询 SUM(particular_field = 'Q1')
是否适用于此问题?或者我可以执行 count(particular_field = 'Q1')
吗?
最佳答案
你可以做
select count(*)
from table
where particular_field = 'Q1'
或
select sum(case when particular_field ='Q1' then 1 else 0 end)
from table
关于mysql - 使用 sum() 模拟行的 count(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12043436/