避免在mysql语句中计算NULL值

时间:2016-02-17 16:29:10

标签: php mysql

只想计算已设置的行并避免未设置的行

SELECT COUNT(DISTINCT member_id) AS total_memebers, 
  COUNT(case member_feesdue when null then 1 else 0 end) AS total_feesdue 
FROM sdg_members

2 个答案:

答案 0 :(得分:2)

COUNT永远不会计算NULL。 SELECT COUNT(col)FROM tbl就足够了。

答案 1 :(得分:0)

我认为你试图将total_feesdue计算在等于1的位置?

SELECT COUNT(DISTINCT member_id) AS total_memebers, 
  (SELECT COUNT(*) FROM sdg_members WHERE sdg_members = 1) AS total_feesdue 
FROM sdg_members