Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>EDIT: NEW INFO BASED ON FULL QUERY</strong></p> <p>The reason the counts can be null in the query you specify is because a left join will return nulls on unmatched records. So the subquery itself is not returning null counts (hence all the responses and confusion). You need to specify the IFNULL in the outer-most select, as follows:</p> <pre><code>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 </code></pre> <p><strong>OLD RESPONSE</strong></p> <p>Can you explain in more detail what you are seeing and what you expect to see? Count can't return NULLs.</p> <p>Run this set of queries and you'll see that the counts are always 2. You can change the way the NULL parent_ids are displayed (as NULL or 0), but the count itself will always return.</p> <pre><code>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; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload