

{"id":105,"date":"2021-11-03T17:33:13","date_gmt":"2021-11-03T09:33:13","guid":{"rendered":"https:\/\/www.01j.top\/?p=105"},"modified":"2021-11-03T17:34:33","modified_gmt":"2021-11-03T09:34:33","slug":"oracle-%e4%b8%addecode-%e5%87%bd%e6%95%b0%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.01j.top\/?p=105","title":{"rendered":"Oracle \u4e2ddecode \u51fd\u6570\u7528\u6cd5"},"content":{"rendered":"\n<h3 id=\"h3-0\"><\/h3>\n\n\n\n<p class=\"is-style-text-indent-2em\">\u542b\u4e49\u89e3\u91ca\uff1a<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">decode(\u6761\u4ef6,\u503c1,\u8fd4\u56de\u503c1,\u503c2,\u8fd4\u56de\u503c2,&#8230;\u503cn,\u8fd4\u56de\u503cn,\u7f3a\u7701\u503c)<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">\u8be5\u51fd\u6570\u7684\u542b\u4e49\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">IF \u6761\u4ef6=\u503c1 THEN<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">RETURN(\u7ffb\u8bd1\u503c1)<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">ELSIF \u6761\u4ef6=\u503c2 THEN<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">RETURN(\u7ffb\u8bd1\u503c2)<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">&#8230;&#8230;<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">ELSIF \u6761\u4ef6=\u503cn THEN<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">RETURN(\u7ffb\u8bd1\u503cn)<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">ELSE<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">RETURN(\u7f3a\u7701\u503c)<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">END IF<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">decode(\u5b57\u6bb5\u6216\u5b57\u6bb5\u7684\u8fd0\u7b97\uff0c\u503c1\uff0c\u503c2\uff0c\u503c3\uff09<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">\u8fd9\u4e2a\u51fd\u6570\u8fd0\u884c\u7684\u7ed3\u679c\u662f\uff0c\u5f53\u5b57\u6bb5\u6216\u5b57\u6bb5\u7684\u8fd0\u7b97\u7684\u503c\u7b49\u4e8e\u503c1\u65f6\uff0c\u8be5\u51fd\u6570\u8fd4\u56de\u503c2\uff0c\u5426\u5219\u8fd4\u56de\u503c3<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">\u5f53\u7136\u503c1\uff0c\u503c2\uff0c\u503c3\u4e5f\u53ef\u4ee5\u662f\u8868\u8fbe\u5f0f\uff0c\u8fd9\u4e2a\u51fd\u6570\u4f7f\u5f97\u67d0\u4e9bsql\u8bed\u53e5\u7b80\u5355\u4e86\u8bb8\u591a<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">\u4f7f\u7528\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">1\u3001\u6bd4\u8f83\u5927\u5c0f<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">select decode(sign(\u53d8\u91cf1-\u53d8\u91cf2),-1,\u53d8\u91cf1,\u53d8\u91cf2) from dual; &#8211;\u53d6\u8f83\u5c0f\u503c<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">sign()\u51fd\u6570\u6839\u636e\u67d0\u4e2a\u503c\u662f0\u3001\u6b63\u6570\u8fd8\u662f\u8d1f\u6570\uff0c\u5206\u522b\u8fd4\u56de0\u30011\u3001-1<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">\u4f8b\u5982\uff1a<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">\u53d8\u91cf1=10\uff0c\u53d8\u91cf2=20<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">\u5219sign(\u53d8\u91cf1-\u53d8\u91cf2)\u8fd4\u56de-1\uff0cdecode\u89e3\u7801\u7ed3\u679c\u4e3a\u201c\u53d8\u91cf1\u201d\uff0c\u8fbe\u5230\u4e86\u53d6\u8f83\u5c0f\u503c\u7684\u76ee\u7684\u3002<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">2\u3001\u6b64\u51fd\u6570\u7528\u5728SQL\u8bed\u53e5\u4e2d\uff0c\u529f\u80fd\u4ecb\u7ecd\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">Decode\u51fd\u6570\u4e0e\u4e00\u7cfb\u5217\u5d4c\u5957\u7684 IF-THEN-ELSE\u8bed\u53e5\u76f8\u4f3c\u3002base_exp\u4e0ecompare1,compare2\u7b49\u7b49\u4f9d\u6b21\u8fdb\u884c\u6bd4\u8f83\u3002\u5982\u679cbase_exp\u548c \u7b2ci \u4e2acompare\u9879\u5339\u914d\uff0c\u5c31\u8fd4\u56de\u7b2ci \u4e2a\u5bf9\u5e94\u7684value \u3002\u5982\u679cbase_exp\u4e0e\u4efb\u4f55\u7684compare\u503c\u90fd\u4e0d\u5339\u914d\uff0c\u5219\u8fd4\u56dedefault\u3002\u6bcf\u4e2acompare\u503c\u987a\u6b21\u6c42\u503c\uff0c\u5982\u679c\u53d1\u73b0\u4e00\u4e2a\u5339\u914d\uff0c\u5219\u5269\u4e0b\u7684compare\u503c\uff08\u5982\u679c\u8fd8\u6709\u7684\u8bdd\uff09\u5c31\u90fd\u4e0d\u518d\u6c42\u503c\u3002\u4e00\u4e2a\u4e3aNULL\u7684base_exp\u88ab\u8ba4\u4e3a\u548cNULL compare\u503c\u7b49\u4ef7\u3002\u5982\u679c\u9700\u8981\u7684\u8bdd\uff0c\u6bcf\u4e00\u4e2acompare\u503c\u90fd\u88ab\u8f6c\u6362\u6210\u548c\u7b2c\u4e00\u4e2acompare \u503c\u76f8\u540c\u7684\u6570\u636e\u7c7b\u578b\uff0c\u8fd9\u4e2a\u6570\u636e\u7c7b\u578b\u4e5f\u662f\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u3002<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">Decode\u51fd\u6570\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u975e\u5e38\u7684\u6709\u7528<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">\u7ed3\u5408Lpad\u51fd\u6570\uff0c\u5982\u4f55\u4f7f\u4e3b\u952e\u7684\u503c\u81ea\u52a8\u52a01\u5e76\u5728\u524d\u9762\u88650<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">select LPAD(decode(count(\u8bb0\u5f55\u7f16\u53f7),0,1,max(to_number(\u8bb0\u5f55\u7f16\u53f7)+1)),14,&#8217;0&#8242;) \u8bb0\u5f55\u7f16\u53f7 from tetdmis<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">\u2460<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">select *from users;<\/p>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img src=\"https:\/\/images2018.cnblogs.com\/blog\/1196236\/201712\/1196236-20171203164344601-1535988673.png\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"is-style-text-indent-2em\">select id, username, age, decode(sex,0,&#8217;\u7537&#8217;,1,&#8217;\u5973&#8217;) from users;<\/p>\n\n\n\n<figure class=\"wp-block-image is-style-default\"><img src=\"https:\/\/images2018.cnblogs.com\/blog\/1196236\/201712\/1196236-20171203164408491-375273096.png\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"is-style-text-indent-2em\">\u2461&#8211;\u5b50\u67e5\u8be2<br>select<br>&nbsp; sysuser.id,<br>&nbsp; sysuser.userid,<br>&nbsp; sysuser.username,<br>&nbsp; sysuser.groupid,<br>&nbsp; decode(sysuser.groupid,<br>&#8216;1&#8217;,(select mc from userjd where sysuser.sysid=userjd.id),<br>&#8216;2&#8217;,(select mc from userjd where sysuser.sysid=userjd.id),<br>&#8216;3&#8217;,(select mc from useryy where sysuser.sysid=useryy.id),<br>&#8216;4&#8217;,(select mc from usergys where sysuser.sysid=usergys.id)<br>) as mc<br>&nbsp; from sysuser;<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">\u4f8b\u5b50\u2463<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">select decode(dir,1,0,1) from a1_interval<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">dir \u7684\u503c\u662f1\u53d8\u4e3a0\uff0c\u662f0\u5219\u53d8\u4e3a1<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">\u6bd4\u5982\u6211\u8981\u67e5\u8be2\u67d0\u73ed\u7537\u751f\u548c\u5973\u751f\u7684\u6570\u91cf\u5206\u522b\u662f\u591a\u5c11?<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">\u901a\u5e38\u6211\u4eec\u8fd9\u4e48\u5199:<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">select count(*) from \u8868 where \u6027\u522b \uff1d \u7537\uff1b<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">select count(*) from \u8868 where \u6027\u522b \uff1d \u5973\uff1b<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">\u8981\u60f3\u663e\u793a\u5230\u4e00\u8d77\u8fd8\u8981union\u4e00\u4e0b\uff0c\u592a\u9ebb\u70e6\u4e86<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">\u7528decode\u5462\uff0c\u53ea\u9700\u8981\u4e00\u53e5\u8bdd<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">select sum(decode(\u6027\u522b\uff0c\u7537\uff0c1\uff0c0\uff09)\uff0csum(decode(\u6027\u522b\uff0c\u5973\uff0c1\uff0c0\uff09) from \u8868<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">eg:<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">select sum(decode(siteno,&#8217;LT&#8217;,1,0)),sum(decode(siteno,&#8217;SZ&#8217;,1,0)) from facd605;<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">select sum(case siteno when &#8216;LT&#8217; then 1 else 0 end),sum(case siteno when &#8216;SZ&#8217; then 1 else 0 end) from facd605;<\/p>\n\n\n\n<p class=\"is-style-text-indent-2em\">vinson<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u542b\u4e49\u89e3\u91ca\uff1a decode(\u6761\u4ef6,\u503c1,\u8fd4\u56de\u503c1,\u503c2,\u8fd4\u56de\u503c2,&#8230;\u503cn,\u8fd4\u56de\u503cn,\u7f3a\u7701\u503c) \u8be5\u51fd\u6570 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29,10],"tags":[27,28],"_links":{"self":[{"href":"https:\/\/www.01j.top\/index.php?rest_route=\/wp\/v2\/posts\/105"}],"collection":[{"href":"https:\/\/www.01j.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.01j.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.01j.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.01j.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=105"}],"version-history":[{"count":1,"href":"https:\/\/www.01j.top\/index.php?rest_route=\/wp\/v2\/posts\/105\/revisions"}],"predecessor-version":[{"id":106,"href":"https:\/\/www.01j.top\/index.php?rest_route=\/wp\/v2\/posts\/105\/revisions\/106"}],"wp:attachment":[{"href":"https:\/\/www.01j.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.01j.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.01j.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}