{"id":168527,"date":"2024-11-06T13:07:16","date_gmt":"2024-11-06T05:07:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/168527\/"},"modified":"2024-11-06T13:07:17","modified_gmt":"2024-11-06T05:07:17","slug":"sql-%e4%b8%ad%e7%9a%84-isnull-%e5%87%bd%e6%95%b8%e4%bb%8b%e7%b4%b9","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/168527\/","title":{"rendered":"SQL \u4e2d\u7684 ISNULL \u51fd\u6578\u4ecb\u7d39"},"content":{"rendered":"<h1 id=\"sql-%e4%b8%ad%e7%9a%84-isnull-%e5%87%bd%e6%95%b8%e4%bb%8b%e7%b4%b9-jjXvThXoOK\">SQL \u4e2d\u7684 ISNULL \u51fd\u6578\u4ecb\u7d39<\/h1>\n<p>\u5728 SQL \u4e2d\uff0c\u8655\u7406\u7a7a\u503c\uff08NULL\uff09\u662f\u4e00\u500b\u5e38\u898b\u7684\u9700\u6c42\u3002\u7576\u6578\u64da\u5eab\u4e2d\u7684\u67d0\u4e9b\u6b04\u4f4d\u6c92\u6709\u503c\u6642\uff0c\u9019\u4e9b\u6b04\u4f4d\u6703\u88ab\u6a19\u8a18\u70ba NULL\u3002\u9019\u53ef\u80fd\u6703\u5728\u67e5\u8a62\u548c\u6578\u64da\u8655\u7406\u904e\u7a0b\u4e2d\u5f15\u767c\u554f\u984c\uff0c\u56e0\u6b64\u9700\u8981\u4f7f\u7528\u4e00\u4e9b\u51fd\u6578\u4f86\u8655\u7406\u9019\u4e9b\u7a7a\u503c\u3002ISNULL \u51fd\u6578\u5c31\u662f\u5176\u4e2d\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\u3002<\/p>\n<h2 id=\"isnull-%e5%87%bd%e6%95%b8%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-jjXvThXoOK\">ISNULL \u51fd\u6578\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>ISNULL \u51fd\u6578\u662f\u4e00\u500b\u7528\u65bc\u6aa2\u67e5\u8868\u9054\u5f0f\u662f\u5426\u70ba NULL \u7684\u51fd\u6578\u3002\u5982\u679c\u8a72\u8868\u9054\u5f0f\u70ba NULL\uff0c\u5247 ISNULL \u51fd\u6578\u6703\u8fd4\u56de\u6307\u5b9a\u7684\u66ff\u4ee3\u503c\uff1b\u5982\u679c\u4e0d\u662f NULL\uff0c\u5247\u8fd4\u56de\u8a72\u8868\u9054\u5f0f\u7684\u539f\u59cb\u503c\u3002\u9019\u4f7f\u5f97 ISNULL \u51fd\u6578\u5728\u6578\u64da\u67e5\u8a62\u548c\u5831\u544a\u4e2d\u975e\u5e38\u6709\u7528\uff0c\u56e0\u70ba\u5b83\u53ef\u4ee5\u5e6b\u52a9\u6211\u5011\u907f\u514d\u56e0\u70ba NULL \u503c\u800c\u5c0e\u81f4\u7684\u932f\u8aa4\u6216\u4e0d\u5b8c\u6574\u7684\u6578\u64da\u3002<\/p>\n<h2 id=\"isnull-%e5%87%bd%e6%95%b8%e7%9a%84%e8%aa%9e%e6%b3%95-jjXvThXoOK\">ISNULL \u51fd\u6578\u7684\u8a9e\u6cd5<\/h2>\n<pre><code>ISNULL(expression, replacement_value)<\/code><\/pre>\n<ul>\n<li><strong>expression<\/strong>: \u8981\u6aa2\u67e5\u7684\u8868\u9054\u5f0f\uff0c\u53ef\u4ee5\u662f\u4efb\u4f55\u6578\u64da\u985e\u578b\u7684\u6b04\u4f4d\u6216\u503c\u3002<\/li>\n<li><strong>replacement_value<\/strong>: \u7576\u8868\u9054\u5f0f\u70ba NULL \u6642\uff0c\u8fd4\u56de\u7684\u66ff\u4ee3\u503c\u3002<\/li>\n<\/ul>\n<h2 id=\"isnull-%e5%87%bd%e6%95%b8%e7%9a%84%e4%bd%bf%e7%94%a8%e7%af%84%e4%be%8b-jjXvThXoOK\">ISNULL \u51fd\u6578\u7684\u4f7f\u7528\u7bc4\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528 ISNULL \u51fd\u6578\u7684\u7c21\u55ae\u7bc4\u4f8b\u3002\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u6709\u4e00\u500b\u540d\u70ba <code>Employees<\/code> \u7684\u8868\u683c\uff0c\u5176\u4e2d\u5305\u542b\u54e1\u5de5\u7684\u59d3\u540d\u548c\u85aa\u6c34\u3002\u67d0\u4e9b\u54e1\u5de5\u7684\u85aa\u6c34\u53ef\u80fd\u70ba NULL\uff0c\u6211\u5011\u5e0c\u671b\u5728\u67e5\u8a62\u4e2d\u5c07\u9019\u4e9b NULL \u503c\u66ff\u63db\u70ba 0\u3002<\/p>\n<pre><code>SELECT \n    Name, \n    ISNULL(Salary, 0) AS Salary \nFROM \n    Employees;<\/code><\/pre>\n<p>\u5728\u9019\u500b\u67e5\u8a62\u4e2d\uff0c\u7576 <code>Salary<\/code> \u6b04\u4f4d\u70ba NULL \u6642\uff0cISNULL \u51fd\u6578\u6703\u8fd4\u56de 0\uff0c\u5426\u5247\u8fd4\u56de\u539f\u59cb\u7684\u85aa\u6c34\u503c\u3002\u9019\u6a23\uff0c\u6211\u5011\u5c31\u80fd\u5920\u5728\u5831\u544a\u4e2d\u6e05\u695a\u5730\u770b\u5230\u6bcf\u4f4d\u54e1\u5de5\u7684\u85aa\u6c34\uff0c\u800c\u4e0d\u6703\u56e0\u70ba NULL \u503c\u800c\u9020\u6210\u6df7\u6dc6\u3002<\/p>\n<h2 id=\"isnull-%e5%87%bd%e6%95%b8%e7%9a%84%e6%87%89%e7%94%a8%e5%a0%b4%e6%99%af-jjXvThXoOK\">ISNULL \u51fd\u6578\u7684\u61c9\u7528\u5834\u666f<\/h2>\n<p>ISNULL \u51fd\u6578\u5728\u8a31\u591a\u5834\u666f\u4e2d\u90fd\u975e\u5e38\u6709\u7528\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u61c9\u7528\u5834\u666f\uff1a<\/p>\n<ul>\n<li><strong>\u5831\u544a\u751f\u6210<\/strong>: \u5728\u751f\u6210\u5831\u544a\u6642\uff0c\u4f7f\u7528 ISNULL \u51fd\u6578\u53ef\u4ee5\u78ba\u4fdd\u6240\u6709\u6578\u64da\u90fd\u6709\u53ef\u8b80\u6027\uff0c\u907f\u514d\u56e0\u70ba NULL \u503c\u800c\u5c0e\u81f4\u7684\u5831\u544a\u4e0d\u5b8c\u6574\u3002<\/li>\n<li><strong>\u6578\u64da\u6e05\u7406<\/strong>: \u5728\u6578\u64da\u6e05\u7406\u904e\u7a0b\u4e2d\uff0cISNULL \u51fd\u6578\u53ef\u4ee5\u5e6b\u52a9\u6211\u5011\u5feb\u901f\u8b58\u5225\u548c\u8655\u7406 NULL \u503c\uff0c\u5f9e\u800c\u63d0\u9ad8\u6578\u64da\u7684\u8cea\u91cf\u3002<\/li>\n<li><strong>\u8a08\u7b97\u548c\u805a\u5408<\/strong>: \u5728\u9032\u884c\u8a08\u7b97\u6216\u805a\u5408\u64cd\u4f5c\u6642\uff0cNULL \u503c\u53ef\u80fd\u6703\u5f71\u97ff\u7d50\u679c\u3002\u4f7f\u7528 ISNULL \u51fd\u6578\u53ef\u4ee5\u78ba\u4fdd\u8a08\u7b97\u7684\u6e96\u78ba\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-jjXvThXoOK\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u96d6\u7136 ISNULL \u51fd\u6578\u975e\u5e38\u6709\u7528\uff0c\u4f46\u5728\u4f7f\u7528\u6642\u4e5f\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li>ISNULL \u51fd\u6578\u7684\u8fd4\u56de\u985e\u578b\u8207 <code>expression<\/code> \u7684\u985e\u578b\u76f8\u540c\uff0c\u56e0\u6b64\u5728\u4f7f\u7528\u6642\u8981\u78ba\u4fdd\u66ff\u4ee3\u503c\u7684\u985e\u578b\u8207\u539f\u59cb\u503c\u76f8\u5bb9\u3002<\/li>\n<li>\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u4f7f\u7528 COALESCE \u51fd\u6578\u53ef\u80fd\u66f4\u5408\u9069\uff0c\u56e0\u70ba COALESCE \u53ef\u4ee5\u63a5\u53d7\u591a\u500b\u53c3\u6578\u4e26\u8fd4\u56de\u7b2c\u4e00\u500b\u975e NULL \u7684\u503c\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-jjXvThXoOK\">\u7e3d\u7d50<\/h2>\n<p>ISNULL \u51fd\u6578\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u548c\u6578\u64da\u5206\u6790\u5e2b\u6709\u6548\u5730\u8655\u7406 SQL \u4e2d\u7684 NULL \u503c\u3002\u901a\u904e\u4f7f\u7528 ISNULL \u51fd\u6578\uff0c\u6211\u5011\u53ef\u4ee5\u78ba\u4fdd\u67e5\u8a62\u7d50\u679c\u7684\u5b8c\u6574\u6027\u548c\u53ef\u8b80\u6027\uff0c\u5f9e\u800c\u63d0\u9ad8\u6578\u64da\u8655\u7406\u7684\u6548\u7387\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u6578\u64da\u5eab\u61c9\u7528\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u5ba2\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u9084\u662f <a href=\"https:\/\/server.hk\">\u4e91\u4f3a\u670d\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u53ef\u9760\u7684\u670d\u52d9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3 SQL \u4e2d\u7684 ISNULL \u51fd\u6578\uff0c\u5b78\u7fd2\u5982\u4f55\u8655\u7406 NULL \u503c\uff0c\u63d0\u5347\u6578\u64da\u67e5\u8a62\u7684\u9748\u6d3b\u6027\u8207\u6e96\u78ba\u6027\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-168527","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168527","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"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=168527"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168527\/revisions"}],"predecessor-version":[{"id":168528,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168527\/revisions\/168528"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=168527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=168527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=168527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}