count(a.selected_he =“是”)可以吗?

时间:2011-12-05 15:05:59

标签: mysql sql

我可以这样问:

having count(a.selected_he = "yes") > 2

我想询问selected_he = yes的结果记录是否超过2。 但我不确定我是否可以在计数函数中写a.selected_he = "yes"

我是对的吗?

2 个答案:

答案 0 :(得分:4)

SELECT sum(CASE WHEN a.selected_he='yes' THEN 1 ELSE 0 END)>2 FROM ...

count计算非null s,而非true s。

答案 1 :(得分:1)

select id,count(*) as counts
from table
where selected_he = 'yes'
group by id
having counts > 2

如果where子句中的列不包含空值。