{"id":200559,"date":"2025-05-06T11:16:25","date_gmt":"2025-05-06T03:16:25","guid":{"rendered":"https:\/\/server.hk\/cnblog\/200559\/"},"modified":"2025-05-06T11:16:25","modified_gmt":"2025-05-06T03:16:25","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%e5%a4%8d%e6%9d%82sql%e8%af%ad%e5%8f%a5%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/200559\/","title":{"rendered":"\u5982\u4f55\u4f18\u5316\u5305\u542b\u5b50\u67e5\u8be2\u7684\u590d\u6742SQL\u8bed\u53e5\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u4f18\u5316\u5305\u542b\u5b50\u67e5\u8be2\u7684\u590d\u6742SQL\u8bed\u53e5\uff1f<\/h1>\n<p>\u5bf9\u4e8e\u4e00\u4e2a\u6570\u636e\u5e93\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0c\u4e3b\u673a\u5b9d\u8d1d\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300a\u5982\u4f55\u4f18\u5316\u5305\u542b\u5b50\u67e5\u8be2\u7684\u590d\u6742SQL\u8bed\u53e5\uff1f\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241108\/1731052648672dc468cfc07.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u4f18\u5316\u590d\u6742sql\u8bed\u53e5<\/strong><\/p>\n<p>\u539fsql\u8bed\u53e5\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\n        from\n            energy_station es\n        left join user_station us on us.station_id = es.id\n        left join user_info u on u.id = us.user_id<\/pre>\n<p><strong>\u4f18\u5316\u65b9\u6cd5<\/strong><\/p>\n<ol>\n<li><strong>\u6dfb\u52a0\u7d22\u5f15<\/strong><\/li>\n<\/ol>\n<p>\u4e3a\u8868air_conditioner_equipment\u7684\u5b57\u6bb5station_id\u6dfb\u52a0\u7d22\u5f15\uff0c\u53ef\u4ee5\u52a0\u5feb\u6839\u636e\u8be5\u5b57\u6bb5\u8fdb\u884c\u67e5\u8be2\u7684\u901f\u5ea6\u3002<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u5b50\u67e5\u8be2\u4f18\u5316<\/strong><\/li>\n<\/ol>\n<p>\u4f18\u5316\u540esql\u8bed\u53e5\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            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\n        FROM energy_station es\n        LEFT JOIN user_station us ON us.station_id = es.id\n        LEFT JOIN user_info u ON u.id = us.user_id\n        LEFT 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\n        GROUP BY es.id;<\/pre>\n<p>\u4f18\u5316\u540e\u7684sql\u8bed\u53e5\u4f7f\u7528\u5b50\u67e5\u8be2ace\u6765\u9884\u5148\u8ba1\u7b97\u6240\u9700\u7684\u805a\u5408\u503c\uff0c\u7136\u540e\u518d\u4f7f\u7528left join\u5c06\u5176\u4e0e\u4e3b\u8868\u8fde\u63a5\u3002\u8fd9\u6837\u53ef\u4ee5\u51cf\u5c11\u9700\u8981\u8fdb\u884c\u7684\u5b50\u67e5\u8be2\u6570\u91cf\uff0c\u4ece\u800c\u63d0\u9ad8\u67e5\u8be2\u6548\u7387\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u4e3b\u673a\u5b9d\u8d1d\uff01\u66f4\u591a\u5173\u4e8e\u6570\u636e\u5e93\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\u3002<\/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\u590d\u6742SQL\u8bed&#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-200559","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200559","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=200559"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/200559\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=200559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=200559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=200559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}