{"id":200738,"date":"2025-05-06T11:18:07","date_gmt":"2025-05-06T03:18:07","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200738\/"},"modified":"2025-05-06T11:18:07","modified_gmt":"2025-05-06T03:18:07","slug":"sql%e4%bc%98%e5%8c%96%ef%bc%9a%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%e6%9f%a5%e8%af%a2%e8%af%ad%e5%8f%a5%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200738\/","title":{"rendered":"SQL\u4f18\u5316\uff1a\u5982\u4f55\u4f18\u5316\u5305\u542b\u5b50\u67e5\u8be2\u7684\u67e5\u8be2\u8bed\u53e5\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>SQL\u4f18\u5316\uff1a\u5982\u4f55\u4f18\u5316\u5305\u542b\u5b50\u67e5\u8be2\u7684\u67e5\u8be2\u8bed\u53e5\uff1f<\/h1>\n<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9\u6570\u636e\u5e93\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300aSQL\u4f18\u5316\uff1a\u5982\u4f55\u4f18\u5316\u5305\u542b\u5b50\u67e5\u8be2\u7684\u67e5\u8be2\u8bed\u53e5\uff1f\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241113\/173146396067340b18bb88d.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>sql\u4f18\u5316\uff1a\u5982\u4f55\u4f18\u5316\u6b64\u67e5\u8be2\uff1f<\/strong><\/p>\n<p>\u539f\u67e5\u8be2\u5982\u4e0b\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>\u4f18\u5316\u5efa\u8bae\uff1a<\/p>\n<ul>\n<li>\u4e3aair_conditioner_equipment\u8868\u7684station_id\u5217\u6dfb\u52a0\u7d22\u5f15\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u6dfb\u52a0\u7d22\u5f15\uff0cmysql\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u67e5\u627e\u6570\u636e\uff0c\u4ece\u800c\u63d0\u9ad8\u67e5\u8be2\u901f\u5ea6\u3002<\/p>\n<p>\u4f18\u5316\u540e\u7684\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>\u672c\u7bc7\u5173\u4e8e\u300aSQL\u4f18\u5316\uff1a\u5982\u4f55\u4f18\u5316\u5305\u542b\u5b50\u67e5\u8be2\u7684\u67e5\u8be2\u8bed\u53e5\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6570\u636e\u5e93\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>SQL\u4f18\u5316\uff1a\u5982\u4f55\u4f18\u5316\u5305\u542b\u5b50\u67e5\u8be2\u7684&#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-200738","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200738","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=200738"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200738\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200738"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}