{"id":160581,"date":"2024-11-03T23:17:23","date_gmt":"2024-11-03T15:17:23","guid":{"rendered":"https:\/\/server.hk\/cnblog\/160581\/"},"modified":"2024-11-03T23:17:24","modified_gmt":"2024-11-03T15:17:24","slug":"oceanbase-%e6%95%99%e7%a8%8b%ef%bc%9aoceanbase-%e6%a2%9d%e4%bb%b6%e5%88%a4%e6%96%b7%e8%a1%a8%e9%81%94%e5%bc%8f%e4%b8%ad%e7%9a%84%e7%a9%ba%e5%80%bc","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/160581\/","title":{"rendered":"OceanBase \u6559\u7a0b\uff1aOceanBase \u689d\u4ef6\u5224\u65b7\u8868\u9054\u5f0f\u4e2d\u7684\u7a7a\u503c"},"content":{"rendered":"<h1 id=\"oceanbase-%e6%95%99%e7%a8%8b%ef%bc%9aoceanbase-%e6%a2%9d%e4%bb%b6%e5%88%a4%e6%96%b7%e8%a1%a8%e9%81%94%e5%bc%8f%e4%b8%ad%e7%9a%84%e7%a9%ba%e5%80%bc-tvNezKnGjT\">OceanBase \u6559\u7a0b\uff1aOceanBase \u689d\u4ef6\u5224\u65b7\u8868\u9054\u5f0f\u4e2d\u7684\u7a7a\u503c<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\u4e2d\uff0cOceanBase \u4f5c\u70ba\u4e00\u500b\u9ad8\u6027\u80fd\u7684\u5206\u4f48\u5f0f\u6578\u64da\u5eab\uff0c\u8d8a\u4f86\u8d8a\u53d7\u5230\u958b\u767c\u8005\u7684\u9752\u775e\u3002\u7279\u5225\u662f\u5728\u8655\u7406\u5927\u898f\u6a21\u6578\u64da\u6642\uff0cOceanBase \u7684\u6027\u80fd\u548c\u64f4\u5c55\u6027\u8868\u73fe\u51fa\u8272\u3002\u7136\u800c\uff0c\u5728\u4f7f\u7528 OceanBase \u9032\u884c\u6578\u64da\u67e5\u8a62\u548c\u64cd\u4f5c\u6642\uff0c\u7a7a\u503c\uff08NULL\uff09\u8655\u7406\u662f\u4e00\u500b\u91cd\u8981\u7684\u8ab2\u984c\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e OceanBase \u4e2d\u7684\u689d\u4ef6\u5224\u65b7\u8868\u9054\u5f0f\u53ca\u5176\u7a7a\u503c\u7684\u8655\u7406\u65b9\u5f0f\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-tvNezKnGjT\">\u4ec0\u9ebc\u662f\u7a7a\u503c\uff08NULL\uff09\uff1f<\/h2>\n<p>\u5728\u6578\u64da\u5eab\u4e2d\uff0c\u7a7a\u503c\uff08NULL\uff09\u8868\u793a\u4e00\u500b\u7f3a\u5931\u7684\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\u201c\u7121\u503c\u201d\u3002\u5728 OceanBase \u4e2d\uff0c\u6b63\u78ba\u8655\u7406\u7a7a\u503c\u5c0d\u65bc\u78ba\u4fdd\u6578\u64da\u7684\u6e96\u78ba\u6027\u548c\u5b8c\u6574\u6027\u81f3\u95dc\u91cd\u8981\u3002<\/p>\n<h2 id=\"oceanbase-%e4%b8%ad%e7%9a%84%e7%a9%ba%e5%80%bc%e8%99%95%e7%90%86-tvNezKnGjT\">OceanBase \u4e2d\u7684\u7a7a\u503c\u8655\u7406<\/h2>\n<p>\u5728 OceanBase \u4e2d\uff0c\u7a7a\u503c\u7684\u8655\u7406\u4e3b\u8981\u6d89\u53ca\u5230 SQL \u67e5\u8a62\u4e2d\u7684\u689d\u4ef6\u5224\u65b7\u8868\u9054\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u7a7a\u503c\u8655\u7406\u65b9\u6cd5\uff1a<\/p>\n<h3 id=\"1-%e4%bd%bf%e7%94%a8-is-null-%e5%92%8c-is-not-null-tvNezKnGjT\">1. \u4f7f\u7528 IS NULL \u548c IS NOT NULL<\/h3>\n<p>\u5728 SQL \u67e5\u8a62\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>IS NULL<\/code> \u548c <code>IS NOT NULL<\/code> \u4f86\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-%e4%bd%bf%e7%94%a8-coalesce-%e5%87%bd%e6%95%b8-tvNezKnGjT\">2. \u4f7f\u7528 COALESCE \u51fd\u6578<\/h3>\n<p><code>COALESCE<\/code> \u51fd\u6578\u53ef\u4ee5\u7528\u4f86\u8fd4\u56de\u7b2c\u4e00\u500b\u975e\u7a7a\u503c\u3002\u9019\u5728\u8655\u7406\u591a\u500b\u53ef\u80fd\u70ba\u7a7a\u7684\u5b57\u6bb5\u6642\u7279\u5225\u6709\u7528\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>SELECT COALESCE(phone, '\u7121\u96fb\u8a71') AS contact_phone 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\u7121\u96fb\u8a71\u201d\u3002<\/p>\n<h3 id=\"3-%e4%bd%bf%e7%94%a8-nullif-%e5%87%bd%e6%95%b8-tvNezKnGjT\">3. \u4f7f\u7528 NULLIF \u51fd\u6578<\/h3>\n<p><code>NULLIF<\/code> \u51fd\u6578\u7528\u65bc\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\u9700\u8981\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\u5be6\u969b\u85aa\u6c34\u3002<\/p>\n<h2 id=\"%e7%a9%ba%e5%80%bc%e5%9c%a8%e6%a2%9d%e4%bb%b6%e5%88%a4%e6%96%b7%e4%b8%ad%e7%9a%84%e5%bd%b1%e9%9f%bf-tvNezKnGjT\">\u7a7a\u503c\u5728\u689d\u4ef6\u5224\u65b7\u4e2d\u7684\u5f71\u97ff<\/h2>\n<p>\u5728\u689d\u4ef6\u5224\u65b7\u4e2d\uff0c\u7a7a\u503c\u7684\u5b58\u5728\u53ef\u80fd\u6703\u5c0e\u81f4\u610f\u60f3\u4e0d\u5230\u7684\u7d50\u679c\u3002\u4f8b\u5982\uff0c\u5728\u4f7f\u7528 <code>AND<\/code> \u548c <code>OR<\/code> \u9032\u884c\u908f\u8f2f\u904b\u7b97\u6642\uff0c\u7a7a\u503c\u53ef\u80fd\u6703\u5f71\u97ff\u6574\u500b\u8868\u9054\u5f0f\u7684\u8a55\u4f30\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u4f8b\uff1a<\/p>\n<pre><code>SELECT * FROM orders WHERE status = 'completed' AND delivery_date IS NULL;<\/code><\/pre>\n<p>\u9019\u689d\u67e5\u8a62\u5c07\u8fd4\u56de\u6240\u6709\u72c0\u614b\u70ba\u201c\u5df2\u5b8c\u6210\u201d\u4e14\u4ea4\u4ed8\u65e5\u671f\u70ba\u7a7a\u7684\u8a02\u55ae\u3002\u5982\u679c\u4ea4\u4ed8\u65e5\u671f\u4e0d\u70ba\u7a7a\uff0c\u5247\u8a72\u689d\u4ef6\u4e0d\u6703\u88ab\u6eff\u8db3\uff0c\u53ef\u80fd\u5c0e\u81f4\u932f\u904e\u4e00\u4e9b\u91cd\u8981\u7684\u6578\u64da\u3002<\/p>\n<h2 id=\"%e7%b5%90%e8%ab%96-tvNezKnGjT\">\u7d50\u8ad6<\/h2>\n<p>\u5728 OceanBase \u4e2d\uff0c\u6b63\u78ba\u8655\u7406\u7a7a\u503c\u662f\u78ba\u4fdd\u6578\u64da\u6e96\u78ba\u6027\u548c\u67e5\u8a62\u6709\u6548\u6027\u7684\u95dc\u9375\u3002\u901a\u904e\u4f7f\u7528 <code>IS NULL<\/code>\u3001<code>COALESCE<\/code> \u548c <code>NULLIF<\/code> \u7b49\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\u6574\u9ad4\u6027\u80fd\u548c\u53ef\u9760\u6027\u3002<\/p>\n<p>\u5982\u679c\u60a8\u5c0d\u65bc\u5982\u4f55\u5728 OceanBase \u4e2d\u9032\u884c\u66f4\u6df1\u5165\u7684\u5b78\u7fd2\u6216\u9700\u8981\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0c\u6b61\u8fce\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u8cc7\u8a0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3 OceanBase \u4e2d\u689d\u4ef6\u5224\u65b7\u8868\u9054\u5f0f\u7684\u7a7a\u503c\u8655\u7406\uff0c\u638c\u63e1\u5982\u4f55\u6709\u6548\u7ba1\u7406\u548c\u4f7f\u7528\u7a7a\u503c\u4ee5\u63d0\u5347\u6578\u64da\u67e5\u8a62\u6548\u7387\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-160581","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/160581","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=160581"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/160581\/revisions"}],"predecessor-version":[{"id":160582,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/160581\/revisions\/160582"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=160581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=160581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=160581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}