How does MySQL database handle non-null value queries? MySQL is not null select statement

MySQL databaseHow to handle non-null value queries?MySQL is not null select statement

Null handling in MySQL

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 major operators:

  • IS NULL:This operator returns true when the column value is NULL.
  • IS NOT NULL:The operator returns true when the column value is not NULL.
  • <=>:  The comparison operator (unlike the = operator) returns true when the two values ​​compared are NULL.

Conditional comparison operations on NULL are special.You cannot use = NULL or ! =NULL finds NULL values ​​in the 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 value in command prompt

In the following example, the table chenweiliang_test_tbl in the database chenweiliang is set to contain two columns, chenweiliang_author and chenweiliang_count, and NULL values ​​are set to be inserted in chenweiliang_count.

Examples

Try the following examples:

Create data table chenweiliang_test_tbl

root @ host #mysql -u root -p password; 输入密码:*******
 mysql > 使用chenweiliang ;
数据库改变了mysql > create table chenweiliang_test_tbl 
 - > (
 - > chenweiliang_author varchar (40 )NOT NULL , - > chenweiliang_count INT 
 - > );
查询OK ,0 行受影响(0.05 秒)mysql >
 
 
INSERT INTO chenweiliang_test_tbl (chenweiliang_author ,chenweiliang_count )values (' chenweiliang ' ,20 );
mysql > INSERT INTO chenweiliang_test_tbl (chenweiliang_author ,chenweiliang_count )values (' 陈沩亮博客' ,NULL );
mysql > INSERT INTO chenweiliang_test_tbl (chenweiliang_author ,chenweiliang_count )values ( ' Google ' ,NULL );
mysql > INSERT INTO chenweiliang_test_tbl (chenweiliang_author ,chenweiliang_count )values (' FK ' ,20 );
 
mysql > SELECT * from chenweiliang_test_tbl ; + --------------- + -------------- + | chenweiliang_author | chenweiliang_count | + --------------- + -------------- + | chenweiliang | 20 | | 陈沩亮博客| NULL | | Google | NULL | | FK | 20 | + --------------- + -------------- +
 4 行中集合(0.01 秒) 

In the following example you can see = and ! The = operator does not work:

mysql > SELECT * FROM chenweiliang_test_tbl WHERE chenweiliang_count = NULL ;
空集(0.00 秒)mysql > SELECT * FROM chenweiliang_test_tbl WHERE chenweiliang_count != NULL ;
空集(0.01 秒)

To find out whether the chenweiliang_test_tbl column in the data table is NULL, you must useIS NULLIS NOT NULL, the following example:

mysql > SELECT * FROM chenweiliang_test_tbl WHERE chenweiliang_count IS NULL ; + --------------- + -------------- + | chenweiliang_author | chenweiliang_count | + --------------- + -------------- + | 陈沩亮博客| NULL | | Google | NULL | + --------------- + -------------- +
 2 行中的组(0.01 秒)的MySQL > SELECT * 从chenweiliang_test_tbl WHERE chenweiliang_count IS NOT 空值 
 
 ; + --------------- + -------------- + | chenweiliang_author | chenweiliang_count | + --------------- + -------------- + | chenweiliang | 20 | | FK | 20 | + --------------- + -------------- +
 2 行中的组(0.01 秒) 

Handling NULL values ​​with PHP script

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

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

MySQL ORDER BY test:

<?
php $ dbhost = ' localhost:3306 ' ; // mysql服务器主机地址

$ dbuser = ' root ' ; // mysql用户名
$ dbpass = ' 123456 ' ; // mysql用户名密码
$ conn = mysqli_connect ($ dbhost ,$ dbuser ,$ dbpass );
如果(!$ conn ){ die (' 连接失败:' 。mysqli_error ($ conn ));
} // 设置编码,防止中文乱码

mysqli_query ($ conn ,“ set names utf8 ” );
 
if (isset ($ chenweiliang_count )){ $ sql = “ SELECT chenweiliang_author,chenweiliang_count FROM chenweiliang_test_tbl WHER chenweiliang_count = $ chenweiliang_count ” ;
} else { $ sql = “ SELECT chenweiliang_author,chenweiliang_count FROM chenweiliang_test_tbl WHER chenweiliang_count IS NULL ” ;
} mysqli_select_db ($ conn ,'


 chenweiliang ' );
$ 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> ' ;

 $ retval ,MYSQL_ASSOC )){ echo “ <tr> ” 。
 “ <td> {$ row ['chenweiliang_author']} </ td> ” 。
 “ <td> {$ row ['chenweiliang_count']} </ td> ” 。
 “ </ tr> ” ;
} echo ' </ table> ' ;
mysqli_close ($ conn );
?>

 

Hope Chen Weiliang Blog ( https://www.chenweiliang.com/ ) shared "How does MySQL database handle non-null value queries? MySQL is not null select statement" to help you.

Welcome to share the link of this article:https://www.chenweiliang.com/cwl-491.html

Welcome to the Telegram channel of Chen Weiliang's blog to get the latest updates!

🔔 Be the first to get the valuable "ChatGPT Content Marketing AI Tool Usage Guide" in the channel top directory! 🌟
📚 This guide contains huge value, 🌟This is a rare opportunity, don’t miss it! ⏰⌛💨
Share and like if you like!
Your sharing and likes are our continuous motivation!

 

Comment

Your email address will not be published. Required fields * Callout

scroll to top