15. Learn MySQL NULL value handling

 MySQL NULL value handling

We already know that MySQL uses the SQL SELECT command and the WHERE clause to read the data in the data table, but when the provided query condition field is NULL, the command may not work properly.

To handle this situation, MySQL provides three major operators:

  • IS NULL:  This operator returns true when the column value is NULL.
  • IS NOT NULL:  When the value of the column is not NULL, the operator returns true.
  • <=>:  The comparison operator (different from the = operator) returns true when the two values ​​being compared are equal or both are NULL.

Conditional comparison operations on NULL are special. You cannot use = NULL or != NULL to find NULL values ​​in a column.

In MySQL, a comparison of a NULL value with any other value (even NULL) always returns NULL, ie NULL = NULL returns NULL.

Handling NULL in MySQL uses the IS NULL and IS NOT NULL operators.

Notice:

select * , columnName1+ifnull(columnName2,0) from tableName;

columnName1 and columnName2 are int type. When there is a null value in columnName2, columnName1+columnName2=null, ifnull(columnName2,0) converts the null value in columnName2 to 0.


Use NULL values ​​in command prompt

In the following example, it is assumed that the table xxxxxx_test_tbl in the database XXXXXX contains two columns xxxxxx_author and xxxxxx_count, and NULL values ​​are set to be inserted in xxxxxx_count.

example

Try the following examples:

Create data table xxxxxx_test_tbl

root@host# mysql -u root -p password;
Enter password:*******
mysql> use XXXXXX;
Database changed
mysql> create table xxxxxx_test_tbl
    -> (
    -> xxxxxx_author varchar(40) NOT NULL,
    -> xxxxxx_count  INT
    -> );
Query OK, 0 rows affected (0.05 sec)
mysql> INSERT INTO xxxxxx_test_tbl (xxxxxx_author, xxxxxx_count) values ('XXXXXX', 20);
mysql> INSERT INTO xxxxxx_test_tbl (xxxxxx_author, xxxxxx_count) values ('菜鸟教程', NULL);
mysql> INSERT INTO xxxxxx_test_tbl (xxxxxx_author, xxxxxx_count) values ('Google', NULL);
mysql> INSERT INTO xxxxxx_test_tbl (xxxxxx_author, xxxxxx_count) values ('FK', 20);
 
mysql> SELECT * from xxxxxx_test_tbl;
+---------------+--------------+
| xxxxxx_author | xxxxxx_count |
+---------------+--------------+
| XXXXXX        | 20           |
| X教X程  | NULL         |
| Google        | NULL         |
| FK            | 20           |
+---------------+--------------+
4 rows in set (0.01 sec)

You can see in the following examples that the = and != operators do not work:

mysql> SELECT * FROM xxxxxx_test_tbl WHERE xxxxxx_count = NULL;
Empty set (0.00 sec)
mysql> SELECT * FROM xxxxxx_test_tbl WHERE xxxxxx_count != NULL;
Empty set (0.01 sec)

To find out whether the xxxxxx_test_tbl column in the data table is NULL,  IS NULL  and  IS NOT NULL must be used , as follows:

mysql> SELECT * FROM xxxxxx_test_tbl WHERE xxxxxx_count IS NULL;
+---------------+--------------+
| xxxxxx_author | xxxxxx_count |
+---------------+--------------+
| XX教程  | NULL         |
| Google        | NULL         |
+---------------+--------------+
2 rows in set (0.01 sec)
 
mysql> SELECT * from xxxxxx_test_tbl WHERE xxxxxx_count IS NOT NULL;
+---------------+--------------+
| xxxxxx_author | xxxxxx_count |
+---------------+--------------+
| XXXXXX        | 20           |
| FK            | 20           |
+---------------+--------------+
2 rows in set (0.01 sec)

Handling NULL values ​​with a PHP script

In a PHP script you can use if...else statements to handle whether a variable is empty and generate the corresponding conditional statement.

In the following example PHP sets the $xxxxxx_count variable and then uses that variable to compare with the xxxxxx_count field in the data table:

MySQL ORDER BY test:

<?php
$dbhost = 'localhost';  // mysql服务器主机地址
$dbuser = 'root';            // mysql用户名
$dbpass = '123456';          // mysql用户名密码
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
    die('连接失败: ' . mysqli_error($conn));
}
// 设置编码,防止中文乱码
mysqli_query($conn , "set names utf8");
 
if( isset($xxxxxx_count ))
{
   $sql = "SELECT xxxxxx_author, xxxxxx_count
           FROM  xxxxxx_test_tbl
           WHERE xxxxxx_count = $xxxxxx_count";
}
else
{
   $sql = "SELECT xxxxxx_author, xxxxxx_count
           FROM  xxxxxx_test_tbl
           WHERE xxxxxx_count IS NULL";
}
mysqli_select_db( $conn, 'XXXXXX' );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
    die('无法读取数据: ' . mysqli_error($conn));
}
echo '<h2>菜鸟教程 IS NULL 测试<h2>';
echo '<table border="1"><tr><td>作者</td><td>登陆次数</td></tr>';
while($row = mysqli_fetch_array($retval, MYSQL_ASSOC))
{
    echo "<tr>".
         "<td>{$row['xxxxxx_author']} </td> ".
         "<td>{$row['xxxxxx_count']} </td> ".
         "</tr>";
}
echo '</table>';
mysqli_close($conn);
?>

Guess you like

Origin blog.csdn.net/m0_66404702/article/details/127084517