我想从两个表中获取四个记录。 两个记录中有两个记录,两个记录记录。
SELECT sum( a.user_follower_count ) AS mention_reach,
count( a.sno ) AS mention_count,
sum( b.user_follower_count ) AS retweet_reach,
count( b.username ) AS count_retweet
FROM twitter_mention AS a, twitter_retweet AS b
这里提到了read_reach和retweet_reach,但计数值是否出错? 这里有什么不对吗?
我在twitter_retweet中只有6条记录,在twitter_retweet中只有10条记录,但它为两个计数值提供了60条记录。
答案 0 :(得分:0)
您的表a
和b
之间没有加入。
添加JOIN b ON
或者在WHERE部分
中添加显式连接更新
select *
from
(SELECT
sum( a.user_follower_count ) AS mention_reach,
count( a.sno ) AS mention_count
FROM twitter_mention AS a) a,
(SELECT
sum( b.user_follower_count ) AS retweet_reach,
count( b.username ) AS count_retweet
FROM twitter_retweet AS b) b
答案 1 :(得分:0)
您的代码正在进行完全加入,即。
对于表a中的每条记录,请给我表b中的所有记录。
SELECT sum( a.user_follower_count ) AS mention_reach,
count( a.sno ) AS mention_count,
sum( b.user_follower_count ) AS retweet_reach,
count( b.username ) AS count_retweet
FROM twitter_mention AS a, twitter_retweet AS b
您需要做的是,确定表b中每个记录的表b中的哪条记录,而不是所有记录。这是通过连接
上的ON子句完成的 SELECT sum( a.user_follower_count ) AS mention_reach,
count( a.sno ) AS mention_count,
sum( b.user_follower_count ) AS retweet_reach,
count( b.username ) AS count_retweet
FROM twitter_mention AS a
JOIN twitter_retweet AS b ON a.<somefield> = B.<somefield>
此外,您应该使用JOIN关键字更新JOIN的语法。您可以看到JOIN语法是我上面的第二个查询。
如果您没有任何公共密钥,我建议使用UNION条款进行两次查询,即
SELECT 'MENTION' as Type,sum( a.user_follower_count ) AS reach,
count( a.sno ) AS count
FROM twitter_mention AS a
UNION
SELECT 'RETWEET' as Type,sum( b.user_follower_count ) ,
count( b.username )
FROM twitter_retweet AS b