{"id":6931,"date":"2024-11-14T13:10:07","date_gmt":"2024-11-14T05:10:07","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6931\/"},"modified":"2024-11-14T13:10:07","modified_gmt":"2024-11-14T05:10:07","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8sql%e5%8a%a8%e6%80%81%e7%bb%9f%e8%ae%a1%e5%a4%9a%e4%b8%aa%e5%9f%8e%e5%b8%82%e7%9a%84%e7%bb%93%e6%9e%9c%e7%8a%b6%e6%80%81%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6931\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528SQL\u52a8\u6001\u7edf\u8ba1\u591a\u4e2a\u57ce\u5e02\u7684\u7ed3\u679c\u72b6\u6001\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173011521083427.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528SQL\u52a8\u6001\u7edf\u8ba1\u591a\u4e2a\u57ce\u5e02\u7684\u7ed3\u679c\u72b6\u6001\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528SQL\u52a8\u6001\u7edf\u8ba1\u591a\u4e2a\u57ce\u5e02\u7684\u7ed3\u679c\u72b6\u6001\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>sql\u7edf\u8ba1\u4e0d\u540c\u5730\u5e02\u7ed3\u679c\u72b6\u6001<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u5f53\u9700\u8981\u7edf\u8ba1\u5927\u91cf\u4e0d\u540c\u5730\u5e02\u7684\u7ed3\u679c\u72b6\u6001\u65f6\uff0c\u5982\u4f55\u907f\u514d\u624b\u52a8\u5199\u6b7b\u6bcf\u4e2a\u57ce\u5e02id\uff0c\u5b9e\u73b0\u66f4\u7075\u6d3b\u7684\u52a8\u6001\u67e5\u8be2\uff1f<\/p>\n<p><strong>\u89e3\u7b54\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528 foreach \u5faa\u73af\u4ee3\u66ff\u5199\u6b7b\u7684\u57ce\u5e02id\u4ee3\u7801\uff0c\u901a\u8fc7\u4f20\u5165\u53c2\u6570\u6765\u6307\u5b9a\u9700\u8981\u7edf\u8ba1\u7684\u57ce\u5e02\u3002<\/p>\n<p><strong>\u4ee3\u7801\uff1a<\/strong><\/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>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 java \u4ee3\u7801\u6216 sql \u811a\u672c\u5c06\u57ce\u5e02id\u5217\u8868\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9 sql \u67e5\u8be2\uff0c\u4ece\u800c\u52a8\u6001\u751f\u6210\u7edf\u8ba1\u7ed3\u679c\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a java \u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>\/\/ \u6784\u5efa\u57ce\u5e02ID\u5217\u8868\nList&lt;String&gt; cityIds = new ArrayList&lt;&gt;();\n\/\/ ...\n\n\/\/ \u6784\u5efa\u67e5\u8be2\u8bed\u53e5\nString query = \"SELECT\n\" +\n    \"    result_status,\n\" +\n    \"&lt;foreach collection=\"cityIds\" item=\"cityId\"&gt;\n\" +\n    \"    COUNT(CASE WHEN city = #{cityId} THEN city END ) AS ${cityId},\n\" +\n    \"&lt;\/foreach&gt;\n\" +\n    \"FROM\n\" +\n    \"    sys_patrol\n\" +\n    \"WHERE\n\" +\n    \"    YEAR ( start_patrol_time ) = '2021'\n\" +\n    \"GROUP BY\n\" +\n    \"    result_status\";\n\n\/\/ \u6267\u884c\u67e5\u8be2\nList&lt;Result&gt; results = jdbcTemplate.query(query, new BeanPropertyRowMapper&lt;&gt;(Result.class), cityIds);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528SQL\u52a8\u6001\u7edf\u8ba1\u591a\u4e2a\u57ce\u5e02\u7684\u7ed3\u679c\u72b6\u6001\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\u7edf\u8ba1\u4e0d\u540c\u5730\u5e02\u7ed3\u679c\u72b6\u6001 \u95ee\u9898\uff1a \u5f53\u9700\u8981\u7edf\u8ba1\u5927\u91cf\u4e0d\u540c\u5730\u5e02\u7684\u7ed3\u679c\u72b6\u6001\u65f6\uff0c\u5982\u4f55\u907f\u514d\u624b\u52a8\u5199\u6b7b\u6bcf\u4e2a\u57ce\u5e02id\uff0c\u5b9e\u73b0\u66f4\u7075\u6d3b\u7684\u52a8\u6001\u67e5\u8be2\uff1f \u89e3\u7b54\uff1a \u4f7f\u7528 foreach \u5faa\u73af\u4ee3\u66ff\u5199\u6b7b\u7684\u57ce\u5e02id\u4ee3\u7801\uff0c\u901a\u8fc7\u4f20\u5165\u53c2\u6570\u6765\u6307\u5b9a\u9700\u8981\u7edf\u8ba1\u7684\u57ce\u5e02\u3002 \u4ee3\u7801\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 \u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 java \u4ee3\u7801\u6216 sql \u811a\u672c\u5c06\u57ce\u5e02id\u5217\u8868\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9 sql \u67e5\u8be2\uff0c\u4ece\u800c\u52a8\u6001\u751f\u6210\u7edf\u8ba1\u7ed3\u679c\u3002 \u4ee5\u4e0b\u662f\u4e00\u4e2a java \u4ee3\u7801\u793a\u4f8b\uff1a \/\/ \u6784\u5efa\u57ce\u5e02ID\u5217\u8868 List&lt;String&gt; cityIds = new ArrayList&lt;&gt;(); \/\/ &#8230; \/\/ \u6784\u5efa\u67e5\u8be2\u8bed\u53e5 String query = &#8220;SELECT &#8221; + &#8221; result_status, [&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-6931","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6931","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=6931"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6931\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}