Cum gestionează baza de date MySQL interogările cu valori non-nule? MySQL nu este o instrucțiune nulă select

Baza de date MySQLCum se gestionează interogările cu valori non-nule?MySQL nu este declarația nulă select

Manipulare nulă în MySQL

Știm deja că MySQL folosește comanda SQL SELECT și clauza WHERE pentru a citi datele din tabelul de date, dar când câmpul de condiție de interogare furnizat este NULL, este posibil ca comanda să nu funcționeze corect.

Pentru a face față acestei situații, MySQL oferă trei operatori majori:

  • E NUL:Acest operator returnează true când valoarea coloanei este NULL.
  • NU ESTE NUL:Operatorul returnează true când valoarea coloanei nu este NULL.
  • <=>:  Operatorul de comparație (spre deosebire de operatorul =) returnează adevărat atunci când cele două valori comparate sunt NULL.

Operațiile de comparare condiționată pe NULL sunt speciale.Nu puteți folosi = NULL sau ! =NULL găsește valorile NULL în coloană.

În MySQL, compararea unei valori NULL cu orice altă valoare (chiar NULL) returnează întotdeauna false, adică NULL = NULL returnează false.

NULL este gestionat în MySQL folosind operatorii IS NULL și IS NOT NULL.


Utilizați valoarea NULL în linia de comandă

În exemplul următor, tabelul chenweiliang_test_tbl din baza de date chenweiliang este setat să conțină două coloane, chenweiliang_author și chenweiliang_count, iar valorile NULL sunt setate să fie inserate în chenweiliang_count.

Instanță

Încercați următoarele exemple:

Creați tabel de date 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 秒) 

În exemplul următor puteți vedea = și ! Operatorul = nu funcționează:

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

Pentru a afla dacă coloana chenweiliang_test_tbl din tabelul de date este NULL, trebuie să utilizațiESTE NULLNU ESTE NULL, următorul exemplu:

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

Gestionarea valorilor NULL cu script PHP

În scriptul PHP, puteți utiliza instrucțiunea if...else pentru a procesa dacă variabila este goală și pentru a genera o instrucțiune condiționată corespunzătoare.

În următorul exemplu, PHP setează variabila $chenweiliang_count și apoi folosește acea variabilă pentru a compara cu câmpul chenweiliang_count din tabelul de date:

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 „Cum gestionează baza de date MySQL interogările cu valori non-nule? MySQL nu este declarația null select” pentru a vă ajuta.

Bine ați venit să distribuiți linkul acestui articol:https://www.chenweiliang.com/cwl-491.html

Bun venit pe canalul Telegram al blogului lui Chen Weiliang pentru a primi cele mai recente actualizări!

🔔 Fii primul care primește valorosul „Ghid de utilizare a instrumentului AI pentru marketing de conținut ChatGPT” în directorul de top al canalului! 🌟
📚 Acest ghid conține o valoare uriașă, 🌟Aceasta este o oportunitate rară, nu o ratați! ⏰⌛💨
Distribuie si da like daca iti place!
Partajarea și like-urile tale sunt motivația noastră continuă!

 

发表 评论

Adresa ta de email nu va fi publicată. Sunt utilizate câmpurile obligatorii * Eticheta

derulați în sus