Director articol
Cum gestionează MySQL interogările cu valori diferite de nulă? (Instrucțiunea SELECT MySQL IS NOT NULL)
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 verifica dacă coloana chenweiliang_test_tbl dintr-un tabel de date este NULL, trebuie să utilizați IS NULL și IS NOT NULL , așa cum se arată în exemplul următor:
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 );
?>
Sperăm că articolul „Cum se gestionează interogările cu valori non-null în baza de date MySQL? MySQL nu este o instrucțiune de selecție nulă” distribuit pe blogul lui Chen Weiliang ( https://www.chenweiliang.com/ ) vă va fi de ajutor.
Nu ezitați să distribuiți linkul acestui articol: https://www.chenweiliang.com/cwl-491.html
