MySQL NULL value handling

MySQL uses the SQL SELECT command and the WHERE clause to read the data in the data table, but when the query condition field provided 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.

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

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

NULL is handled in MySQL using the IS NULL and IS NOT NULL operators.


Use NULL values ​​in command prompt

In the following example, it is assumed that the table tcount_tbl in the database RUNOOB contains two columns, runoob_author and runoob_count, and NULL values ​​are set to be inserted in runoob_count.

Try the following examples:

MariaDB [RUNOOB]> select * from tcount_tbl;
+---------------+--------------+
| runoob_author | runoob_count |
+---------------+--------------+
| mahran        |           20 |
| mahran        |         NULL |
| Jen |        NULL |
| Gill          |           20 |
| John Poul | 1 |
| Sanjay        |            1 |
+---------------+--------------+
6 rows in set (0.00 sec)

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

MariaDB [RUNOOB]> SELECT * FROM tcount_tbl WHERE runoob_count = NULL;
Empty set (0.00 sec)

MariaDB [RUNOOB]> SELECT * FROM tcount_tbl WHERE runoob_count != NULL;
Empty set (0.00 sec)

To find out whether the runoob_count column in the data table is NULL, you must use IS NULL and IS NOT NULL, as follows:

MariaDB [RUNOOB]> SELECT * FROM tcount_tbl where runoob_count IS NULL;
+---------------+--------------+
| runoob_author | runoob_count |
+---------------+--------------+
| mahran        |         NULL |
| Jen |         NULL |
+---------------+--------------+
2 rows in set (0.00 sec)

MariaDB [RUNOOB]> SELECT * FROM tcount_tbl where runoob_count IS NOT NULL;
+---------------+--------------+
| runoob_author | runoob_count |
+---------------+--------------+
| mahran        |           20 |
| Gill          |           20 |
| John Poul     |            1 |
| Sanjay        |            1 |
+---------------+--------------+
4 rows in set (0.00 sec)

使用PHP脚本处理 NULL 值

PHP脚本中你可以在 if...else 语句来处理变量是否为空,并生成相应的条件语句。

以下实例中PHP设置了$runoob_count变量,然后使用该变量与数据表中的 runoob_count 字段进行比较:

  1. <?php
  2. $dbhost ='localhost:3036';
  3. $dbuser ='root';
  4. $dbpass ='rootpassword';
  5. $conn = mysql_connect($dbhost, $dbuser, $dbpass);
  6. if(! $conn )
  7. {
  8. die('Could not connect: '. mysql_error());
  9. }
  10. if( isset($runoob_count ))
  11. {
  12. $sql ='SELECT runoob_author, runoob_count
  13. FROM tcount_tbl
  14. WHERE runoob_count IS NOT NULL';
  15. }
  16. else
  17. {
  18. $sql ='SELECT runoob_author, runoob_count
  19. FROM tcount_tbl
  20. WHERE runoob_count IS NULL';
  21. }
  22. mysql_select_db('RUNOOB');
  23. $retval = mysql_query( $sql, $conn );
  24. if(! $retval )
  25. {
  26. die('Could not get data: '. mysql_error());
  27. }
  28. while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
  29. {
  30. echo "Author:{$row['runoob_author']} <br> ".
  31. "Count: {$row['runoob_count']} <br> ".
  32. "--------------------------------<br>";
  33. }
  34. echo "Fetched data successfully\n";
  35. mysql_close($conn);
  36. ?>
 
运行结果:

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326470151&siteId=291194637