{"id":5639,"date":"2024-11-14T13:43:01","date_gmt":"2024-11-14T05:43:01","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5639\/"},"modified":"2024-11-14T13:43:01","modified_gmt":"2024-11-14T05:43:01","slug":"sql%e8%af%ad%e5%8f%a5%e6%80%8e%e4%b9%88%e6%9f%a5%e8%af%a2%e7%a9%ba%e5%80%bc","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5639\/","title":{"rendered":"sql\u8bed\u53e5\u600e\u4e48\u67e5\u8be2\u7a7a\u503c"},"content":{"rendered":"<blockquote><p>\n  \u5728 sql \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u53e5\u67e5\u8be2\u7a7a\u503c\uff1ais null\uff1a\u8fd4\u56de column_name \u4e3a\u7a7a\u7684\u6240\u6709\u884c\u3002is not null\uff1a\u8fd4\u56de column_name \u4e0d\u4e3a\u7a7a\u7684\u6240\u6709\u884c\u3002coalesce() \u51fd\u6570\uff1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u975e\u7a7a\u503c\uff0c\u5982\u679c\u6240\u6709\u503c\u90fd\u4e3a\u7a7a\uff0c\u5219\u8fd4\u56de\u9ed8\u8ba4\u503c\u3002nvl() \u51fd\u6570\uff1a\u8fd4\u56de\u8981\u68c0\u67e5\u7684\u503c\uff0c\u5982\u679c\u4e3a\u7a7a\u5219\u8fd4\u56de\u9ed8\u8ba4\u503c\u3002nullif() \u51fd\u6570\uff1a\u5982\u679c\u4e24\u4e2a\u503c\u76f8\u7b49\uff0c\u5219\u8fd4\u56de null\uff0c\u5426\u5219\u8fd4\u56de\u7b2c\u4e00\u4e2a\u503c\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/30\/2024053019311388956.jpg\" class=\"aligncenter\" title=\"sql\u8bed\u53e5\u600e\u4e48\u67e5\u8be2\u7a7a\u503c\u63d2\u56fe\" alt=\"sql\u8bed\u53e5\u600e\u4e48\u67e5\u8be2\u7a7a\u503c\u63d2\u56fe\" \/><\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 SQL \u8bed\u53e5\u67e5\u8be2\u7a7a\u503c<\/h2>\n<p>\u5728 SQL \u4e2d\uff0c\u7a7a\u503c\u8868\u793a\u6570\u636e\u5e93\u4e2d\u4e0d\u5b58\u5728\u6570\u636e\u6216\u6570\u636e\u672a\u77e5\u3002\u67e5\u8be2\u7a7a\u503c\u5bf9\u4e8e\u786e\u5b9a\u7f3a\u5931\u7684\u6570\u636e\u6216\u9a8c\u8bc1\u6570\u636e\u5b8c\u6574\u6027\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u67e5\u8be2\u7a7a\u503c\u7684\u5e38\u7528 SQL \u8bed\u53e5\uff1a<\/p>\n<p><strong>1. IS NULL<\/strong><\/p>\n<pre>SELECT * FROM table_name WHERE column_name IS NULL;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u67e5\u8be2\u5c06\u8fd4\u56de column_name \u4e3a\u7a7a\u7684\u6240\u6709\u884c\u3002<\/p>\n<p><strong>2. IS NOT NULL<\/strong><\/p>\n<pre>SELECT * FROM table_name WHERE column_name IS NOT NULL;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u67e5\u8be2\u5c06\u8fd4\u56de column_name \u4e0d\u4e3a\u7a7a\u7684\u6240\u6709\u884c\u3002<\/p>\n<p><strong>3. COALESCE() \u51fd\u6570<\/strong><\/p>\n<p>COALESCE() \u51fd\u6570\u53ef\u7528\u4e8e\u5c06\u7b2c\u4e00\u4e2a\u975e\u7a7a\u503c\u4f5c\u4e3a\u7ed3\u679c\u8fd4\u56de\u3002\u5982\u679c\u6240\u6709\u503c\u90fd\u4e3a\u7a7a\uff0c\u5219\u8fd4\u56de\u6700\u540e\u4e00\u4e2a\u6307\u5b9a\u7684\u9ed8\u8ba4\u503c\u3002<\/p>\n<pre>SELECT COALESCE(column1, column2, 'default_value') FROM table_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. NVL() \u51fd\u6570<\/strong><\/p>\n<p>NVL() \u51fd\u6570\u7c7b\u4f3c\u4e8e COALESCE()\uff0c\u4f46\u5b83\u4ec5\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1a\u8981\u68c0\u67e5\u7684\u503c\u548c\u9ed8\u8ba4\u503c\u3002<\/p>\n<pre>SELECT NVL(column_name, 'default_value') FROM table_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>5. NULLIF() \u51fd\u6570<\/strong><\/p>\n<p>NULLIF() \u51fd\u6570\u53ef\u7528\u4e8e\u68c0\u67e5\u4e24\u4e2a\u503c\u662f\u5426\u76f8\u7b49\u3002\u5982\u679c\u76f8\u7b49\uff0c\u5219\u8fd4\u56de NULL\uff1b\u5426\u5219\uff0c\u8fd4\u56de\u7b2c\u4e00\u4e2a\u503c\u3002<\/p>\n<pre>SELECT NULLIF(column1, column2) FROM table_name;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u67e5\u8be2\u7a7a\u503c\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 sql \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u53e5\u67e5\u8be2\u7a7a\u503c\uff1ais null\uff1a\u8fd4\u56de column_name \u4e3a\u7a7a\u7684\u6240\u6709\u884c\u3002is not null\uff1a\u8fd4\u56de column_name \u4e0d\u4e3a\u7a7a\u7684\u6240\u6709\u884c\u3002coalesce() \u51fd\u6570\uff1a\u8fd4\u56de\u7b2c\u4e00\u4e2a\u975e\u7a7a\u503c\uff0c\u5982\u679c\u6240\u6709\u503c\u90fd\u4e3a\u7a7a\uff0c\u5219\u8fd4\u56de\u9ed8\u8ba4\u503c\u3002nvl() \u51fd\u6570\uff1a\u8fd4\u56de\u8981\u68c0\u67e5\u7684\u503c\uff0c\u5982\u679c\u4e3a\u7a7a\u5219\u8fd4\u56de\u9ed8\u8ba4\u503c\u3002nullif() \u51fd\u6570\uff1a\u5982\u679c\u4e24\u4e2a\u503c\u76f8\u7b49\uff0c\u5219\u8fd4\u56de null\uff0c\u5426\u5219\u8fd4\u56de\u7b2c\u4e00\u4e2a\u503c\u3002 \u5982\u4f55\u4f7f\u7528 SQL \u8bed\u53e5\u67e5\u8be2\u7a7a\u503c \u5728 SQL \u4e2d\uff0c\u7a7a\u503c\u8868\u793a\u6570\u636e\u5e93\u4e2d\u4e0d\u5b58\u5728\u6570\u636e\u6216\u6570\u636e\u672a\u77e5\u3002\u67e5\u8be2\u7a7a\u503c\u5bf9\u4e8e\u786e\u5b9a\u7f3a\u5931\u7684\u6570\u636e\u6216\u9a8c\u8bc1\u6570\u636e\u5b8c\u6574\u6027\u975e\u5e38\u6709\u7528\u3002 \u4ee5\u4e0b\u662f\u67e5\u8be2\u7a7a\u503c\u7684\u5e38\u7528 SQL \u8bed\u53e5\uff1a 1. IS NULL SELECT * FROM table_name WHERE column_name IS NULL; \u767b\u5f55\u540e\u590d\u5236 \u6b64\u67e5\u8be2\u5c06\u8fd4\u56de column_name \u4e3a\u7a7a\u7684\u6240\u6709\u884c\u3002 2. IS NOT NULL SELECT * FROM table_name WHERE column_name IS NOT NULL; \u767b\u5f55\u540e\u590d\u5236 \u6b64\u67e5\u8be2\u5c06\u8fd4\u56de column_name \u4e0d\u4e3a\u7a7a\u7684\u6240\u6709\u884c\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-5639","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5639","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=5639"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5639\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5639"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5639"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}