{"id":478,"date":"2021-07-19T08:06:01","date_gmt":"2021-07-19T00:06:01","guid":{"rendered":"http:\/\/zhuxinyong.com\/?p=478"},"modified":"2021-07-19T08:35:45","modified_gmt":"2021-07-19T00:35:45","slug":"mysql-%e4%b9%8b-case-when-%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/zhuxinyong.com\/?p=478","title":{"rendered":"MySQL \u4e4b \bCase When \u7528\u6cd5"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"759\" height=\"500\" src=\"http:\/\/zhuxinyong.com\/wp-content\/uploads\/2021\/07\/59ddd3bd1272bba14bc403febe225e0f.jpeg\" alt=\"\" class=\"wp-image-484\" srcset=\"https:\/\/zhuxinyong.com\/wp-content\/uploads\/2021\/07\/59ddd3bd1272bba14bc403febe225e0f.jpeg 759w, https:\/\/zhuxinyong.com\/wp-content\/uploads\/2021\/07\/59ddd3bd1272bba14bc403febe225e0f-300x198.jpeg 300w\" sizes=\"auto, (max-width: 759px) 100vw, 759px\" \/><\/figure>\n\n\n\n<p>\u5148\u770b\u4e0b\u8bed\u6cd5 MySQL 8.0 \u7684\u8bed\u6cd5\uff1a<\/p>\n\n\n\n<p>\u7b2c\u4e00\u79cd\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">CASE case_value\n    WHEN when_value THEN statement_list\n    [WHEN when_value THEN statement_list] ...\n    [ELSE statement_list]\nEND CASE<\/pre>\n\n\n\n<p>\u7b2c\u4e8c\u79cd\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">CASE\n    WHEN search_condition THEN statement_list\n    [WHEN search_condition THEN statement_list] ...\n    [ELSE statement_list]\nEND CASE<\/pre>\n\n\n\n<p>\u73b0\u5728\u7684\u9700\u6c42\u662f\uff1a\u7ed9\u6570\u636e\u7684 grade \u5b57\u6bb5\u6dfb\u52a0\u76f8\u5e94\u7684\u503c\u3002<\/p>\n\n\n\n<p>\u6bd4\u5982\uff1a<\/p>\n\n\n\n<p>\u4e00\u5e74\u7ea7\u8981\u6c42\u8ba4\u8bc6\u7684\u5b57\u5305\u62ec\uff1agrade = 1<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u4e00\u4e8c\u4e09<br>\u5341\u6728\u79be<br>\u4e0a\u4e0b\u571f\u4e2a<\/p><\/blockquote>\n\n\n\n<p>\u4e8c\u5e74\u7ea7\u8981\u6c42\u8ba4\u8bc6\u7684\u5b57\u5305\u62ec\uff1agrade=2<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u5b9c\u5b9e\u8272\u534e\u8c37\u91d1\u5c3d\u5c42\u4e30\u58ee<br>\u6ce2\u6d6a\u706f\u4f5c\u5b57\u82f9\u4e3d\u52b3<br>\u5c24\u5176\u533a\u5de8\u5b83\u5b89\u5757\u7ad9<\/p><\/blockquote>\n\n\n\n<p>\u4e66\u5199\u7684SQL\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">UPDATE `w` SET \n\t`grade` = (CASE `word`\n\t\tWHERE \"\u4e0a\" THEN \"1\"\n\t\tWHERE \"\u4e30\" THEN \"2\"\n\tEND)\n\tWHERE word in(\"\u4e0a\",\"\u4e30\")<\/pre>\n\n\n\n<p>\u540c\u65f6\u6539\u52a8\u591a\u4e2a\u5b57\u6bb5\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">UPDATE demo SET content = ( \n  CASE \n     WHEN id = 1 THEN '\u5185\u5bb9111' \n     WHEN id = 0 THEN '\u5185\u5bb9000'\n  END\n),id = (\n    CASE \n     WHEN id = 1 THEN 11\n     WHEN id = 0 THEN 22\n  END\n)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5148\u770b\u4e0b\u8bed\u6cd5 MySQL 8.0 \u7684\u8bed\u6cd5\uff1a \u7b2c\u4e00\u79cd\uff1a \u7b2c\u4e8c\u79cd\uff1a \u73b0\u5728\u7684\u9700\u6c42\u662f\uff1a\u7ed9\u6570\u636e\u7684 grade \u5b57&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,26],"tags":[25],"class_list":["post-478","post","type-post","status-publish","format-standard","hentry","category-all","category-database","tag-mysql"],"_links":{"self":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/478","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=478"}],"version-history":[{"count":7,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/478\/revisions"}],"predecessor-version":[{"id":492,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=\/wp\/v2\/posts\/478\/revisions\/492"}],"wp:attachment":[{"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuxinyong.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}