Tuhinga Tuhinga
pātengi raraunga MySQLMe pehea te whakahaere i nga patai uara kore-kore?MySQL ehara i te korero kowhiri kore
Te whakahaere kore i MySQL
Kua mohio tatou kei te whakamahi a MySQL i te whakahau SQL SELECT me te WHERE rara hei panui i nga raraunga i roto i te ripanga raraunga, engari i te mea ko te NULL te waahi patai e whakaratohia ana, kaore pea te whakahau e mahi tika.
Hei whakahaere i tenei ahuatanga, ka whakaratohia e MySQL nga kaiwhakahaere nui e toru:
- HE NULL:Ka hoki mai tenei kaiwhakahaere i te pono ina he NULL te uara o te pou.
- KARERE NULL:Ka hoki mai te kaiwhakahaere i te pono ka kore te uara o te pou i te NULL.
- <=>: Ko te kaiwhakahaere whakatairite (kaore i rite ki te = operator) ka hoki pono ina he NULL nga uara e rua.
He mea motuhake nga mahi whakataurite herenga i runga i te NULL.Kaore e taea e koe te whakamahi = NULL ranei! =Ka kitea e NULL nga uara NULL i te pou.
I roto i MySQL, ko te whakatairite o te uara NULL me tetahi atu uara (ahakoa NULL) ka whakahoki teka i nga wa katoa, ara ko NULL = NULL ka hoki teka.
Ka whakahaerehia te NULL i MySQL ma te whakamahi i te IS NULL me te IS NOT NULL.
Whakamahia te uara NULL i te whakahau whakahau
I roto i te tauira e whai ake nei, ko te ripanga chenweiliang_test_tbl i roto i te chenweiliang pātengi raraunga kua whakaritea kia rua nga pou, chenweiliang_author me chenweiliang_count, me nga uara NULL ka whakauruhia ki te chenweiliang_count.
Akoranga
Whakamātauria ngā tauira e whai ake nei:
Waihangahia te ripanga raraunga 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 秒)
I roto i te tauira e whai ake nei ka kite koe = me ! Ko te = karekau e mahi:
mysql > SELECT * FROM chenweiliang_test_tbl WHERE chenweiliang_count = NULL ; 空集(0.00 秒)mysql > SELECT * FROM chenweiliang_test_tbl WHERE chenweiliang_count != NULL ; 空集(0.01 秒)
Hei kimi mehemea he NULL te chenweiliang_test_tbl tīwae i te ripanga raraunga, me whakamahi koeIS NULLAKORE NULL, te tauira e whai ake nei:
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 秒)
Te whakahaere i nga uara NULL me te tuhinga PHP
I roto i te tuhinga PHP, ka taea e koe te whakamahi i te korero if...else ki te tukatuka mehemea kei te putua te taurangi me te whakaputa i tetahi tauākī herenga.
I roto i te tauira e whai ake nei ka tautuhia e PHP te taurangi $chenweiliang_count ka whakamahi i taua taurangi hei whakataurite ki te mara chenweiliang_count i te ripanga raraunga:
MySQL ORDER MA te whakamatautau:
<? 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 "Me pehea te whakahaere a MySQL i nga patai uara kore-kore? Ehara a MySQL i te tauākī kōwhiri kore" hei āwhina i a koe.
Nau mai ki te tohatoha i te hono o tenei tuhinga:https://www.chenweiliang.com/cwl-491.html
Nau mai ki te hongere Telegram o te blog a Chen Weiliang ki te tiki i nga korero hou!
📚 He uara nui tenei aratohu, 🌟He waahi onge tenei, kaua e ngaro! ⏰⌛💨
Tohaina me te pai ki te pai koe!
Ko to tiritiri me o hiahia ko to maatau hihiri tonu!