Articulu Directory
basa di dati MySQLCumu trattà e dumande di valore micca nulla?MySQL ùn hè micca una dichjarazione di selezzione nulla
Gestione nulla in MySQL
Sapemu digià chì MySQL usa u cumandamentu SQL SELECT è a clause WHERE per leghje e dati in a tabella di dati, ma quandu u campu di a cundizione di dumanda furnitu hè NULL, u cumandimu pò micca travaglià bè.
Per trattà sta situazione, MySQL furnisce trè operatori principali:
- È NULL:Questu operatore torna veru quandu u valore di a colonna hè NULL.
- NON È NULL:L'operatore torna veru quandu u valore di a colonna ùn hè micca NULL.
- <=>: L'operatore di paraguni (cuntrariu di l'operatore =) torna veru quandu i dui valori paragunati sò NULL.
L'operazioni di paraguni cundiziunali nantu à NULL sò speciali.Ùn pudete micca aduprà = NULL o ! =NULL trova i valori NULL in a colonna.
In MySQL, a comparazione di un valore NULL cù qualsiasi altru valore (ancu NULL) torna sempre falsu, vale à dì NULL = NULL torna falsu.
NULL hè trattatu in MySQL cù l'operatori IS NULL è IS NOT NULL.
Aduprate u valore NULL in u prompt di cumanda
In l'esempiu seguitu, a tavola chenweiliang_test_tbl in a basa di dati chenweiliang hè stallata per cuntene duie colonne, chenweiliang_author è chenweiliang_count, è i valori NULL sò posti per esse inseriti in chenweiliang_count.
Istanza
Pruvate i seguenti esempi:
Crea a tabella di 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 秒)
In l'esempiu seguente pudete vede = è ! L'operatore = ùn funziona micca:
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 sapè s'ellu a colonna chenweiliang_test_tbl in a tabella di dati hè NULL, duvete adupràIS NULL和NON hè NULL, u seguente esempiu:
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 秒)
Manipulazione di valori NULL cù script PHP
In u script PHP, pudete aduprà a dichjarazione if...else per processà se a variàbile hè viota è generà una dichjarazione cundizionale currispondente.
In l'esempiu seguente PHP stabilisce a variàbile $chenweiliang_count è poi usa quella variabile per paragunà cù u campu di chenweiliang_count in a tabella di dati:
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/ ) spartutu "Cumu a basa di dati MySQL gestisce e dumande di valore micca nulla? MySQL ùn hè micca null select statement" per aiutà.
Benvenuti à sparte u ligame di stu articulu:https://www.chenweiliang.com/cwl-491.html
Benvenuti à u canali Telegram di u blog di Chen Weiliang per avè l'ultime aghjurnamenti!
📚 Questa guida cuntene un valore enormu, 🌟Questa hè una rara opportunità, ùn mancate micca! ⏰⌛💨
Condividi è mi piace se ti piace!
A vostra spartera è Mi piace sò a nostra motivazione cuntinua!