MySQL NULL value

NULLThe value is missing unknown data, and by default, the column of the table can hold NULLthe value.

This chapter explains the IS NULLand IS NOT NULLoperators.

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 NULLsaved with the value.

NULLValues ​​are treated differently than other values.

NULLUsed as a placeholder for unknown or inapplicable values.

Note : and 0 cannot be compared NULL; they are not equivalent.

So how do we test NULLthe value? The value cannot be tested using comparison operators NULLsuch as =, <, or <>.
We have to use the IS NULLand IS NOT NULLoperator.

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 NULLif the value is .IFNULL()

SELECT ProductName,UnitPrice*(UnitsInStock+IFNULL(UnitsOnOrder,0))
FROM Products

Guess you like

Origin blog.csdn.net/SSY_1992/article/details/130242306