{"id":7425,"date":"2024-11-14T16:26:01","date_gmt":"2024-11-14T08:26:01","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7425\/"},"modified":"2024-11-14T16:26:01","modified_gmt":"2024-11-14T08:26:01","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:\/\/fwq.ai\/blog\/7425\/","title":{"rendered":"\u5982\u4f55\u4f18\u5316\u5305\u542b\u5b50\u67e5\u8be2\u7684\u590d\u6742SQL\u8bed\u53e5\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173081028016230.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f18\u5316\u5305\u542b\u5b50\u67e5\u8be2\u7684\u590d\u6742SQL\u8bed\u53e5\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f18\u5316\u5305\u542b\u5b50\u67e5\u8be2\u7684\u590d\u6742SQL\u8bed\u53e5\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f18\u5316\u590d\u6742<\/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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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\u8868r_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> \u767b\u5f55\u540e\u590d\u5236 <\/p>\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\u5982\u4f55\u4f18\u5316\u5305\u542b\u5b50\u67e5\u8be2\u7684\u590d\u6742SQL\u8bed\u53e5\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>\u5982\u4f55\u4f18\u5316\u590d\u6742 \u539fsql\u8bed\u53e5\u5982\u4e0b\uff1a select es.id, es.station_name, es.province, es.city, es.district, es.detailed, ( select count(*) from air_conditioner_equipment where station_id = es.id ) as air_conditioning_count, ( select sum( cold_power ) from air_conditioner_equipment where station_id = es.id) as cold_power_total, ( select sum( hot_power) from air_conditioner_equipment where station_id = es.id) as hot_power_total, es.create_time, es.state from energy_station es left join user_station us [&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-7425","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7425","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=7425"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7425\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}