In che modo il database MySQL gestisce le query con valori non nulli? MySQL non è un'istruzione select null

Database MySQLCome gestire le query con valori non nulli?MySQL non è un'istruzione select nulla

Gestione nulla in MySQL

Sappiamo già che MySQL utilizza il comando SQL SELECT e la clausola WHERE per leggere i dati nella tabella dei dati, ma quando il campo della condizione della query fornito è NULL, il comando potrebbe non funzionare correttamente.

Per gestire questa situazione, MySQL fornisce tre operatori principali:

  • È ZERO:Questo operatore restituisce true quando il valore della colonna è NULL.
  • NON È NULLA:L'operatore restituisce true quando il valore della colonna non è NULL.
  • <=>:  L'operatore di confronto (a differenza dell'operatore =) restituisce true quando i due valori confrontati sono NULL.

Le operazioni di confronto condizionale su NULL sono speciali.Non puoi usare = NULL o ! =NULL trova valori NULL nella colonna.

In MySQL, un confronto di un valore NULL con qualsiasi altro valore (anche NULL) restituisce sempre false, ovvero NULL = NULL restituisce false.

NULL viene gestito in MySQL utilizzando gli operatori IS NULL e IS NOT NULL.


Utilizzare il valore NULL nel prompt dei comandi

Nell'esempio seguente, la tabella chenweiliang_test_tbl nel database chenweiliang è impostata per contenere due colonne, chenweiliang_author e chenweiliang_count, e i valori NULL sono impostati per essere inseriti in chenweiliang_count.

Esempio

Prova i seguenti esempi:

Crea tabella dati 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 秒) 

Nell'esempio seguente puoi vedere = e ! L'operatore = non funziona:

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

Per scoprire se la colonna chenweiliang_test_tbl nella tabella dei dati è NULL, devi utilizzareÈ ZERONON È NULL, il seguente esempio:

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 秒) 

Gestione dei valori NULL con script PHP

Nello script PHP, puoi utilizzare l'istruzione if...else per elaborare se la variabile è vuota e generare un'istruzione condizionale corrispondente.

Nell'esempio seguente PHP imposta la variabile $chenweiliang_count e quindi utilizza tale variabile per confrontare con il campo chenweiliang_count nella tabella dei dati:

MySQL ORDINE PER 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 );
?>

 

发表 评论

Il tuo indirizzo email non verrà pubblicato. 必填 项 已 用 * 标注

Scorrere fino a Top