gpt4 book ai didi

mysql - 将mysql结果求和到指定组中

转载 作者:行者123 更新时间:2023-11-29 07:10:32 26 4
gpt4 key购买 nike

    SELECT 
f.hmis, sa.number_of_computers, sa.service_area
FROM
facility_service_area sa,
facility f
WHERE
f.hmis = sa.facility_hmis


hmis comp serv_area
80703 3 MCH
80706 1 LD
80701 2 ART
80701 3 ART
80701 2 LD
80701 3 LD
80701 2 MCH
80701 1 MCH
80702 4 MCH
80707 1 MCH
80707 4 MCH

我想对每个 hmi 下每个指定服务区域中的所有计算机进行求和,结果如下所示。

hmis   comp. Service_area
80703 3 MCH
80706 1 LD
80701 5 ART
80701 5 LD
80701 3 MCH
80702 4 MCH
80707 4 MCH

最佳答案

Use GROUP BY with SUM() aggregate function. 
SELECT f.hmis,
SUM(sa.number_of_computers),
sa.service_area
FROM facility_service_area sa,
facility f
WHERE f.hmis = sa.facility_hmis
GROUP BY sa.service_area


Also you can use INNER JOIN for this.

SELECT f.hmis, SUM(sa.number_of_computers), sa.service_area
FROM facility_service_area sa INNER JOIN
facility f
ON f.hmis = sa.facility_hmis
GROUP BY sa.service_area

关于mysql - 将mysql结果求和到指定组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39957783/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com