I have a table in which a field contains an integer or NULL
.
parent_id
2
4
6
NULL
NULL
45
2
How would I go about adding an IFNULL statement so that ans_count
will be populated with 0
instead of NULL
?
Here is my SQL code:
...
(SELECT parent_id AS pid, COUNT(*) AS ans_count
FROM qa
GROUP BY parent_id) AS n
UPDATE
Full SQL below - thanks to all for your patience.
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
parent_id
that contains NULLs, but later you are talking about eliminating NULLs for a different column,ans_count
. To confuse things more, your code snippet contains both. – Gerson