我需要计算所有tweets
和retweets
(所有转发也是推文)表中的每个用户authors
。我的第一个想法很有效:
推文柜台
SELECT a.id, a.name, count(*)
FROM authors AS a
INNER JOIN tweets AS t
ON t.fromuser_id = a.id
GROUP BY a.id, a.name
ORDER BY count(*)
转推柜台
SELECT a.id, a.name, count(*)
FROM authors AS a
INNER JOIN tweets AS t
ON t.fromuser_id = a.id AND retweet = TRUE
GROUP BY a.id, a.name
ORDER BY count(*)
...但现在我想把所有这些放在一起。我想知道是否有比这更好(更快)的方法:
合并
SELECT a.id, a.name, count(*), (
SELECT count(*)
FROM tweets
WHERE fromuser_id = a.id AND retweet = TRUE
)
FROM authors AS a
INNER JOIN tweets AS t
ON t.fromuser_id = a.id
GROUP BY a.id, a.name
ORDER BY count(*)