{"id":5469,"date":"2024-11-14T12:06:55","date_gmt":"2024-11-14T04:06:55","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5469\/"},"modified":"2024-11-14T12:06:55","modified_gmt":"2024-11-14T04:06:55","slug":"mysql%e4%b8%adcase-when%e6%80%8e%e4%b9%88%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5469\/","title":{"rendered":"mysql\u4e2dcase when\u600e\u4e48\u4f7f\u7528"},"content":{"rendered":"<blockquote><p>\n  mysql \u4e2d\u7684 case when \u8bed\u53e5\u662f\u4e00\u79cd\u6761\u4ef6\u8bed\u53e5\uff0c\u7528\u4e8e\u6839\u636e\u6307\u5b9a\u7684\u6761\u4ef6\u5bf9\u6570\u636e\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\u3002\u5176\u8bed\u6cd5\u4e3a\uff1acase when condition1 then result1 when condition2 then result2&#8230; else default_result end\u3002\u6761\u4ef6 (condition) \u4e3a\u5e03\u5c14\u8868\u8fbe\u5f0f\uff0c\u5982\u679c\u4e3a true\uff0c\u5219\u6267\u884c\u5bf9\u5e94\u7684 result\u3002else \u5b50\u53e5\u6307\u5b9a\u6240\u6709\u6761\u4ef6\u4e3a false \u65f6\u7684\u64cd\u4f5c\u3002\u793a\u4f8b\uff1a\u4f7f\u7528 case when \u8bed\u53e5\u6839\u636e\u5206\u6570\u5c06\u5b66\u751f\u7b49\u7ea7\u5212\u5206\u4e3a a\u3001b\u3001\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/28\/2024052815422588793.jpg\" class=\"aligncenter\" title=\"mysql\u4e2dcase when\u600e\u4e48\u4f7f\u7528\u63d2\u56fe\" alt=\"mysql\u4e2dcase when\u600e\u4e48\u4f7f\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL \u4e2d CASE WHEN \u8bed\u53e5\u7684\u4f7f\u7528<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong>MySQL \u4e2d\u7684 CASE WHEN \u8bed\u53e5\u5982\u4f55\u4f7f\u7528\uff1f<\/p>\n<p><strong>\u56de\u7b54\uff1a<\/strong><\/p>\n<p>CASE WHEN \u8bed\u53e5\u662f\u4e00\u79cd\u6761\u4ef6\u8bed\u53e5\uff0c\u5b83\u5141\u8bb8\u6839\u636e\u6307\u5b9a\u7684\u6761\u4ef6\u5bf9\u6570\u636e\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\u3002\u5b83\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>CASE WHEN condition1 THEN result1\nWHEN condition2 THEN result2\n...\nELSE default_result\nEND<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u7528\u6cd5\uff1a<\/strong><\/p>\n<ol>\n<li> <strong>\u6761\u4ef6 (condition)\uff1a<\/strong>\u6bcf\u4e2a WHEN \u5b50\u53e5\u90fd\u5305\u542b\u4e00\u4e2a\u6761\u4ef6\u8868\u8fbe\u3002\u6761\u4ef6\u53ef\u4ee5\u662f\u4efb\u4f55\u5e03\u5c14\u8868\u8fbe\u5f0f\u3002\u5982\u679c\u6761\u4ef6\u4e3a true\uff0c\u5219\u6267\u884c\u5bf9\u5e94\u7684 result\u3002<\/li>\n<li> <strong>\u7ed3\u679c (result)\uff1a<\/strong>\u6bcf\u4e2a result \u662f\u8981\u6267\u884c\u7684\u52a8\u4f5c\u6216\u8981\u8fd4\u56de\u7684\u503c\u3002\u53ef\u4ee5\u662f\u5e38\u91cf\u3001\u8868\u8fbe\u5f0f\u6216\u5b50\u67e5\u8be2\u3002<\/li>\n<li> <strong>ELSE \u5b50\u53e5\uff1a<\/strong>ELSE \u5b50\u53e5\u662f\u53ef\u9009\u7684\uff0c\u6307\u5b9a\u5982\u679c\u6240\u6709\u6761\u4ef6\u5747\u4e3a false \u65f6\u8981\u6267\u884c\u7684\u64cd\u4f5c\u6216\u8981\u8fd4\u56de\u7684\u503c\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u4ee5\u4e0b\u67e5\u8be2\u4f7f\u7528 CASE WHEN \u8bed\u53e5\u6839\u636e\u5206\u6570\u5c06\u5b66\u751f\u7b49\u7ea7\u5212\u5206\u4e3a A\u3001B\u3001C\u3001D \u548c F\uff1a<\/p>\n<pre>SELECT name, score, \nCASE\n    WHEN score &gt;= 90 THEN 'A'\n    WHEN score &gt;= 80 THEN 'B'\n    WHEN score &gt;= 70 THEN 'C'\n    WHEN score &gt;= 60 THEN 'D'\n    ELSE 'F'\nEND AS grade\nFROM students;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>name     score    grade\nAlice    95      A\nBob       82      B\nCarol     76      C\nDave      58      F<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>CASE WHEN \u8bed\u53e5\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u591a\u4e2a WHEN \u5b50\u53e5\u6765\u5904\u7406\u591a\u4e2a\u6761\u4ef6\u3002<\/li>\n<li>\u6bcf\u4e2a WHEN \u5b50\u53e5\u90fd\u53ef\u4ee5\u6709\u591a\u4e2a\u6761\u4ef6\uff0c\u4f7f\u7528 AND \u6216 OR \u8fd0\u7b97\u7b26\u8fde\u63a5\u3002<\/li>\n<li>\u5982\u679c\u6ca1\u6709 ELSE \u5b50\u53e5\uff0c\u5219\u5f53\u6240\u6709\u6761\u4ef6\u5747\u4e3a false \u65f6\uff0cCASE WHEN \u8bed\u53e5\u5c06\u8fd4\u56de null\u3002<\/li>\n<li>CASE WHEN \u8bed\u53e5\u53ef\u4ee5\u5d4c\u5957\uff0c\u4ee5\u6839\u636e\u590d\u6742\u6761\u4ef6\u6267\u884c\u590d\u6742\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4e2dcase when\u600e\u4e48\u4f7f\u7528\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>mysql \u4e2d\u7684 case when \u8bed\u53e5\u662f\u4e00\u79cd\u6761\u4ef6\u8bed\u53e5\uff0c\u7528\u4e8e\u6839\u636e\u6307\u5b9a\u7684\u6761\u4ef6\u5bf9\u6570\u636e\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\u3002\u5176\u8bed\u6cd5\u4e3a\uff1acase when condition1 then result1 when condition2 then result2&#8230; else default_result end\u3002\u6761\u4ef6 (condition) \u4e3a\u5e03\u5c14\u8868\u8fbe\u5f0f\uff0c\u5982\u679c\u4e3a true\uff0c\u5219\u6267\u884c\u5bf9\u5e94\u7684 result\u3002else \u5b50\u53e5\u6307\u5b9a\u6240\u6709\u6761\u4ef6\u4e3a false \u65f6\u7684\u64cd\u4f5c\u3002\u793a\u4f8b\uff1a\u4f7f\u7528 case when \u8bed\u53e5\u6839\u636e\u5206\u6570\u5c06\u5b66\u751f\u7b49\u7ea7\u5212\u5206\u4e3a a\u3001b\u3001 MySQL \u4e2d CASE WHEN \u8bed\u53e5\u7684\u4f7f\u7528 \u95ee\u9898\uff1aMySQL \u4e2d\u7684 CASE WHEN \u8bed\u53e5\u5982\u4f55\u4f7f\u7528\uff1f \u56de\u7b54\uff1a CASE WHEN \u8bed\u53e5\u662f\u4e00\u79cd\u6761\u4ef6\u8bed\u53e5\uff0c\u5b83\u5141\u8bb8\u6839\u636e\u6307\u5b9a\u7684\u6761\u4ef6\u5bf9\u6570\u636e\u6267\u884c\u4e0d\u540c\u7684\u64cd\u4f5c\u3002\u5b83\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 &#8230; ELSE default_result END \u767b\u5f55\u540e\u590d\u5236 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-5469","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5469","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=5469"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5469\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5469"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}