将几个查询的结果相加,然后在 SQL 中找到前 5 个
我有 3 个查询:
table: pageview
SELECT event_id, count(*) AS pageviews
FROM pageview
GROUP BY event_id
ORDER BY pageviews DESC, rand()
LIMIT 1000
table: upvote
SELECT event_id, count(*) AS upvotes
FROM upvote
GROUP BY event_id
ORDER BY upvotes DESC, rand()
LIMIT 1000
table: attending
SELECT event_id, count(*) AS attendants
FROM attending
GROUP BY event_id
ORDER BY attendants DESC, rand()
LIMIT 1000
我想合并按金额排序的所有 3 个查询的 event_id
,然后选择前 5 个查询。我该如何操作?
编辑:这是我为实现这一目标所做的事情:
SELECT event_id, sum(amount) AS total
FROM (
(SELECT event_id, count(*) AS amount
FROM pageview
GROUP BY event_id
ORDER BY amount DESC, rand()
LIMIT 1000)
UNION ALL
(SELECT event_id, count(*) as amount
FROM upvote
GROUP BY event_id
ORDER BY amount DESC, rand()
LIMIT 1000)
UNION ALL
(SELECT event_id, count(*) as amount
FROM attending
GROUP BY event_id
ORDER BY amount DESC, rand()
LIMIT 1000)
) x
GROUP BY 1
ORDER BY sum(amount) DESC
LIMIT 5;
I have 3 queries:
table: pageview
SELECT event_id, count(*) AS pageviews
FROM pageview
GROUP BY event_id
ORDER BY pageviews DESC, rand()
LIMIT 1000
table: upvote
SELECT event_id, count(*) AS upvotes
FROM upvote
GROUP BY event_id
ORDER BY upvotes DESC, rand()
LIMIT 1000
table: attending
SELECT event_id, count(*) AS attendants
FROM attending
GROUP BY event_id
ORDER BY attendants DESC, rand()
LIMIT 1000
I'd like to combine the event_id
s of all 3 queries ordered by amount and then choose the top 5. How do I do that?
EDIT: HERE IS WHAT I DID TO MAKE IT HAPPEN:
SELECT event_id, sum(amount) AS total
FROM (
(SELECT event_id, count(*) AS amount
FROM pageview
GROUP BY event_id
ORDER BY amount DESC, rand()
LIMIT 1000)
UNION ALL
(SELECT event_id, count(*) as amount
FROM upvote
GROUP BY event_id
ORDER BY amount DESC, rand()
LIMIT 1000)
UNION ALL
(SELECT event_id, count(*) as amount
FROM attending
GROUP BY event_id
ORDER BY amount DESC, rand()
LIMIT 1000)
) x
GROUP BY 1
ORDER BY sum(amount) DESC
LIMIT 5;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对所有三个查询的结果行进行
UNION
,然后选择金额
最高的 5 行:手册:
UNION ALL
以保留重复项。添加每个
event_id
的计数:这里棘手的部分是并非每个
event_id
都会出现在所有三个基本查询中。因此,请注意JOIN
不会完全丢失行,并且添加不会导致NULL
。使用
UNION ALL
,而不是UNION
。您不想删除相同的行,而是想将它们相加。x
是表别名和AS x
的简写。子查询需要有一个名称。这里可以是任何其他名称。MySQL 中未实现 SOL 功能 FULL OUTER JOIN(我上次检查过),因此您必须使用 UNION。
FULL OUTER JOIN
将连接所有三个基本查询而不会丢失行。回答后续问题
或者,以多种方式使用基数:
To
UNION
the resulting rows of all three queries and then pick the 5 rows with the highestamount
:The manual:
UNION ALL
to keep duplicates.To add the counts for every
event_id
:The tricky part here is that not every
event_id
will be present in all three base queries. So take care that aJOIN
does not lose rows completely and additions don't turn outNULL
.Use
UNION ALL
, notUNION
. You don't want to remove identical rows, you want to add them up.x
is a table alias and shorthand forAS x
. It is required for for a subquery to have a name. Can be any other name here.The SOL feature
FULL OUTER JOIN
is not implemented in MySQL (last time I checked), so you have to make do withUNION
.FULL OUTER JOIN
would join all three base queries without losing rows.Answer to follow-up question
Or, to use the base counts in multiple ways: