0

我有一张名为 Request 的表。
其他表通过请求 id 链接到请求表。有一个 TwitterTweet 表和一个 FacebookPost 表。因此,一个请求可以有 50 个 TwitterTweets 和/或 20 个 FacebookPosts 或任意数量的 Tweets/Posts 我们可以将它们加在一起,总数为 70。

我正在尝试创建一个查询,该查询可以告诉我总数最高的请求是什么。

我知道这是错误的:(我试图通过 TwitterTweet 中的计数对它们进行排序,但它不会让我做一个我认为会带回 Count.count 列的 OUTER JOIN。它迫使我做一个 Left Join让它编译。我的逻辑是做一个连接,以便通过requestid为每一行计算结果)

SELECT r1.`id` AS requestid, r1 . * 
FROM  `Request` AS r1
LEFT JOIN 

(SELECT COUNT( * ) AS count, rid
FROM 

((SELECT  `TwitterTweet`.`id` AS  `smid` ,  `TwitterTweet`.`requestid` AS rid
FROM  `TwitterTweet` 
WHERE  `TwitterTweet`.`requestid` = requestid
AND  `TwitterTweet`.`active` =1) AS talias

)) AS Count ON ( Count.rid = requestid ) 
ORDER BY Count.count

*当我尝试在 Facebook 端添加时,它不会再编译(概念是结果是从 TwitterTweet 添加的,而 FacebookPost 的结果附加到特定的 requestid,这会给我们一个计数。整个结果集应按该计数排序)

SELECT r1.`id` AS requestid, r1 . * 
FROM  `Request` AS r1
LEFT JOIN 

(SELECT COUNT( * ) AS count, rid
FROM 

((SELECT  `TwitterTweet`.`id` AS  `smid` ,  `TwitterTweet`.`requestid` AS rid
FROM  `TwitterTweet` 
WHERE  `TwitterTweet`.`requestid` = requestid
AND  `TwitterTweet`.`active` =1 ) AS talias 

UNION All

(SELECT `FacebookPost`.`id` AS  `smid`, `FacebookPost`.`requestid` AS rid
FROM  `FacebookPost`
WHERE  `FacebookPost`.`requestid` = requestid
AND  `FacebookPost`.`active` = 1) as falias 

)) AS Count ON ( Count.rid = requestid ) 
ORDER BY Count.count

我更新了查询并尝试添加别名:

SELECT rid, SUM(count) total_count

FROM 

(
(SELECT COUNT(*) AS count, r.rid
FROM   request r
       JOIN TwitterTweet tt
       ON r.id = tt.requestid
WHERE  tt.active = 1
GROUP BY r.rid) AS twitter

UNION ALL

(SELECT COUNT(*) AS count, r.rid
FROM   request r
       JOIN FacebookPost fp
       ON r.id = fp.requestid
WHERE  fp.active = 1
GROUP BY r.rid ) AS fbook
)

GROUP BY rid

ORDER BY SUM(count) DESC

我进行了另一次调整,给中间子查询一个别名,但现在我只得到一行返回,rid 列中为零,total_count 列中为 5686……5686 可能是所有结果。

SELECT counts.rid, SUM(count) total_count

FROM 

(
SELECT COUNT(*) AS count, r.requestid AS rid
FROM   request r
       JOIN TwitterTweet tt
       ON r.id = tt.requestid
WHERE  tt.active = 1
GROUP BY r.requestid

UNION ALL

SELECT COUNT(*) AS count, r.requestid AS rid
FROM   request r
       JOIN FacebookPost fp
       ON r.id = fp.requestid
WHERE  fp.active = 1
GROUP BY r.requestid
) AS counts

GROUP BY counts.rid

ORDER BY SUM(count) DESC

知道了!!!感谢您的帮助,我不得不根据请求删除这些连接:

SELECT counts.rid, SUM(count) total_count

FROM 

(
SELECT COUNT(*) AS count, tt.requestid AS rid
FROM  TwitterTweet tt
WHERE  tt.active = 1
GROUP BY tt.requestid

UNION ALL

SELECT COUNT(*) AS count, fp.requestid AS rid
FROM   FacebookPost fp
WHERE  fp.active = 1
GROUP BY fp.requestid
) AS counts

GROUP BY counts.rid

ORDER BY SUM(count) DESC
4

1 回答 1

1
SELECT id, SUM(count) total_count

FROM 

(
SELECT COUNT(*) AS count, r.id
FROM   request r
       JOIN TwitterTweet tt
       ON r.id = tt.requestid
WHERE  tt.active = 1
GROUP BY r.id

UNION ALL

SELECT COUNT(*) AS count, r.id
FROM   request r
       JOIN FacebookPost fp
       ON r.id = fp.requestid
WHERE  fp.active = 1
GROUP BY r.id
) sub

GROUP BY id

ORDER BY SUM(count) DESC
;
于 2012-10-11T18:41:35.300 回答