gpt4 book ai didi

mysql - 多表Count的SQL查询

转载 作者:行者123 更新时间:2023-11-29 12:13:17 25 4
gpt4 key购买 nike

我有这个查询来计算每个表中 documentador 重复的次数

(select count(documentador) AS count, documentador from impo_expo GROUP BY documentador order by documentador) UNION ALL
(select count(documentador), documentador from prov_compr GROUP BY documentador order by documentador) UNION ALL
(select count(documentador), documentador from pedimento GROUP BY documentador order by documentador) UNION ALL
(select count(documentador), documentador from partidas GROUP BY documentador order by documentador) order by documentador asc

输出:

count   documentador
2 Imelda
4 Imelda
2 Imelda
1 Imelda
2 Juan Enrique
1 Juan Enrique
1 Juan Enrique
1 Juan Enrique
2 Raul

我怎样才能将它们组合起来得到这样的东西?

count   documentador
9 Imelda
5 Juan Enrique
2 Raul

最佳答案

围绕您的查询添加“分组依据”和“总和”怎么样?像这样的事情:

select documentador, sum(count) as thesum from (
(select count(documentador) AS count, documentador from impo_expo GROUP BY documentador) UNION ALL
(select count(documentador), documentador from prov_compr GROUP BY documentador) UNION ALL
(select count(documentador), documentador from pedimento GROUP BY documentador) UNION ALL
(select count(documentador), documentador from partidas GROUP BY documentador) order by documentador asc
) a group by documentador order by thesum desc

关于mysql - 多表Count的SQL查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30296869/

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