{"id":12749,"date":"2024-09-10T11:10:13","date_gmt":"2024-09-10T03:10:13","guid":{"rendered":"https:\/\/www.gbase8.cn\/?p=12749"},"modified":"2024-09-10T14:54:08","modified_gmt":"2024-09-10T06:54:08","slug":"gbase-8a-%e5%8f%af%e8%83%bd%e6%9c%89%e6%95%88%e7%9a%84or%e4%bc%98%e5%8c%96","status":"publish","type":"post","link":"https:\/\/www.gbase8.cn\/en\/12749","title":{"rendered":"GBase 8a \u53ef\u80fd\u6709\u6548\u7684OR\u4f18\u5316"},"content":{"rendered":"<p>\u4e1a\u52a1SQL\u4e2d\u5305\u542bOR, \u5982\u679c\u53d1\u73b0\u6027\u80fd\u5bf9\u6bd4\u6ca1\u6709\u8fd9\u4e2aOR\u4e0b\u964d\u6781\u5927\uff0c\u53ef\u4ee5\u8003\u8651\u672c\u6587\u4ecb\u7ecd\u7684OR\u4f18\u5316\u8bd5\u8bd5\u3002\u5305\u62ec\u5728\u8fc7\u6ee4\u6761\u4ef6\u91cc\u7684OR\u4ee5\u53caJOIN\u6761\u4ef6\u91cc\u7684OR\u4e24\u79cd\u60c5\u51b5\u3002<\/p>\n\n\n\n<p>\u6240\u6709\u6539\u5199\u90fd\u662f\u5728\u9ed8\u8ba4\u6267\u884c\u8ba1\u5212\u7684\u4f18\u5316\u6548\u679c\u4e0d\u597d\u65f6\uff0c\u770b\u770b\u8fd9\u4e2a\u624b\u5de5\u6307\u5b9a\u7684\u4f18\u5316\u662f\u5426\u66f4\u597d\u4e00\u4e9b\u3002\u6027\u80fd\u662f\u5426\u63d0\u9ad8\u4ee5\u53ca\u6700\u7ec8\u7ed3\u679c\u662f\u5426\u6b63\u786e\uff0c\u90fd\u5efa\u8bae\u591a\u52a0\u9a8c\u8bc1\u3002<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Directory Navigation<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2ab661a283f\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2ab661a283f\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.gbase8.cn\/en\/12749\/#%E8%BF%87%E6%BB%A4%E6%9D%A1%E4%BB%B6\" >\u8fc7\u6ee4\u6761\u4ef6<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.gbase8.cn\/en\/12749\/#AXX_OR_AYY_%E4%BB%A5%E5%8F%8A_A_in_XXYY%E7%9A%84%E5%86%99%E6%B3%95\" >A=XX OR A=YY \u4ee5\u53ca A in (XX,YY)\u7684\u5199\u6cd5<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.gbase8.cn\/en\/12749\/#AXX_OR_BYY_or_CZZ\" >A=XX OR B=YY or C=ZZ<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.gbase8.cn\/en\/12749\/#JOIN%E6%9D%A1%E4%BB%B6_%E8%A6%81%E6%B1%82%E7%BB%93%E6%9E%9C%E9%9B%86%E6%B2%A1%E6%9C%89%E8%86%A8%E8%83%80\" >JOIN\u6761\u4ef6 \u8981\u6c42\u7ed3\u679c\u96c6\u6ca1\u6709\u81a8\u80c0<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.gbase8.cn\/en\/12749\/#%E6%80%BB%E7%BB%93\" >Summary<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E8%BF%87%E6%BB%A4%E6%9D%A1%E4%BB%B6\"><\/span>\u8fc7\u6ee4\u6761\u4ef6<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u5206\u4e3a\u4e24\u79cd\u60c5\u51b5\uff0c\u540c\u4e00\u4e2a\u5217\u4e0d\u540c\u7684\u503c\uff0c\u4ee5\u53ca\u4e0d\u540c\u7684\u5217\u4e0d\u540c\u7684\u503c(\u524d\u8005\u7b97\u540e\u8005\u7684\u7279\u6b8a\u60c5\u51b5\uff0c\u4f46\u66f4\u5e38\u89c1\u3002\u6838\u5fc3\u5c31\u662f\u624b\u5de5\u5c06\u591a\u4e2aOR\u5224\u65ad\uff0c\u6539\u62101\u4e2a\u5224\u65ad\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"AXX_OR_AYY_%E4%BB%A5%E5%8F%8A_A_in_XXYY%E7%9A%84%E5%86%99%E6%B3%95\"><\/span>A=XX OR A=YY \u4ee5\u53ca A in (XX,YY)\u7684\u5199\u6cd5<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u6539\u5199\u6210\u9002\u5408\u6570\u5b57\u7684\u5199\u6cd5<br>decode(A,XX,XX,YY,XX,'')=XX<br>\u4e5f\u5c31\u662f\u628aYY\u503c\u6539\u6210XX,\u53d8\u6210\u90fd\u5224\u65ad\u7b49\u503cXX<\/p>\n\n\n\n<p>\u6539\u6210case\u7684\u5199\u6cd5\uff0c\u9002\u5408\u5b57\u7b26<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>case A when XX then XX when YY then XX end)=XX<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>gbase&gt; select * from t3 where id=1 or id=11;\n+------+------+\n| id   | id2  |\n+------+------+\n|    1 |    1 |\n|   11 |   11 |\n+------+------+\n2 rows in set (Elapsed: 00:00:00.07)\n\ngbase&gt; select * from t3 where decode(id,1,1,11,1)=1;\n+------+------+\n| id   | id2  |\n+------+------+\n|    1 |    1 |\n|   11 |   11 |\n+------+------+\n2 rows in set (Elapsed: 00:00:00.08)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"AXX_OR_BYY_or_CZZ\"><\/span>A=XX OR B=YY or C=ZZ<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>\u6539\u5199\u6210<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>concat(case when a=XX then XX else '' end,case when B=YY then XX else '' end,case when C=ZZ then XX else '' end) like 'XX%'<\/code><\/pre>\n\n\n\n<p>\u5176\u4e2d\u7684XX,YY\u53ef\u4ee5\u662f\u76f8\u540c\u503c\u6216\u8005\u4e0d\u540c\u503c\u3002 \u6838\u5fc3\u4e5f\u662f\u5c06\u591a\u4e2aOR\u5224\u65ad\uff0c\u6539\u62101\u4e2a\u5224\u65ad\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>gbase&gt; select * from t3 where id=111 or id2=9999;\n+------+------+\n| id   | id2  |\n+------+------+\n|  111 | 1111 |\n+------+------+\n1 row in set (Elapsed: 00:00:00.04)\n\ngbase&gt; select * from t3 where concat(case when id=111 then 111 else '' end,case when id2=9999 then 111 else '' end) like '111%';\n+------+------+\n| id   | id2  |\n+------+------+\n|  111 | 1111 |\n+------+------+\n1 row in set (Elapsed: 00:00:00.09)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"JOIN%E6%9D%A1%E4%BB%B6_%E8%A6%81%E6%B1%82%E7%BB%93%E6%9E%9C%E9%9B%86%E6%B2%A1%E6%9C%89%E8%86%A8%E8%83%80\"><\/span>JOIN\u6761\u4ef6 \u8981\u6c42\u7ed3\u679c\u96c6\u6ca1\u6709\u81a8\u80c0<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u5982\u4e0b\u7684JOIN\u91cc\uff0cOR\u6761\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select b.XX from a \nleft join b on (a.id=b.id or a.name=b.name) and ...\u5176\u5b83join\u6761\u4ef6<\/code><\/pre>\n\n\n\n<p>\u6539\u5199\u62102\u4e2ajoin<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>select coalesce(b.XX,b2.XX) from a\nleft join b on a.id=b.id and \u2026\u5176\u5b83join\u6761\u4ef6\nleft join b b2 on a.id&lt;&gt;b2.id and a.name=b2.name and \u2026\u5176\u5b83join\u6761\u4ef6<\/code><\/pre>\n\n\n\n<p>\u8981\u6c42\u7ed3\u679c\u96c6\u6ca1\u6709\u81a8\u80c0\uff0c\u6bd4\u5982a\u88681\u4e07\u884c\uff0c\u7ed3\u679c\u96c6\u4f9d\u7136\u662f\u5de6\u8868\u76841\u4e07\u884c\u3002\u4e5f\u5c31\u662f1\u5bf91\u7684\u573a\u666f\uff0c\u53f3\u8868\u4e0d\u80fd\u51fa\u73b0\u91cd\u590d\u503c\uff0c\u5bfc\u81f4\u5de6\u8868\u81a8\u80c0\u3002<br>\u5426\u5219\u56e0\u5de6\u8868\u51fa\u73b0\u91cd\u590d\uff0c1\uff1an\uff0c\u5bfc\u81f4\u6539\u5199\u540e\u7684\u65b9\u6848\u7ed3\u679c\u96c6\u884c\u6570\u5c11\u4e86\u3002<\/p>\n\n\n\n<p>\u800cselect\u90e8\u5206\u7528\u5230b\u8868\u7684\u5217\uff0c\u9700\u8981\u6839\u636eb\u548cb2\u7684\u5217\u8fdb\u884c\u9009\u62e9,\u6bd4\u5982b.XX \u53d8\u6210 coalesce(b.XX,b2.XX)<\/p>\n\n\n\n<pre class=\"wp-block-code has-small-font-size\"><code>gbase&gt; select * from t1;\n+------+------+\n| id   | id2  |\n+------+------+\n|    1 |   66 |\n|    2 |   77 |\n+------+------+\n2 rows in set (Elapsed: 00:00:00.06)\n\ngbase&gt; select * from t3;\n+------+------+\n| id   | id2  |\n+------+------+\n|    1 |   66 |\n|    3 |   77 |\n|    9 |   99 |\n+------+------+\n3 rows in set (Elapsed: 00:00:00.09)\n\ngbase&gt; select * from t1 left join t3 on t1.id=t3.id or t1.id2=t3.id2;\n+------+------+------+------+\n| id   | id2  | id   | id2  |\n+------+------+------+------+\n|    1 |   66 |    1 |   66 |\n|    2 |   77 |    3 |   77 |\n+------+------+------+------+\n2 rows in set (Elapsed: 00:00:00.28)\n\ngbase&gt; select * from t1 left join t3 on t1.id=t3.id left join t3 t3_2 on t1.id&lt;&gt;t3_2.id and t1.id2=t3_2.id2;\n+------+------+------+------+------+------+\n| id   | id2  | id   | id2  | id   | id2  |\n+------+------+------+------+------+------+\n|    2 |   77 | NULL | NULL |    3 |   77 |\n|    1 |   66 |    1 |   66 | NULL | NULL |\n+------+------+------+------+------+------+\n2 rows in set (Elapsed: 00:00:00.28)\n\ngbase&gt; select t1.id,t1.id2,coalesce(t3.id,t3_2.id)id,coalesce(t3.id2,t3_2.id2)id2  from t1 left join t3 on t1.id=t3.id left join t3 t3_2 on t1.id&lt;&gt;t3_2.id and t1.id2=t3_2.id2;\n+------+------+------+------+\n| id   | id2  | id   | id2  |\n+------+------+------+------+\n|    2 |   77 |    3 |   77 |\n|    1 |   66 |    1 |   66 |\n+------+------+------+------+\n2 rows in set (Elapsed: 00:00:00.16)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%80%BB%E7%BB%93\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u672c\u6587\u7684OR\u4f18\u5316\uff0c\u90fd\u8d8b\u5411\u4e8e\u624b\u5de5\u8c03\u6574\u6267\u884c\u8ba1\u5212\uff0c\u7279\u522b\u662f\u8fc7\u6ee4\u6761\u4ef6\u3002 \u800cJOIN\u7684OR\u4f18\u5316\uff0c\u5fc5\u987b\u4fdd\u8bc11\uff1a1\uff0c\u4e0d\u80fd\u662f1\uff1an\u3002\u5426\u5219\u7ed3\u679c\u96c6\u53ef\u80fd\u662f\u9519\u7684\u3002<\/p>","protected":false},"excerpt":{"rendered":"<p>\u4e1a\u52a1SQL\u4e2d\u5305\u542bOR, \u5982\u679c\u53d1\u73b0\u6027\u80fd\u5bf9\u6bd4\u6ca1\u6709\u8fd9\u4e2aOR\u4e0b\u964d\u6781\u5927\uff0c\u53ef\u4ee5\u8003\u8651\u672c\u6587\u4ecb\u7ecd\u7684OR\u4f18\u5316\u8bd5\u8bd5\u3002\u5305\u62ec\u5728\u8fc7\u6ee4\u6761\u4ef6\u91cc\u7684OR\u4ee5\u53caJOIN\u6761\u4ef6\u91cc\u7684OR\u4e24\u79cd\u60c5\u51b5\u3002<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-12749","post","type-post","status-publish","format-standard","hentry","category-gbase8a"],"_links":{"self":[{"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/posts\/12749","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/comments?post=12749"}],"version-history":[{"count":6,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/posts\/12749\/revisions"}],"predecessor-version":[{"id":12757,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/posts\/12749\/revisions\/12757"}],"wp:attachment":[{"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/media?parent=12749"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/categories?post=12749"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/tags?post=12749"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}