if count value is null set it to zero - sql select statement

count, mysql, sql

Solution

In the outer query, you can replace a `NULL` with a zero using the `IFNULL()` function, e.g.

SELECT ...
     , IFNULL(v.t1count,0) AS t1count
  FROM ...
  LEFT
  JOIN ( SELECT ... AS t1count
                ...
       ) v
    ON ...

The NULL you are getting returned by the outer query isn't from the inline view query. The NULL is a result of "no match" being found by the `LEFT [OUTER] JOIN` operation.

If you are referencing `v.t1count` in other expressions in the outer query, you can replace those references with `NULLIF(v.t1count,0)` as well.

Problem

Can't seem to get this working. If the count is null I want to set it to zero... else set it to the count. I am adding multiple counts in another part of my code so I cannot have null values when I do this. $table = " ... ``` LEFT JOIN (SELECT user_id, IF(count(user_id) = '(null)',0,count(user_id)) as t1count FROM screenshot_logs GROUP BY user_id) as t_screenshots on t_screenshots.user_id = users.user_id ... ``` ";

Original source

Related problems