{"id":199737,"date":"2025-05-06T10:13:11","date_gmt":"2025-05-06T02:13:11","guid":{"rendered":"https:\/\/server.hk\/cnblog\/199737\/"},"modified":"2025-05-06T10:13:11","modified_gmt":"2025-05-06T02:13:11","slug":"%e5%88%86%e6%9e%90-mysql-%e4%b8%ad-isnull-%e5%87%bd%e6%95%b0%e7%9a%84%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/199737\/","title":{"rendered":"\u5206\u6790 MySQL \u4e2d ISNULL \u51fd\u6570\u7684\u4f7f\u7528\u65b9\u6cd5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5206\u6790 MySQL \u4e2d ISNULL \u51fd\u6570\u7684\u4f7f\u7528\u65b9\u6cd5<\/h1>\n<p>\u6570\u636e\u5e93\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300a\u5206\u6790 MySQL \u4e2d ISNULL \u51fd\u6570\u7684\u4f7f\u7528\u65b9\u6cd5\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><\/p>\n<p>ISNULL \u51fd\u6570\u5728 MySQL \u4e2d\u7684\u5e94\u7528\u573a\u666f\u5206\u6790<\/p>\n<p>MySQL \u662f\u4e00\u4e2a\u6d41\u884c\u7684\u5173\u7cfb\u578b\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u51fd\u6570\u548c\u64cd\u4f5c\u7b26\u6765\u5904\u7406\u6570\u636e\u3002\u5176\u4e2d\uff0cISNULL \u51fd\u6570\u662f\u4e00\u79cd\u5e38\u7528\u7684\u51fd\u6570\uff0c\u7528\u6765\u5224\u65ad\u5b57\u6bb5\u662f\u5426\u4e3a\u7a7a\uff0c\u5e76\u8fd4\u56de\u76f8\u5e94\u7684\u5904\u7406\u7ed3\u679c\u3002\u672c\u6587\u5c06\u5206\u6790 ISNULL \u51fd\u6570\u5728 MySQL \u4e2d\u7684\u5e94\u7528\u573a\u666f\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001ISNULL \u51fd\u6570\u7684\u8bed\u6cd5\u548c\u529f\u80fd<\/p>\n<p>ISNULL \u51fd\u6570\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>ISNULL(expression)<\/pre>\n<p>ISNULL \u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\uff0c\u901a\u5e38\u662f\u5b57\u6bb5\u6216\u8868\u8fbe\u5f0f\uff0c\u7528\u6765\u5224\u65ad\u8be5\u53c2\u6570\u662f\u5426\u4e3a\u7a7a\u3002\u5982\u679c\u53c2\u6570\u4e3a\u7a7a\uff0c\u5219\u8fd4\u56de TRUE\uff0c\u5426\u5219\u8fd4\u56de FALSE\u3002<\/p>\n<p>\u4e8c\u3001ISNULL \u51fd\u6570\u7684\u5e94\u7528\u573a\u666f\u5206\u6790<\/p>\n<ol>\n<li>\u5224\u65ad\u5b57\u6bb5\u662f\u5426\u4e3a\u7a7a<br \/>ISNULL \u51fd\u6570\u53ef\u4ee5\u7528\u6765\u5224\u65ad\u7279\u5b9a\u5b57\u6bb5\u662f\u5426\u4e3a\u7a7a\uff0c\u8fd9\u5728\u6570\u636e\u5904\u7406\u548c\u7b5b\u9009\u65f6\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 ISNULL \u51fd\u6570\u7b5b\u9009\u51fa\u67d0\u4e2a\u8868\u4e2d\u7279\u5b9a\u5b57\u6bb5\u4e3a\u7a7a\u7684\u8bb0\u5f55\uff0c\u4ee5\u4fbf\u8fdb\u4e00\u6b65\u5904\u7406\u6216\u586b\u5145\u6570\u636e\u3002<\/li>\n<\/ol>\n<p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>SELECT * FROM users WHERE ISNULL(email);<\/pre>\n<ol>\n<li>\u66ff\u6362 NULL \u503c<br \/>\u6709\u65f6\u5019\u6570\u636e\u5e93\u4e2d\u7684\u5b57\u6bb5\u503c\u4e3a\u7a7a\uff08NULL\uff09\uff0c\u800c\u6211\u4eec\u5e0c\u671b\u5c06\u8fd9\u4e9b\u7a7a\u503c\u66ff\u6362\u4e3a\u7279\u5b9a\u7684\u6570\u503c\u6216\u5b57\u7b26\u4e32\u3002ISNULL \u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u8fd9\u4e00\u76ee\u7684\uff0c\u5c06\u7a7a\u503c\u66ff\u6362\u4e3a\u6307\u5b9a\u7684\u503c\u3002<\/li>\n<\/ol>\n<p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>SELECT IF(ISNULL(email), '\u672a\u77e5\u90ae\u7bb1', email) AS new_email FROM users;<\/pre>\n<ol>\n<li>\u8ba1\u7b97\u7a7a\u503c\u6570\u91cf<br \/>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u6709\u65f6\u6211\u4eec\u9700\u8981\u7edf\u8ba1\u67d0\u4e2a\u5b57\u6bb5\u4e2d\u7a7a\u503c\u7684\u6570\u91cf\u3002ISNULL \u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5feb\u901f\u8ba1\u7b97\u51fa\u8fd9\u4e2a\u6570\u91cf\uff0c\u4e3a\u540e\u7eed\u5206\u6790\u63d0\u4f9b\u4f9d\u636e\u3002<\/li>\n<\/ol>\n<p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>SELECT COUNT(*) AS null_count FROM users WHERE ISNULL(username);<\/pre>\n<ol>\n<li>\u907f\u514d NULL \u503c\u7684\u8ba1\u7b97\u9519\u8bef<br \/>\u5728\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u6216\u903b\u8f91\u5224\u65ad\u65f6\uff0c\u9047\u5230 NULL \u503c\u4f1a\u5bfc\u81f4\u7ed3\u679c\u51fa\u73b0\u9519\u8bef\u3002\u4f7f\u7528 ISNULL \u51fd\u6570\u80fd\u591f\u9884\u5148\u6392\u9664\u6389\u5b57\u6bb5\u4e3a\u7a7a\u7684\u60c5\u51b5\uff0c\u907f\u514d\u51fa\u73b0\u8ba1\u7b97\u9519\u8bef\u3002<\/li>\n<\/ol>\n<p>\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>SELECT num1 + IF(ISNULL(num2), 0, num2) AS total FROM numbers_table;<\/pre>\n<p>\u4e09\u3001\u603b\u7ed3<\/p>\n<p>ISNULL \u51fd\u6570\u5728 MySQL \u4e2d\u5177\u6709\u91cd\u8981\u7684\u5e94\u7528\u4ef7\u503c\uff0c\u80fd\u591f\u65b9\u4fbf\u5730\u5904\u7406\u7a7a\u503c\u7684\u60c5\u51b5\uff0c\u63d0\u9ad8\u6570\u636e\u5904\u7406\u7684\u6548\u7387\u548c\u51c6\u786e\u6027\u3002\u901a\u8fc7\u5bf9 ISNULL \u51fd\u6570\u7684\u5408\u7406\u4f7f\u7528\uff0c\u53ef\u4ee5\u4f18\u5316\u6570\u636e\u5e93\u67e5\u8be2\u548c\u6570\u636e\u5904\u7406\u8fc7\u7a0b\uff0c\u4fdd\u8bc1\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u51c6\u786e\u6027\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u548c\u6570\u636e\u5206\u6790\u4e2d\uff0c\u5408\u7406\u8fd0\u7528 ISNULL \u51fd\u6570\u5c06\u662f\u63d0\u5347\u5de5\u4f5c\u6548\u7387\u7684\u5229\u5668\u3002<\/p>\n<p>\u4ee5\u4e0a\u662fISNULL \u51fd\u6570\u5728 MySQL \u4e2d\u7684\u5e94\u7528\u573a\u666f\u5206\u6790\uff0c\u5e0c\u671b\u5bf9\u8bfb\u8005\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u4e3b\u673a\u5b9d\u8d1d\uff01\u66f4\u591a\u5173\u4e8e\u6570\u636e\u5e93\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\u3002<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5206\u6790 MySQL \u4e2d ISNUL&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-199737","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/199737","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=199737"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/199737\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=199737"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=199737"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=199737"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}