4.2.22. MySQL NULL value processing

发布时间 :2025-10-25 12:31:20 UTC      

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 query condition field provided is NULL, the command may not work properly.

To handle this situation, MySQL provides three operators:

  • IS NULL: This operator returns true when the value of the column is NULL.

  • IS NOT NULL: When the value of the column is not NULL, the operator returns true.

  • <=>: The comparison operator (unlike the = operator) returns true when the two values compared are equal or both are NULL.

The conditional comparison operation on NULL is quite special. You cannot use = NULL or! = NULL to find the NULL value in a column.

In MySQL, the comparison of the NULL value with any other value, even if it is NULL, always returns NULL, that is, NULL = NULL returns NULL.

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

注意:

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

ColumnName1,columnName2 is of int type. When the value in columnName2 is null, columnName1+columnName2=null, ifnull (columnName2,0) converts the null value in columnName2 to 0.

Use the NULL value at the command prompt

In the following example, it is assumed that the table runoob_test_tbl in the database RUNOOB contains two columns runoob_author and runoob_count, and the insert null value is set in runoob_count.

Example

Try the following example:

Create datasheet runoob_test_tbl

root@host# mysql -u root -p
password;Enterpassword:******\*mysql>useRUNOOB;Databasechangedmysql>createtablerunoob_test_tbl->(->runoob_authorvarchar(40)NOTNULL,
->runoob_countINT->);QueryOK,0rowsaffected(0.05sec)mysql>INSERTINTOrunoob_test_tbl(runoob_author,runoob_count)values('RUNOOB',20);mysql>INSERTINTOrunoob_test_tbl(runoob_author,runoob_count)values('菜鸟教程',NULL);mysql>INSERTINTOrunoob_test_tbl(runoob_author,runoob_count)values('Google',NULL);mysql>INSERTINTOrunoob_test_tbl(runoob_author,runoob_count)values('FK',20);mysql>SELECT\*fromrunoob_test_tbl;
+---------------+--------------+ \|runoob_author\|runoob_count\|
+---------------+--------------+ \|RUNOOB\|20\| \| 菜鸟教程 \|NULL\|
\|Google\|NULL\| \|FK\|20\|
+---------------+--------------+4rowsinset(0.01sec)

In the following example, you can see that the = and! = operators do not work:

mysql>SELECT\*FROMrunoob_test_tblWHERErunoob_count=NULL;Emptyset(0.00sec)mysql>SELECT\*FROMrunoob_test_tblWHERErunoob_count!=NULL;Emptyset(0.01sec)

Look in the data table runoob_test_tbl Whether the column is NULL, you must use the IS NULL And IS NOT NULL , as an example:

mysql>SELECT\*FROMrunoob_test_tblWHERErunoob_countISNULL;
+---------------+--------------+ \|runoob_author\|runoob_count\|
+---------------+--------------+ \| 菜鸟教程 \|NULL\| \|Google\|NULL\|
+---------------+--------------+2rowsinset(0.01sec)mysql>SELECT\*fromrunoob_test_tblWHERErunoob_countISNOTNULL;
+---------------+--------------+ \|runoob_author\|runoob_count\|
+---------------+--------------+ \|RUNOOB\|20\| \|FK\|20\|
+---------------+--------------+2rowsinset(0.01sec)

Using PHP scripts to process NULL values

In the PHP script, you can process whether the variable is empty or not in the if…else statement and generate the corresponding conditional statement.

In the following example, PHP sets the $runoob_count variable, and then uses it to match the runoob_count Fields to compare:

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($runoob_count)){$sql="SELECT runoob_author,
runoob_count FROM runoob_test_tbl WHERE runoob_count
=$runoob_count";}else{$sql="SELECT runoob_author, runoob_count FROM
runoob_test_tbl WHERE runoob_count IS
NULL";}mysqli_select_db($conn,'RUNOOB');$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['runoob_author']}</td>"."<td>{$row['runoob_count']}</td>"."</tr>";}echo'</table>';mysqli_close($conn);?>

The output is shown in the following figure:

Image0

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.