NULL
The value is missing unknown data, and by default, the column of the table can hold NULL
the value.
This chapter explains the IS NULL
and IS NOT NULL
operators.
If a column in a table is optional, then we can insert new records or update existing records without adding a value to that column. This means the field will be NULL
saved with the value.
NULL
Values are treated differently than other values.
NULL
Used as a placeholder for unknown or inapplicable values.
Note : and 0 cannot be compared NULL
; they are not equivalent.
So how do we test NULL
the value? The value cannot be tested using comparison operators NULL
such as =
, <
, or <>
.
We have to use the IS NULL
and IS NOT NULL
operator.
How do we select only records with NULL values in the column? We have to use the IS NULL operator:
SELECT LastName,FirstName,Address FROM Persons
WHERE Address IS NULL
How do we select records that do not have NULL values in the "Address" column? We have to use the IS NOT NULL operator:
SELECT LastName,FirstName,Address FROM Persons
WHERE Address IS NOT NULL
IFNULL() function
In MySQL
, we can use IFNULL()
the function, like this:
Below, if "UnitsOnOrder" is NULL
, it does not contribute to the calculation, so returns 0 NULL
if the value is .IFNULL()
SELECT ProductName,UnitPrice*(UnitsInStock+IFNULL(UnitsOnOrder,0))
FROM Products