{"id":238,"date":"2023-02-24T05:58:49","date_gmt":"2023-02-23T21:58:49","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=238"},"modified":"2023-04-30T15:20:56","modified_gmt":"2023-04-30T07:20:56","slug":"mysql-null-value-processing","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/24\/mysql-null-value-processing\/","title":{"rendered":"MySQL NULL \u503c\u5904\u7406"},"content":{"rendered":"<h2>\u5b57\u6bb5\u4e3a NULL \u503c\u53ca\u6bd4\u8f83<\/h2>\n<p>\u6211\u4eec\u5df2\u7ecf\u77e5\u9053 MySQL \u4f7f\u7528<code>SQL SELECT<\/code>\u547d\u4ee4\u53ca<code>WHERE<\/code>\u5b50\u53e5\u6765\u8bfb\u53d6\u6570\u636e\u8868\u4e2d\u7684\u6570\u636e\uff0c\u4f46\u662f\u5f53\u63d0\u4f9b\u7684\u67e5\u8be2\u6761\u4ef6\u5b57\u6bb5\u4e3a<code>NULL<\/code>\u65f6\uff0c\u8be5\u547d\u4ee4\u53ef\u80fd\u5c31\u65e0\u6cd5\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<p><!-- more --><\/p>\n<p>\u4e3a\u4e86\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\uff0cMySQL\u63d0\u4f9b\u4e86\u4e09\u5927\u8fd0\u7b97\u7b26\uff1a<\/p>\n<ul>\n<li><code>IS NULL<\/code>: \u5f53\u5217\u7684\u503c\u662f<code>NULL<\/code>\uff0c\u6b64\u8fd0\u7b97\u7b26\u8fd4\u56de true<\/li>\n<li><code>IS NOT NULL<\/code>: \u5f53\u5217\u7684\u503c\u4e0d\u4e3a<code>NULL<\/code>\uff0c\u8fd0\u7b97\u7b26\u8fd4\u56de true<\/li>\n<li><code>&lt;=&gt;<\/code>: \u6bd4\u8f83\u64cd\u4f5c\u7b26\uff08\u4e0d\u540c\u4e8e<code>=<\/code>\u8fd0\u7b97\u7b26\uff09\uff0c\u5f53\u6bd4\u8f83\u7684\u7684\u4e24\u4e2a\u503c\u76f8\u7b49\u6216\u8005\u90fd\u4e3a<code>NULL<\/code>\u65f6\u8fd4\u56de true<\/li>\n<\/ul>\n<p>\u5173\u4e8e<code>NULL<\/code>\u7684\u6761\u4ef6\u6bd4\u8f83\u8fd0\u7b97\u662f\u6bd4\u8f83\u7279\u6b8a\u7684\u3002\u4e0d\u80fd\u4f7f\u7528<code>= NULL<\/code>\u6216<code>!= NULL<\/code>\u5728\u5217\u4e2d\u67e5\u627e<code>NULL<\/code>\u503c<\/p>\n<p>\u5728 MySQL \u4e2d\uff0c<code>NULL<\/code>\u503c\u4e0e\u4efb\u4f55\u5176\u5b83\u503c\u7684\u6bd4\u8f83\uff08\u5373\u4f7f\u662f NULL\uff09\u6c38\u8fdc\u8fd4\u56de<code>NULL<\/code>\uff0c\u5373<code>NULL = NULL<\/code>\u8fd4\u56de<code>NULL<\/code><\/p>\n<p>MySQL \u4e2d\u5904\u7406<code>NULL<\/code>\u4f7f\u7528<code>IS NULL<\/code>\u548c<code>IS NOT NULL<\/code>\u8fd0\u7b97\u7b26<\/p>\n<h2>ifnull NULL\u503c\u8f6c\u6362<\/h2>\n<pre><code class=\"language-sql\">select * , columnName1+ifnull(columnName2,0) from tableName;<\/code><\/pre>\n<p><code>columnName1<\/code>\uff0c<code>columnName2<\/code>\u4e3a int \u578b\uff0c\u5f53<code>columnName2<\/code>\u4e2d\u6709\u503c\u4e3a<code>null<\/code>\u65f6\uff0c<code>columnName1+columnName2=null<\/code>\uff0c <code>ifnull(columnName2,0)<\/code>\u628a<code>columnName2<\/code>\u4e2d<code>null<\/code>\u503c\u8f6c\u4e3a 0<\/p>\n<h2>\u4f7f\u7528\u5b9e\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u4e2d\u5047\u8bbe\u6570\u636e\u5e93 appblog \u4e2d\u7684\u8868<code>appblog_test_tbl<\/code>\u542b\u6709\u4e24\u5217<code>appblog_author<\/code>\u548c<code>ppblog_count<\/code>\uff0c<code>appblog_count<\/code>\u4e2d\u8bbe\u7f6e\u63d2\u5165<code>NULL<\/code>\u503c\u3002<\/p>\n<p>\u521b\u5efa\u6570\u636e\u8868<code>appblog_test_tbl<\/code><\/p>\n<pre><code class=\"language-sql\">root@host# mysql -u root -p password;\nEnter password:*******\nmysql&gt; use appblog;\nDatabase changed\nmysql&gt; create table appblog_test_tbl\n    -&gt; (\n    -&gt; appblog_author varchar(40) NOT NULL,\n    -&gt; appblog_count INT\n    -&gt; );\nQuery OK, 0 rows affected (0.05 sec)\nmysql&gt; INSERT INTO appblog_test_tbl (appblog_author, appblog_count) values (&#039;APPBLOG.CN&#039;, 20);\nmysql&gt; INSERT INTO appblog_test_tbl (appblog_author, appblog_count) values (&#039;APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2&#039;, NULL);\nmysql&gt; INSERT INTO appblog_test_tbl (appblog_author, appblog_count) values (&#039;Google&#039;, NULL);\nmysql&gt; INSERT INTO appblog_test_tbl (appblog_author, appblog_count) values (&#039;FK&#039;, 20);\n\nmysql&gt; SELECT * from appblog_test_tbl;\n+-----------------+---------------+\n| appblog_author  | appblog_count |\n+-----------------+---------------+\n| APPBLOG.CN      | 20            |\n| APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2 | NULL          |\n| Google          | NULL          |\n| FK              | 20            |\n+-----------------+---------------+\n4 rows in set (0.01 sec)<\/code><\/pre>\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u4e2d\u53ef\u4ee5\u770b\u5230<code>=<\/code>\u548c<code>!=<\/code>\u8fd0\u7b97\u7b26\u662f\u4e0d\u8d77\u4f5c\u7528\u7684\uff1a<\/p>\n<pre><code class=\"language-sql\">mysql&gt; SELECT * FROM appblog_test_tbl WHERE appblog_count = NULL;\nEmpty set (0.00 sec)\nmysql&gt; SELECT * FROM appblog_test_tbl WHERE appblog_count != NULL;\nEmpty set (0.01 sec)<\/code><\/pre>\n<p>\u67e5\u627e\u6570\u636e\u8868\u4e2d<code>appblog_test_tbl<\/code>\u5217\u662f\u5426\u4e3a<code>NULL<\/code>\uff0c\u5fc5\u987b\u4f7f\u7528<code>IS NULL<\/code>\u548c<code>IS NOT NULL<\/code>\uff0c\u5982\u4e0b\u5b9e\u4f8b\uff1a<\/p>\n<pre><code class=\"language-sql\">mysql&gt; SELECT * FROM appblog_test_tbl WHERE appblog_count IS NULL;\n+-----------------+---------------+\n| appblog_author  | appblog_count |\n+-----------------+---------------+\n| APP\u5f00\u53d1\u6280\u672f\u535a\u5ba2 | NULL          |\n| Google          | NULL          |\n+-----------------+---------------+\n2 rows in set (0.01 sec)\n\nmysql&gt; SELECT * from appblog_test_tbl WHERE appblog_count IS NOT NULL;\n+----------------+---------------+\n| appblog_author | appblog_count |\n+----------------+---------------+\n| APPBLOG.CN     | 20            |\n| FK             | 20            |\n+----------------+---------------+\n2 rows in set (0.01 sec)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5b57\u6bb5\u4e3a NULL \u503c\u53ca\u6bd4\u8f83 \u6211\u4eec\u5df2\u7ecf\u77e5\u9053 MySQL \u4f7f\u7528SQL SELECT\u547d\u4ee4\u53caWHERE\u5b50\u53e5\u6765\u8bfb\u53d6\u6570\u636e\u8868 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-238","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=238"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/238\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}