Drejtoria e artikujve
Si i trajton MySQL pyetjet me vlerë jo-null? (Deklarata MySQL IS NOT NULL SELECT)
Trajtimi null në MySQL
Ne tashmë e dimë se MySQL përdor komandën SQL SELECT dhe klauzolën WHERE për të lexuar të dhënat në tabelën e të dhënave, por kur fusha e kushtit të pyetjes e dhënë është NULL, komanda mund të mos funksionojë siç duhet.
Për të trajtuar këtë situatë, MySQL ofron tre operatorë kryesorë:
- ËSHTË NULL:Ky operator kthen true kur vlera e kolonës është NULL.
- NUK ËSHTË NULL:Operatori kthen true kur vlera e kolonës nuk është NULL.
- <=>: Operatori i krahasimit (ndryshe nga operatori =) kthehet i vërtetë kur dy vlerat e krahasuara janë NULL.
Operacionet e krahasimit të kushtëzuar në NULL janë të veçanta.Nuk mund të përdorni = NULL ose ! =NULL gjen vlerat NULL në kolonë.
Në MySQL, një krahasim i një vlere NULL me çdo vlerë tjetër (madje edhe NULL) kthen gjithmonë false, dmth. NULL = NULL kthen false.
NULL trajtohet në MySQL duke përdorur operatorët IS NULL dhe IS NOT NULL.
Përdorni vlerën NULL në vijën e komandës
Në shembullin e mëposhtëm, tabela chenweiliang_test_tbl në bazën e të dhënave chenweiliang është vendosur të përmbajë dy kolona, chenweiliang_author dhe chenweiliang_count, dhe vlerat NULL janë vendosur të futen në chenweiliang_count.
Shembull
Provoni shembujt e mëposhtëm:
Krijo tabelën e të dhënave 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ë shembullin e mëposhtëm mund të shihni = dhe ! Operatori = nuk funksionon:
mysql > SELECT * FROM chenweiliang_test_tbl WHERE chenweiliang_count = NULL ; 空集(0.00 秒)mysql > SELECT * FROM chenweiliang_test_tbl WHERE chenweiliang_count != NULL ; 空集(0.01 秒)
Për të kontrolluar nëse kolona chenweiliang_test_tbl në një tabelë të dhënash është NULL, duhet të përdorni IS NULL dhe IS NOT NULL , siç tregohet në shembullin e mëposhtëm:
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 秒)
Trajtimi i vlerave NULL me skriptin PHP
Në skriptin PHP, mund të përdorni deklaratën if...else për të përpunuar nëse ndryshorja është bosh dhe të gjeneroni një deklaratë të kushtëzuar korresponduese.
Në shembullin e mëposhtëm PHP vendos variablin $chenweiliang_count dhe më pas përdor atë variabël për t'u krahasuar me fushën chenweiliang_count në tabelën e të dhënave:
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 );
?>
Shpresojmë që artikulli "Si të trajtohen pyetjet me vlerë jo-null në bazën e të dhënave MySQL? MySQL IS NOT NULL SELECT statement" i ndarë në blogun e Chen Weiliang ( https://www.chenweiliang.com/ ) do t'ju jetë i dobishëm.
Ndihuni të lirë të ndani lidhjen e këtij artikulli: https://www.chenweiliang.com/cwl-491.html
