{"id":200565,"date":"2025-05-06T10:34:18","date_gmt":"2025-05-06T02:34:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200565\/"},"modified":"2025-05-06T10:34:18","modified_gmt":"2025-05-06T02:34:18","slug":"%e5%a6%82%e4%bd%95%e4%bc%98%e5%8c%96%e5%8c%85%e5%90%ab%e5%ad%90%e6%9f%a5%e8%af%a2%e7%9a%84-sql-%e6%9f%a5%e8%af%a2%e4%bb%a5%e6%8f%90%e9%ab%98%e6%80%a7%e8%83%bd%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200565\/","title":{"rendered":"\u5982\u4f55\u4f18\u5316\u5305\u542b\u5b50\u67e5\u8be2\u7684 SQL \u67e5\u8be2\u4ee5\u63d0\u9ad8\u6027\u80fd\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u4f18\u5316\u5305\u542b\u5b50\u67e5\u8be2\u7684 SQL \u67e5\u8be2\u4ee5\u63d0\u9ad8\u6027\u80fd\uff1f<\/h1>\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241108\/1731060045672de14d797dd.jpg\" class=\"aligncenter\"><\/p>\n<p>\u5982\u4f55\u4f18\u5316\u67e5\u8be2\u4ee5\u63d0\u9ad8\u6027\u80fd\uff1f <\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u6761\u67e5\u8be2\u8bed\u53e5\uff1a<\/p>\n<pre>select\n    es.id,\n    es.station_name,\n    es.province,\n    es.city,\n    es.district,\n    es.detailed,\n    ( select count(*) from air_conditioner_equipment where station_id = es.id ) as air_conditioning_count,\n    ( select sum( cold_power ) from air_conditioner_equipment where station_id = es.id) as cold_power_total,\n    ( select sum( hot_power) from air_conditioner_equipment where station_id = es.id) as hot_power_total,\n    es.create_time,\n    es.state\nfrom\n    energy_station es\nleft join user_station us on us.station_id = es.id\nleft join user_info u on u.id = us.user_id<\/pre>\n<p>\u9488\u5bf9\u6b64\u67e5\u8be2\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u8fdb\u884c\u4f18\u5316\uff1a<\/p>\n<p><strong>\u6dfb\u52a0\u7d22\u5f15<\/strong><\/p>\n<p>\u67e5\u8be2\u4e2d\u9891\u7e41\u8fde\u63a5\u7684\u5b57\u6bb5station_id\u672a\u6dfb\u52a0\u7d22\u5f15\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u67e5\u8be2\u6027\u80fd\u4f4e\u4e0b\u3002\u4e3astation_id\u5b57\u6bb5\u6dfb\u52a0\u7d22\u5f15\u53ef\u4ee5\u663e\u7740\u63d0\u9ad8\u67e5\u8be2\u901f\u5ea6\u3002 <\/p>\n<p><strong>\u4f7f\u7528\u5b50\u67e5\u8be2\u4f18\u5316<\/strong><\/p>\n<p>\u67e5\u8be2\u4e2d\u5305\u542b\u591a\u4e2a\u5b50\u67e5\u8be2\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u67e5\u8be2\u6267\u884c\u6548\u7387\u4f4e\u4e0b\u3002\u53ef\u4ee5\u4f7f\u7528\u8fde\u63a5\u5b50\u67e5\u8be2\u7684\u65b9\u5f0f\u6765\u4f18\u5316\u67e5\u8be2\uff1a<\/p>\n<pre>SELECT\n    es.id,\n    es.station_name,\n    es.province,\n    es.city,\n    es.district,\n    es.detailed,\n    SUM(ace.air_conditioning_count) AS air_conditioning_count,\n    SUM(ace.cold_power) AS cold_power_total,\n    SUM(ace.hot_power) AS hot_power_total,\n    es.create_time,\n    es.state\nFROM energy_station es\nLEFT JOIN user_station us ON us.station_id = es.id\nLEFT JOIN user_info u ON u.id = us.user_id\nLEFT JOIN (\n      SELECT  station_id, COUNT(*) as air_conditioning_count, SUM(cold_power) as cold_power, SUM(hot_power) as hot_power\n      FROM air_conditioner_equipment\n      GROUP BY station_id\n) ace ON ace.station_id = es.id\nGROUP BY es.id;<\/pre>\n<p>\u901a\u8fc7\u8fde\u63a5\u5b50\u67e5\u8be2\uff0c\u53ef\u4ee5\u5c06\u591a\u4e2a\u5b50\u67e5\u8be2\u5408\u5e76\u4e3a\u4e00\u4e2a\uff0c\u4ece\u800c\u63d0\u9ad8\u67e5\u8be2\u6548\u7387\u3002 <\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6570\u636e\u5e93\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f18\u5316\u5305\u542b\u5b50\u67e5\u8be2\u7684 SQL \u67e5&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-200565","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200565","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=200565"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200565\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}