使用IFNULL将NULL设置为零

时间:2011-07-25 03:06:07

标签: mysql sql ifnull

我有一个表格,其中一个字段包含一个整数或NULL

parent_id
2
4
6
NULL
NULL
45
2

我如何添加IFNULL语句,以便ans_count填充0而不是NULL

这是我的SQL代码:

...
(SELECT parent_id AS pid, COUNT(*) AS ans_count
   FROM qa
  GROUP BY parent_id) AS n

更新

下面的完整SQL - 感谢所有人的耐心等待。

SELECT  *
FROM    qa
        JOIN user_profiles
          ON user_id = author_id
        LEFT JOIN (SELECT cm_id,
                          cm_author_id,
                          id_fk,
                          cm_text,
                          cm_timestamp,
                          first_name AS cm_first_name,
                          last_name AS cm_last_name,
                          facebook_id AS cm_fb_id,
                          picture AS cm_picture
                    FROM  cm
                    JOIN  user_profiles
                      ON  user_id = cm_author_id) AS c
          ON id = c.id_fk
        LEFT JOIN (SELECT   parent_id AS pid, COUNT(*) AS ans_count
                     FROM   qa
                    GROUP   BY parent_id) AS n
          ON id = n.pid
WHERE   id  LIKE '%'
ORDER   BY id DESC

6 个答案:

答案 0 :(得分:13)

编辑:基于完整查询的新信息

在您指定的查询中计数可以为null的原因是因为左连接将在不匹配的记录上返回空值。因此子查询本身不会返回空计数(因此所有响应和混淆)。您需要在最外面的选择中指定IFNULL,如下所示:

SELECT  qa.*, user_profiles.*, c.*, n.pid, ifnull(n.ans_count, 0) as ans_count
FROM    qa
        JOIN user_profiles
          ON user_id = author_id
        LEFT JOIN (SELECT cm_id,
                          cm_author_id,
                          id_fk,
                          cm_text,
                          cm_timestamp,
                          first_name AS cm_first_name,
                          last_name AS cm_last_name,
                          facebook_id AS cm_fb_id,
                          picture AS cm_picture
                    FROM  cm
                    JOIN  user_profiles
                      ON  user_id = cm_author_id) AS c
          ON id = c.id_fk
        LEFT JOIN (SELECT   parent_id AS pid, COUNT(*) AS ans_count
                     FROM   qa
                    GROUP   BY parent_id) AS n
          ON id = n.pid
WHERE   id  LIKE '%'
ORDER   BY id DESC

旧回复

您能更详细地解释一下您所看到的以及您希望看到的内容吗? Count不能返回NULL。

运行这组查询,您将看到计数始终为2.您可以更改NULL parent_ids的显示方式(为NULL或0),但计数本身将始终返回。

create temporary table if not exists SO_Test(
    parent_id int null);

insert into SO_Test(parent_id)
select 2 union all select 4 union all select 6 union all select null union all select null union all select 45 union all select 2;


SELECT IFNULL(parent_id, 0) AS pid, COUNT(*) AS ans_count
   FROM SO_Test
  GROUP BY IFNULL(parent_id, 0);

SELECT parent_id AS pid, COUNT(*) AS ans_count
   FROM SO_Test
  GROUP BY parent_id;

drop table SO_Test;

答案 1 :(得分:2)

我没有测试过这个,但我认为它会起作用

(SELECT IF( parent_id IS NULL, 0, parent_id) AS pid, COUNT(*) AS ans_count
   FROM qa
  GROUP BY parent_id) AS n

答案 2 :(得分:1)

只需将其包装在您的陈述中:

IFNULL( 
  (SELECT parent_id AS pid, COUNT(*) AS ans_count
   FROM qa
   GROUP BY parent_id)
 , 0
) AS n

答案 3 :(得分:1)

您是否可以发布展示您所谈论行为的实际数据和完整查询?根据我的经验,COUNT(*)永远不会为NULL。

Can Count(*) ever return null?

Does COUNT(*) always return a result?

答案 4 :(得分:1)

您是否尝试过仅计算parent_id的数量?

(SELECT parent_id AS pid, COUNT(parent_id) AS ans_count
   FROM qa
  GROUP BY parent_id)

答案 5 :(得分:1)

SELECT IFNULL(parent_id, 0) AS pid, COUNT(IFNULL(parent_id, 0)) AS ans_count
FROM qa
GROUP BY IFNULL(parent_id, 0)