We have seen the SQL SELECT command using the WHERE clause to fetch data from the MySQL table. However, when we try to give the condition to compare the value of the field or column to NULL, it does not work correctly.
To handle this situation, MySQL provides three major operators
- is NULL: This operator returns True when the value of the column is null.
- is not NULL: operator returns True when the value of the column is not NULL.
- <=> operator Comparison value (unlike = operator) is ture, even if two null values
The terms involved are special. You cannot use =null or!=null to find a column with a NULL value. This comparison always tells them whether it is a real failure, because it is impossible. Even the null=null failed.
If you want to find columns that are or are not NULL, use is NULL or is not NULL.
to use a null value at a command prompt:
Suppose a table tcount_tbl, it contains two columns Stutorial_author and Tutorial_count, and one of the tutorial_count null-represented values is unknown
Example:
Try the following example:
root@host# mysql-u root-p password;
Enter password:******* mysql> Use tutorials; Database changed mysql> CREATE TABLE Tcount_tbl-> (-> tutorial_author-varchar () not NULL,-> Tutoria
L_count INT->); Query OK, 0 rows affected (0.05 sec) mysql> INSERT into Tcount_tbl-> (Tutorial_author, Tutorial_count) values (' m
Ahran ', 20);
Mysql> INSERT into Tcount_tbl-> (Tutorial_author, Tutorial_count) VALUES (' Mahnaz ', NULL);
Mysql> INSERT into Tcount_tbl-> (Tutorial_author, Tutorial_count) VALUES (' Jen ', NULL);
Mysql> INSERT into Tcount_tbl-> (Tutorial_author, Tutorial_count) VALUES (' Gill ', 20);
Mysql> SELECT * from TCOUNT_TBL; +-----------------+----------------+
| Tutorial_author |
Tutorial_count | +-----------------+----------------+
| Mahran | 20 | | Mahnaz | NULL | | Jen | NULL | | Gill |
20 |
+-----------------+----------------+ 4 rows in Set (0.00 sec) mysql>
You can see that = and!= do not use null values, as follows:
Mysql> SELECT * from tcount_tbl WHERE tutorial_count = NULL;
Empty Set (0.00 sec)
mysql> SELECT * from Tcount_tbl WHERE tutorial_count!= NULL;
Empty Set (0.01 sec)
To find a record where the Tutorial_count column is or is not NULL, the query should write this:
Mysql> SELECT * from Tcount_tbl
-> WHERE tutorial_count is NULL;
+-----------------+----------------+
| tutorial_author | Tutorial_count
| +-----------------+----------------+
| Mahnaz | NULL |
| Jen | NULL |
+-----------------+----------------+
2 rows in Set (0.00 sec)
mysql> SELECT * from Tcount_tbl
-> WHERE Tutorial_count is not NULL;
+-----------------+----------------+
| tutorial_author | Tutorial_count
| +-----------------+----------------+
| Mahran | |
| Gill | |
+-----------------+----------------+
2 rows in Set (0.00 sec)
PHP script handles Null values:
You can use the If ... Else a query that operates on the basis of Null values based on conditional preparation.
Example:
The following example tutorial_count from the outside, and then it is compared to the values that can be in the table.
<?php
$dbhost = ' localhost:3036 ';
$dbuser = ' root ';
$dbpass = ' Rootpassword ';
$conn = mysql_connect ($dbhost, $dbuser, $dbpass);
if (! $conn)
{
die (' Could not connect: '. Mysql_error ());
}
if (Isset ($tutorial _count))
{
$sql = ' SELECT tutorial_author, tutorial_count from
tcount_tbl
WHERE tutorial_count = $tutorial _count ';
}
else
{
$sql = ' SELECT tutorial_author, tutorial_count from
tcount_tbl
WHERE Tutorial_count is $ Tutorial_count ';
}
mysql_select_db (' tutorials ');
$retval = mysql_query ($sql, $conn);
if (! $retval)
{
die (' could not get data: '. mysql_error ());
}
while ($row = Mysql_fetch_array ($retval, MYSQL_ASSOC))
{
echo author:{$row [' Tutorial_author ']} <br> ".
" Count: {$row [' Tutorial_count ']} <br> '.
' --------------------------------<br> ";
}
echo "fetched data successfully\n";
Mysql_close ($conn);
? >