通过连接从两个SQL表中获取所有数据

时间:2013-10-09 10:44:43

标签: sql sql-server

我有下面的查询,它创建了两个表之间的连接,它正在工作fint但我想也显示在“T”中没有出现在D中的所有数据。这只显示来自的Twwet ID在两者中,我是否还可以显示在D中不是Apper但在T

中执行的Ones
SELECT     
  t.ClientId,  
  t.PageName, 
  t.Country, 
  t.CreatedAt,
  t.TweetId,
  t.TweetMessage, 
  t.Replied, 
  t.RetweetCount, 
  t.FavoritedCount, 
  t.en_Media_type, 
  Sum(d.FollowersCount) As TotalFollowers
FROM dbo.[Tweets(HT)] AS t
INNER JOIN dbo.RetweetersData AS d ON d.TweetID = t.TweetId
WHERE t.ClientId = 54 
GROUP BY t.ClientId,  
  t.PageName, 
  t.Country, 
  t.CreatedAt,
  t.TweetId,
  t.TweetMessage, 
  t.Replied, 
  t.RetweetCount, 
  t.FavoritedCount, 
  t.en_Media_type

2 个答案:

答案 0 :(得分:4)

更改

INNER JOIN dbo.RetweetersData ...

LEFT JOIN dbo.RetweetersData ...

请参阅this explanation of joins

答案 1 :(得分:1)

我认为您需要使用Left join而不是Inner join 尝试以下

SELECT     
  t.ClientId,  
  t.PageName, 
  t.Country, 
  t.CreatedAt,
  t.TweetId,
  t.TweetMessage, 
  t.Replied, 
  t.RetweetCount, 
  t.FavoritedCount, 
  t.en_Media_type, 
  Sum(d.FollowersCount) As TotalFollowers
FROM dbo.[Tweets(HT)] AS t
Left JOIN dbo.RetweetersData AS d ON d.TweetID = t.TweetId
WHERE t.ClientId = 54 
GROUP BY t.ClientId,  
  t.PageName, 
  t.Country, 
  t.CreatedAt,
  t.TweetId,
  t.TweetMessage, 
  t.Replied, 
  t.RetweetCount, 
  t.FavoritedCount, 
  t.en_Media_type