如何在sql查询中使用Count条件

时间:2016-05-06 10:07:41

标签: mysql

如何在sql查询中使用count条件。这是我的查询

SELECT COUNT(test='maths') FROM homework where id = 1;

1 个答案:

答案 0 :(得分:4)

在MySQL条件中,10为结果。因此,请改用sum()

SELECT sum(test='maths') FROM homework where id = 1;

count()只计算非空值。所以你也可以用count()来做,但是这样

SELECT count(case when test='maths' then 1 else null end) 
FROM homework 
where id = 1;
相关问题