{"id":6401,"date":"2024-11-14T12:20:53","date_gmt":"2024-11-14T04:20:53","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6401\/"},"modified":"2024-11-14T12:20:53","modified_gmt":"2024-11-14T04:20:53","slug":"mysql%e4%b8%adcase-when-then-%e7%9a%84%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6401\/","title":{"rendered":"mysql\u4e2dcase when then \u7684\u7528\u6cd5"},"content":{"rendered":"<blockquote><p>\n  mysql \u4e2d\u7684 case when then \u8bed\u53e5\u7528\u4e8e\u6839\u636e\u6761\u4ef6\u6267\u884c\u4e0d\u540c\u7684\u52a8\u4f5c\uff1a\u6307\u5b9a\u6761\u4ef6\uff1a\u4f7f\u7528 when \u5b50\u53e5\u6307\u5b9a\u8981\u8bc4\u4f30\u7684\u6761\u4ef6\u3002\u6267\u884c\u52a8\u4f5c\uff1a\u6761\u4ef6\u4e3a\u771f\u5219\u6267\u884c then \u5b50\u53e5\u4e2d\u7684\u52a8\u4f5c\u3002\u63d0\u4f9b\u9ed8\u8ba4\u7ed3\u679c\uff1aelse \u5b50\u53e5\u6307\u5b9a\u6240\u6709\u6761\u4ef6\u4e3a\u5047\u65f6\u8981\u6267\u884c\u7684\u52a8\u4f5c\u3002\u8be5\u8bed\u53e5\u53ef\u66ff\u4ee3\u590d\u6742\u7684 if-else \u8bed\u53e5\uff0c\u4f7f\u4ee3\u7801\u66f4\u6e05\u6670\u4e14\u53ef\u6839\u636e\u591a\u4e2a\u6761\u4ef6\u8fdb\u884c\u5206\u652f\u51b3\u7b56\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202408\/01\/2024080121304296543.jpg\" class=\"aligncenter\" title=\"mysql\u4e2dcase when then \u7684\u7528\u6cd5\u63d2\u56fe\" alt=\"mysql\u4e2dcase when then \u7684\u7528\u6cd5\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL \u4e2d CASE WHEN THEN \u8bed\u53e5\u7684\u7528\u6cd5<\/strong><\/p>\n<p>MySQL \u4e2d\u7684 CASE WHEN THEN \u8bed\u53e5\u662f\u4e00\u79cd\u63a7\u5236\u6d41\u8bed\u53e5\uff0c\u7528\u4e8e\u6839\u636e\u6307\u5b9a\u7684\u6761\u4ef6\u6267\u884c\u4e0d\u540c\u7684\u52a8\u4f5c\u3002\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>CASE\n  WHEN condition1 THEN result1\n  WHEN condition2 THEN result2\n  ...\n  ELSE 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>\u6307\u5b9a\u6761\u4ef6\uff1a<\/strong>\u4f7f\u7528 WHEN \u5b50\u53e5\u6307\u5b9a\u8981\u8bc4\u4f30\u7684\u6761\u4ef6\u3002<\/li>\n<li> <strong>\u6267\u884c\u52a8\u4f5c\uff1a<\/strong>\u5982\u679c\u6761\u4ef6\u4e3a\u771f\uff0c\u5219\u6267\u884c THEN \u5b50\u53e5\u4e2d\u6307\u5b9a\u7684\u52a8\u4f5c\u3002<\/li>\n<li> <strong>\u63d0\u4f9b\u9ed8\u8ba4\u7ed3\u679c\uff1a<\/strong>ELSE \u5b50\u53e5\u6307\u5b9a\u5982\u679c\u6240\u6709\u6761\u4ef6\u90fd\u4e3a\u5047\u65f6\u8981\u6267\u884c\u7684\u52a8\u4f5c\u3002<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>SELECT CASE\n  WHEN age &lt; 18 THEN 'Teenager'\n  WHEN age &lt; 65 THEN 'Adult'\n  ELSE 'Senior'\nEND AS age_category\nFROM person;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u4e2a\u793a\u4f8b\u6839\u636e age \u5217\u7684\u503c\u5c06\u4eba\u5458\u5206\u7c7b\u4e3a\u9752\u5c11\u5e74\u3001\u6210\u5e74\u6216\u8001\u5e74\u4eba\u3002<\/p>\n<p><strong>\u7279\u70b9\uff1a<\/strong><\/p>\n<ul>\n<li>CASE WHEN THEN \u8bed\u53e5\u53ef\u4ee5\u5305\u542b\u591a\u4e2a WHEN \u5b50\u53e5\uff0c\u6bcf\u4e2a\u5b50\u53e5\u5904\u7406\u4e00\u4e2a\u4e0d\u540c\u7684\u6761\u4ef6\u3002<\/li>\n<li>ELSE \u5b50\u53e5\u662f\u53ef\u9009\u7684\uff0c\u5982\u679c\u4e0d\u6307\u5b9a\uff0c\u5219\u5728\u6240\u6709\u6761\u4ef6\u90fd\u4e3a\u5047\u65f6\u8fd4\u56de NULL\u3002<\/li>\n<li>CASE WHEN THEN \u8bed\u53e5\u53ef\u4ee5\u5d4c\u5957\u5728\u5176\u4ed6 CASE \u8bed\u53e5\u4e2d\uff0c\u4ee5\u521b\u5efa\u66f4\u590d\u6742\u7684\u6761\u4ef6\u3002<\/li>\n<li>\u5b50\u67e5\u8be2\u53ef\u4ee5\u7528\u4f5c\u6761\u4ef6\u6216\u7ed3\u679c\uff0c\u4ee5\u63d0\u4f9b\u66f4\u7075\u6d3b\u7684\u63a7\u5236\u3002<\/li>\n<\/ul>\n<p><strong>\u4f18\u52bf\uff1a<\/strong><\/p>\n<ul>\n<li>\u66ff\u4ee3\u590d\u6742\u7684 IF-ELSE \u8bed\u53e5\uff0c\u4f7f\u4ee3\u7801\u66f4\u6e05\u6670\u3001\u66f4\u7b80\u6d01\u3002<\/li>\n<li>\u5141\u8bb8\u6839\u636e\u591a\u4e2a\u6761\u4ef6\u8fdb\u884c\u5206\u652f\u51b3\u7b56\u3002<\/li>\n<li>\u53ef\u4ee5\u52a8\u6001\u751f\u6210\u7ed3\u679c\uff0c\u6839\u636e\u4e0d\u540c\u7684\u8f93\u5165\u503c\u8fd4\u56de\u4e0d\u540c\u7684\u503c\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4e2dcase when then \u7684\u7528\u6cd5\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 then \u8bed\u53e5\u7528\u4e8e\u6839\u636e\u6761\u4ef6\u6267\u884c\u4e0d\u540c\u7684\u52a8\u4f5c\uff1a\u6307\u5b9a\u6761\u4ef6\uff1a\u4f7f\u7528 when \u5b50\u53e5\u6307\u5b9a\u8981\u8bc4\u4f30\u7684\u6761\u4ef6\u3002\u6267\u884c\u52a8\u4f5c\uff1a\u6761\u4ef6\u4e3a\u771f\u5219\u6267\u884c then \u5b50\u53e5\u4e2d\u7684\u52a8\u4f5c\u3002\u63d0\u4f9b\u9ed8\u8ba4\u7ed3\u679c\uff1aelse \u5b50\u53e5\u6307\u5b9a\u6240\u6709\u6761\u4ef6\u4e3a\u5047\u65f6\u8981\u6267\u884c\u7684\u52a8\u4f5c\u3002\u8be5\u8bed\u53e5\u53ef\u66ff\u4ee3\u590d\u6742\u7684 if-else \u8bed\u53e5\uff0c\u4f7f\u4ee3\u7801\u66f4\u6e05\u6670\u4e14\u53ef\u6839\u636e\u591a\u4e2a\u6761\u4ef6\u8fdb\u884c\u5206\u652f\u51b3\u7b56\u3002 MySQL \u4e2d CASE WHEN THEN \u8bed\u53e5\u7684\u7528\u6cd5 MySQL \u4e2d\u7684 CASE WHEN THEN \u8bed\u53e5\u662f\u4e00\u79cd\u63a7\u5236\u6d41\u8bed\u53e5\uff0c\u7528\u4e8e\u6839\u636e\u6307\u5b9a\u7684\u6761\u4ef6\u6267\u884c\u4e0d\u540c\u7684\u52a8\u4f5c\u3002\u5176\u8bed\u6cd5\u5982\u4e0b\uff1a CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 &#8230; ELSE default_result END \u767b\u5f55\u540e\u590d\u5236 \u7528\u6cd5\uff1a \u6307\u5b9a\u6761\u4ef6\uff1a\u4f7f\u7528 WHEN \u5b50\u53e5\u6307\u5b9a\u8981\u8bc4\u4f30\u7684\u6761\u4ef6\u3002 \u6267\u884c\u52a8\u4f5c\uff1a\u5982\u679c\u6761\u4ef6\u4e3a\u771f\uff0c\u5219\u6267\u884c THEN \u5b50\u53e5\u4e2d\u6307\u5b9a\u7684\u52a8\u4f5c\u3002 \u63d0\u4f9b\u9ed8\u8ba4\u7ed3\u679c\uff1aELSE \u5b50\u53e5\u6307\u5b9a\u5982\u679c\u6240\u6709\u6761\u4ef6\u90fd\u4e3a\u5047\u65f6\u8981\u6267\u884c\u7684\u52a8\u4f5c\u3002 \u793a\u4f8b\uff1a SELECT CASE WHEN age &lt; 18 [&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-6401","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6401","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=6401"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6401\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}