{"id":6948,"date":"2024-11-14T08:21:43","date_gmt":"2024-11-14T00:21:43","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6948\/"},"modified":"2024-11-14T08:21:43","modified_gmt":"2024-11-14T00:21:43","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e5%8a%a8%e6%80%81%e7%bb%9f%e8%ae%a1%e6%96%b9%e6%b3%95%e4%bc%98%e5%8c%96-mysql-%e6%9f%a5%e8%af%a2%e4%b8%ad%e5%af%b9%e5%a4%a7%e9%87%8f%e5%9c%b0%e5%b8%82%e7%bb%93","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6948\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528\u52a8\u6001\u7edf\u8ba1\u65b9\u6cd5\u4f18\u5316 MySQL \u67e5\u8be2\u4e2d\u5bf9\u5927\u91cf\u5730\u5e02\u7ed3\u679c\u72b6\u6001\u7684\u7edf\u8ba1\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173016872654797.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528\u52a8\u6001\u7edf\u8ba1\u65b9\u6cd5\u4f18\u5316 MySQL \u67e5\u8be2\u4e2d\u5bf9\u5927\u91cf\u5730\u5e02\u7ed3\u679c\u72b6\u6001\u7684\u7edf\u8ba1\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528\u52a8\u6001\u7edf\u8ba1\u65b9\u6cd5\u4f18\u5316 MySQL \u67e5\u8be2\u4e2d\u5bf9\u5927\u91cf\u5730\u5e02\u7ed3\u679c\u72b6\u6001\u7684\u7edf\u8ba1\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>sql \u67e5\u8be2\u4f18\u5316\uff1a\u52a8\u6001\u7edf\u8ba1\u5730\u5e02\u7ed3\u679c\u72b6\u6001<\/strong><\/p>\n<p>\u5728  \u67e5\u8be2\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u7edf\u8ba1\u4e0d\u540c\u5730\u5e02\u7684\u7ed3\u679c\u72b6\u6001\u3002\u4f20\u7edf\u7684\u65b9\u6cd5\u662f\u5728 where \u6216 group by \u5b50\u53e5\u4e2d\u5199\u6b7b\u6bcf\u4e2a\u5730\u5e02 id\uff0c\u4f46\u5f53\u6d89\u53ca\u5927\u91cf\u5730\u5e02\u65f6\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4f1a\u53d8\u5f97\u5197\u957f\u4e14\u96be\u4ee5\u7ef4\u62a4\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a\u4f7f\u7528 foreach \u5faa\u73af<\/strong><\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 foreach \u5faa\u73af\u6765\u52a8\u6001\u751f\u6210\u8981\u7edf\u8ba1\u7684\u5730\u5e02\u5217\u8868\u3002\u8fd9\u4f7f\u6211\u4eec\u80fd\u591f\u6839\u636e\u9700\u8981\u8f7b\u677e\u5730\u6dfb\u52a0\u6216\u5220\u9664\u5730\u5e02\u3002<\/p>\n<p>\u4fee\u6539\u540e\u7684\u67e5\u8be2\u5982\u4e0b\uff1a<\/p>\n<pre># \u4f7f\u7528foreach\u4ee3\u66ff\u5199\u6b7b\u4ee3\u7801\uff0c\u901a\u8fc7\u4f20\u53c2\u6570\u53d8\u66f4\u8981\u7edf\u8ba1\u7684\u57ce\u5e02\n&lt;foreach collection=\"citys\" item=\"city\"&gt;\n    count(case when city = #{city.id} then city end ) as city.name,\n&lt;\/foreach&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0ccitys \u662f\u4e00\u4e2a\u5305\u542b\u5730\u5e02 id \u548c\u540d\u79f0\u7684\u96c6\u5408\uff0ccity.id \u662f\u57ce\u5e02 id\uff0ccity.name \u662f\u57ce\u5e02\u540d\u79f0\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>SELECT\n    result_status,\n    &lt;foreach collection=\"citys\" item=\"city\"&gt;\n        COUNT(CASE WHEN city = #{city.id} THEN city END ) AS city.name,\n    &lt;\/foreach&gt;\nFROM\n    sys_patrol\nWHERE\n    YEAR ( start_patrol_time ) = '2021'\nGROUP BY\n    result_status<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u901a\u8fc7\u4f7f\u7528 foreach \u5faa\u73af\uff0c\u6211\u4eec\u907f\u514d\u4e86\u5199\u6b7b\u5730\u5e02 id \u7684\u95ee\u9898\uff0c\u4f7f\u67e5\u8be2\u66f4\u52a0\u7075\u6d3b\u3001\u53ef\u7ef4\u62a4\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528\u52a8\u6001\u7edf\u8ba1\u65b9\u6cd5\u4f18\u5316 MySQL \u67e5\u8be2\u4e2d\u5bf9\u5927\u91cf\u5730\u5e02\u7ed3\u679c\u72b6\u6001\u7684\u7edf\u8ba1\uff1f\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>sql \u67e5\u8be2\u4f18\u5316\uff1a\u52a8\u6001\u7edf\u8ba1\u5730\u5e02\u7ed3\u679c\u72b6\u6001 \u5728 \u67e5\u8be2\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u7edf\u8ba1\u4e0d\u540c\u5730\u5e02\u7684\u7ed3\u679c\u72b6\u6001\u3002\u4f20\u7edf\u7684\u65b9\u6cd5\u662f\u5728 where \u6216 group by \u5b50\u53e5\u4e2d\u5199\u6b7b\u6bcf\u4e2a\u5730\u5e02 id\uff0c\u4f46\u5f53\u6d89\u53ca\u5927\u91cf\u5730\u5e02\u65f6\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4f1a\u53d8\u5f97\u5197\u957f\u4e14\u96be\u4ee5\u7ef4\u62a4\u3002 \u89e3\u51b3\u65b9\u6848\uff1a\u4f7f\u7528 foreach \u5faa\u73af \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 foreach \u5faa\u73af\u6765\u52a8\u6001\u751f\u6210\u8981\u7edf\u8ba1\u7684\u5730\u5e02\u5217\u8868\u3002\u8fd9\u4f7f\u6211\u4eec\u80fd\u591f\u6839\u636e\u9700\u8981\u8f7b\u677e\u5730\u6dfb\u52a0\u6216\u5220\u9664\u5730\u5e02\u3002 \u4fee\u6539\u540e\u7684\u67e5\u8be2\u5982\u4e0b\uff1a # \u4f7f\u7528foreach\u4ee3\u66ff\u5199\u6b7b\u4ee3\u7801\uff0c\u901a\u8fc7\u4f20\u53c2\u6570\u53d8\u66f4\u8981\u7edf\u8ba1\u7684\u57ce\u5e02 &lt;foreach collection=&#8221;citys&#8221; item=&#8221;city&#8221;&gt; count(case when city = #{city.id} then city end ) as city.name, &lt;\/foreach&gt; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0ccitys \u662f\u4e00\u4e2a\u5305\u542b\u5730\u5e02 id \u548c\u540d\u79f0\u7684\u96c6\u5408\uff0ccity.id \u662f\u57ce\u5e02 id\uff0ccity.name \u662f\u57ce\u5e02\u540d\u79f0\u3002 \u793a\u4f8b\uff1a SELECT result_status, &lt;foreach collection=&#8221;citys&#8221; item=&#8221;city&#8221;&gt; COUNT(CASE WHEN city = #{city.id} THEN city [&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-6948","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6948","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=6948"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6948\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}