gpt4 book ai didi

mysql - 和盟Mysql

转载 作者:行者123 更新时间:2023-11-29 08:10:56 24 4
gpt4 key购买 nike

我必须找出一个用户在两个不同的表中出现的次数......现在我使用了联合

我有这个查询,我想对我的记录进行SUM

(SELECT s.proprietario_id as p, COUNT(*) as conta
FROM sn_like_spotted s
GROUP BY s.proprietario_id
ORDER BY COUNT(*) DESC, s.id ASC
)
UNION ALL
(SELECT s.proprietario_id as p, COUNT(*) as conta2
FROM sn_like_risposta s
GROUP BY s.proprietario_id
ORDER BY COUNT(*) DESC, s.id ASC
)

我的结果:

p  conta

1 33
2 15
3 1
4 11
5 2
1 20
2 9
4 2
9 2
18 5
19 7

我想要具有最大 conta 总和的 p

结果应该是

p  conta

1 53
2 24
4 13
19 7
...

最佳答案

使用子查询!

SELECT p, SUM(conta) AS conta 
FROM (
SELECT s.proprietario_id as p, COUNT(*) as conta
FROM sn_like_spotted s
GROUP BY s.proprietario_id
UNION ALL
SELECT s.proprietario_id as p, COUNT(*) as conta2
FROM sn_like_risposta s
GROUP BY s.proprietario_id
) AS baseview
GROUP BY p
ORDER BY conta DESC, p ASC

编辑

我自定义/粘贴了您的原始查询,该查询产生了错误的排序 - 现已修复。

关于mysql - 和盟Mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21645971/

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