{"id":160249,"date":"2024-11-03T21:31:22","date_gmt":"2024-11-03T13:31:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/160249\/"},"modified":"2024-11-03T21:31:23","modified_gmt":"2024-11-03T13:31:23","slug":"oceanbase-%e6%95%99%e7%a8%8b%ef%bc%9aoceanbase-sql-%e5%87%bd%e6%95%b8%e4%b8%ad%e7%9a%84%e7%a9%ba%e5%80%bc","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/160249\/","title":{"rendered":"OceanBase \u6559\u7a0b\uff1aOceanBase SQL \u51fd\u6578\u4e2d\u7684\u7a7a\u503c"},"content":{"rendered":"<h1 id=\"oceanbase-%e6%95%99%e7%a8%8b%ef%bc%9aoceanbase-sql-%e5%87%bd%e6%95%b8%e4%b8%ad%e7%9a%84%e7%a9%ba%e5%80%bc-mzPHhNopgt\">OceanBase \u6559\u7a0b\uff1aOceanBase SQL \u51fd\u6578\u4e2d\u7684\u7a7a\u503c<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\u4e2d\uff0c\u8655\u7406\u7a7a\u503c\uff08NULL\uff09\u662f\u4e00\u500b\u91cd\u8981\u7684\u8ab2\u984c\u3002OceanBase \u4f5c\u70ba\u4e00\u500b\u9ad8\u6027\u80fd\u7684\u5206\u4f48\u5f0f\u6578\u64da\u5eab\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e SQL \u51fd\u6578\u4f86\u8655\u7406\u7a7a\u503c\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e OceanBase \u4e2d\u7684\u7a7a\u503c\u6982\u5ff5\u53ca\u5176\u76f8\u95dc\u7684 SQL \u51fd\u6578\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528\u9019\u4e9b\u529f\u80fd\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e7%a9%ba%e5%80%bc%ef%bc%88null%ef%bc%89%ef%bc%9f-mzPHhNopgt\">\u4ec0\u9ebc\u662f\u7a7a\u503c\uff08NULL\uff09\uff1f<\/h2>\n<p>\u5728 SQL \u4e2d\uff0c\u7a7a\u503c\uff08NULL\uff09\u8868\u793a\u7f3a\u5931\u6216\u672a\u77e5\u7684\u6578\u64da\u3002\u5b83\u8207\u96f6\uff080\uff09\u6216\u7a7a\u5b57\u7b26\u4e32\uff08&#8221;&#8221;\uff09\u4e0d\u540c\uff0c\u56e0\u70ba\u9019\u4e9b\u503c\u662f\u5177\u9ad4\u7684\uff0c\u800c NULL \u5247\u8868\u793a\u6c92\u6709\u503c\u3002\u7406\u89e3\u7a7a\u503c\u7684\u6982\u5ff5\u5c0d\u65bc\u6578\u64da\u5eab\u64cd\u4f5c\u81f3\u95dc\u91cd\u8981\uff0c\u56e0\u70ba\u5b83\u6703\u5f71\u97ff\u67e5\u8a62\u7d50\u679c\u548c\u6578\u64da\u8655\u7406\u908f\u8f2f\u3002<\/p>\n<h2 id=\"oceanbase-%e4%b8%ad%e7%9a%84%e7%a9%ba%e5%80%bc%e8%99%95%e7%90%86-mzPHhNopgt\">OceanBase \u4e2d\u7684\u7a7a\u503c\u8655\u7406<\/h2>\n<p>\u5728 OceanBase \u4e2d\uff0c\u7a7a\u503c\u7684\u8655\u7406\u4e3b\u8981\u4f9d\u8cf4\u65bc SQL \u51fd\u6578\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684 SQL \u51fd\u6578\uff0c\u7528\u65bc\u8655\u7406\u7a7a\u503c\uff1a<\/p>\n<h3 id=\"1-is-null-%e5%92%8c-is-not-null-mzPHhNopgt\">1. IS NULL \u548c IS NOT NULL<\/h3>\n<p>\u9019\u5169\u500b\u689d\u4ef6\u7528\u65bc\u6aa2\u67e5\u67d0\u500b\u5b57\u6bb5\u662f\u5426\u70ba\u7a7a\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>SELECT * FROM users WHERE email IS NULL;<\/code><\/pre>\n<p>\u4e0a\u8ff0\u67e5\u8a62\u5c07\u8fd4\u56de\u6240\u6709\u96fb\u5b50\u90f5\u4ef6\u5730\u5740\u70ba\u7a7a\u7684\u7528\u6236\u3002<\/p>\n<h3 id=\"2-coalesce-%e5%87%bd%e6%95%b8-mzPHhNopgt\">2. COALESCE \u51fd\u6578<\/h3>\n<p>COALESCE \u51fd\u6578\u8fd4\u56de\u5176\u53c3\u6578\u4e2d\u7b2c\u4e00\u500b\u975e\u7a7a\u503c\u3002\u9019\u5728\u9700\u8981\u63d0\u4f9b\u9ed8\u8a8d\u503c\u6642\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>SELECT COALESCE(phone, '\u672a\u63d0\u4f9b') AS contact_number FROM users;<\/code><\/pre>\n<p>\u9019\u5c07\u8fd4\u56de\u7528\u6236\u7684\u96fb\u8a71\u865f\u78bc\uff0c\u5982\u679c\u96fb\u8a71\u865f\u78bc\u70ba\u7a7a\uff0c\u5247\u8fd4\u56de\u201c\u672a\u63d0\u4f9b\u201d\u3002<\/p>\n<h3 id=\"3-nullif-%e5%87%bd%e6%95%b8-mzPHhNopgt\">3. NULLIF \u51fd\u6578<\/h3>\n<p>NULLIF \u51fd\u6578\u6bd4\u8f03\u5169\u500b\u8868\u9054\u5f0f\uff0c\u5982\u679c\u5b83\u5011\u76f8\u7b49\uff0c\u5247\u8fd4\u56de NULL\uff0c\u5426\u5247\u8fd4\u56de\u7b2c\u4e00\u500b\u8868\u9054\u5f0f\u3002\u9019\u5728\u907f\u514d\u7279\u5b9a\u503c\u6642\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>SELECT NULLIF(salary, 0) AS adjusted_salary FROM employees;<\/code><\/pre>\n<p>\u5982\u679c\u85aa\u6c34\u70ba 0\uff0c\u5247\u8fd4\u56de NULL\uff0c\u5426\u5247\u8fd4\u56de\u85aa\u6c34\u7684\u5be6\u969b\u503c\u3002<\/p>\n<h3 id=\"4-ifnull-%e5%87%bd%e6%95%b8-mzPHhNopgt\">4. IFNULL \u51fd\u6578<\/h3>\n<p>IFNULL \u51fd\u6578\u7528\u65bc\u6aa2\u67e5\u4e00\u500b\u8868\u9054\u5f0f\u662f\u5426\u70ba\u7a7a\u503c\uff0c\u5982\u679c\u662f\uff0c\u5247\u8fd4\u56de\u7b2c\u4e8c\u500b\u53c3\u6578\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>SELECT IFNULL(bonus, 100) AS final_bonus FROM employees;<\/code><\/pre>\n<p>\u5982\u679c\u734e\u91d1\u70ba\u7a7a\uff0c\u5247\u8fd4\u56de 100\uff0c\u5426\u5247\u8fd4\u56de\u734e\u91d1\u7684\u5be6\u969b\u503c\u3002<\/p>\n<h2 id=\"%e7%a9%ba%e5%80%bc%e7%9a%84%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-mzPHhNopgt\">\u7a7a\u503c\u7684\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u5728\u4f7f\u7528\u7a7a\u503c\u6642\uff0c\u958b\u767c\u8005\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li>\u7a7a\u503c\u4e0d\u7b49\u65bc\u4efb\u4f55\u503c\uff0c\u5305\u62ec\u81ea\u8eab\u3002\u56e0\u6b64\uff0c\u4f7f\u7528\u7b49\u865f\uff08=\uff09\u6bd4\u8f03\u7a7a\u503c\u6642\uff0c\u7d50\u679c\u5c07\u59cb\u7d42\u70ba\u5047\u3002<\/li>\n<li>\u5728\u9032\u884c\u805a\u5408\u8a08\u7b97\u6642\uff0c\u7a7a\u503c\u6703\u88ab\u5ffd\u7565\u3002\u4f8b\u5982\uff0c\u8a08\u7b97\u5e73\u5747\u503c\u6642\uff0c\u7a7a\u503c\u4e0d\u6703\u8a08\u5165\u8a08\u7b97\u3002<\/li>\n<li>\u5728\u4f7f\u7528 JOIN \u64cd\u4f5c\u6642\uff0c\u7a7a\u503c\u53ef\u80fd\u6703\u5f71\u97ff\u7d50\u679c\u96c6\u7684\u8fd4\u56de\uff0c\u7279\u5225\u662f\u5728\u4f7f\u7528\u5916\u90e8\u9023\u63a5\u6642\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b5%90%e8%ab%96-mzPHhNopgt\">\u7d50\u8ad6<\/h2>\n<p>\u5728 OceanBase \u4e2d\uff0c\u6b63\u78ba\u8655\u7406\u7a7a\u503c\u662f\u78ba\u4fdd\u6578\u64da\u6e96\u78ba\u6027\u548c\u5b8c\u6574\u6027\u7684\u95dc\u9375\u3002\u901a\u904e\u4f7f\u7528\u4e0a\u8ff0 SQL \u51fd\u6578\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u548c\u64cd\u4f5c\u7a7a\u503c\uff0c\u5f9e\u800c\u63d0\u9ad8\u6578\u64da\u5eab\u7684\u6027\u80fd\u548c\u53ef\u9760\u6027\u3002\u4e86\u89e3\u9019\u4e9b\u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\u5c07\u6709\u52a9\u65bc\u958b\u767c\u8005\u5728\u5be6\u969b\u61c9\u7528\u4e2d\u66f4\u597d\u5730\u8655\u7406\u6578\u64da\u3002<\/p>\n<p>\u5982\u9700\u9032\u4e00\u6b65\u4e86\u89e3 OceanBase \u6216\u5176\u4ed6\u6578\u64da\u5eab\u89e3\u6c7a\u65b9\u6848\uff0c\u60a8\u53ef\u4ee5\u8a2a\u554f <a href=\"https:\/\/server.hk\">Server.HK<\/a>\uff0c\u6211\u5011\u63d0\u4f9b\u5404\u7a2e <a href=\"https:\/\/server.hk\">VPS<\/a> \u548c <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u670d\u52d9\uff0c\u5e6b\u52a9\u60a8\u6eff\u8db3\u4e0d\u540c\u7684\u696d\u52d9\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22 OceanBase \u6559\u7a0b\uff0c\u6df1\u5165\u4e86\u89e3 OceanBase SQL \u51fd\u6578\u4e2d\u7684\u7a7a\u503c\u8655\u7406\uff0c\u63d0\u5347\u6578\u64da\u5eab\u64cd\u4f5c\u6548\u7387\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-160249","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/160249","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=160249"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/160249\/revisions"}],"predecessor-version":[{"id":160250,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/160249\/revisions\/160250"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=160249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=160249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=160249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}