SQL's NULL values are confusing – understanding how they work
A tricky one that catches me from time to time is the effect of a null in a subquery that you're doing "not in" operator on:
select a.* from A a where a.f1 not in (select b.f2 from B b where ...)
If the query for B produces any nulls, you'll always get no results from the query for A, regardless of the other content in A and B. That's because the query engine can't be sure any particular value is not in the list, because any null in the subquery is treated like an unknown that could stand for anything. So it returns null or false at each check of the not-in condition, and a null final result in a where clause won't have a record making it into the results, only positively true values would.
More on reddit.comReplace nulls values in sql using select statement in mysql? - Stack Overflow
What is the best way to handle null in the aggregation function with the calculations?
why am I having null in a sum in SQL?
Videos
You have a lot of options for substituting NULL values in MySQL:
CASE
select case
when fieldname is null then '123'
else fieldname end as fieldname
from tablename
COALESCE
select coalesce(fieldname, '123') as fieldname
from tablename
IFNULL
select ifnull(fieldname, '123') as fieldname
from tablename
There is a statement called IFNULL, which takes all the input values and returns the first non NULL value.
example:
select IFNULL(column, 1) FROM table;
http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_ifnull