Home >Database >Mysql Tutorial >How Can I Effectively Retrieve Rows with NULL Values in MySQL?
In MySQL, it can be challenging to retrieve rows containing null values using the standard comparison operator (=). This anomaly stems from the unique nature of the null value in SQL.
Let's illustrate this with an example:
SELECT pid FROM planets WHERE userid = NULL
This query is expected to return rows where the userid column has the NULL value. However, in many cases, it returns an empty set, despite the data being present in the table (as verified via phpMyAdmin).
The issue arises because null in SQL is distinct from other values. According to one of Codd's 12 rules, null cannot be equal to any other value, including itself. Consequently, using the = operator to compare a column to NULL will always return false.
To successfully retrieve rows with null values, we must use the IS NULL operator:
SELECT pid FROM planets WHERE userid IS NULL
By using IS NULL, we examine whether the column is truly null rather than attempting to compare it to NULL using the = operator.
However, it is essential to exhaust other options before resorting to modifying the table structure or engine.
The above is the detailed content of How Can I Effectively Retrieve Rows with NULL Values in MySQL?. For more information, please follow other related articles on the PHP Chinese website!