4.2.22. MySQL NULL value processing

发布时间 : 2025-10-25 13:32:50 UTC      

Page Views: 9 views

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:

菜鸟教程 IS NULL 测试

';echo'';while($row=mysqli_fetch_array($retval,MYSQL_ASSOC)){echo""."".""."";}echo'
作者登陆次数
{$row['runoob_author']}{$row['runoob_count']}
';mysqli_close($conn);?>

The output is shown in the following figure:

Image0

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。