SQL query that produces the following output?

时间:2015-05-24 21:50:35

标签: mysql sql relational-database

I am trying to write an SQL statement producing the below output.
I have the two following tables:

UserMovie

userID | movieID
-----------------
  135  |  k0jps
  135  |  p1zka
  125  |  v0t67
  115  |  opp2s
  111  |  xnwri
  115  |  kspdl

Follows

followerid | followingid
------------------------
    122    |     135  
    192    |     111
    125    |     240
    120    |     125
    45     |     111

I want to fetch the number of followers of each user who's userid is in the UserMovie Table, giving the following result:

Result

userid | followerCount
----------------------
  135  |       1
  125  |       1
  115  |       0
  111  |       2

The following statement gives me partially what i want:

SELECT followingid, count(*) as followerCount   
FROM Follows   
WHERE followingid in (SELECT DISTINCT userID FROM UserMovie)   
GROUP BY followingid  

The issue with the above query is that users with 0 followers do not appear in the results giving the following output:

userid | followerCount
----------------------
  135  |       1
  125  |       1
  111  |       2

Any idea on how to do it?

4 个答案:

答案 0 :(得分:1)

Try this to include users with no follows:

SELECT  UserId, Count(followerid) AS followerCount
FROM (SELECT DISTINCT userId FROM UserMovie ) m
LEFT JOIN Follows f
ON f.followingid = m.userID
GROUP BY UserId

Now it generates :

UserId  followerCount
111 2
115 0
125 1
135 1

答案 1 :(得分:0)

Here's one approach: get a distinct list of userID from UserMovie in an inline view (use either a GROUP BY or a DISTINCT keyword), and perform an "outer join" operation of that to the Followers table to find followers. Collapse the rows from that with a GROUP BY, and use an aggregate function to get a count of unique/distinct non-null values of userId from the Followers table.

For example:

SELECT u.userID
     , COUNT(DISTINCT f.userID) AS cnt_followers
  FROM ( SELECT m.userID
           FROM UserMovie m
          GROUP BY m.userID
       ) u
  LEFT
  JOIN Follows f
    ON f.followingid = u.userID
 GROUP BY u.userID

EDIT

There's an invalid column reference in the SELECT list, f.userID is not valid. That should be f.followerID.

When we fix that, the query returns:

userID  cnt_followers
111     2
115     0
125     1
135     1

SQL Fiddle HERE http://sqlfiddle.com/#!9/de3e7/2

As long as we are counting "distinct" followerid (question doesn't give any guarantee that (followerID,followingID) is UNIQUE in Followers table), we could eliminate the inline view

SELECT u.userID
     , COUNT(DISTINCT f.userID) AS cnt_followers
  FROM UserMovie u
  LEFT
  JOIN Follows f
    ON f.followingid = u.userID
 GROUP BY u.userID

答案 2 :(得分:0)

以下对我有用。 但是对于没有关注者的用户,我得到NULL而不是0

SELECT DISTINCT u.userid, t.followerCount   
FROM UserMovie u  
LEFT JOIN (   
SELECT followingid, count(*) AS followerCount      
FROM Follows      
WHERE followingid in (SELECT DISTINCT userID FROM UserMovie)      
GROUP BY followingid ) as t   
on t.followingid = u.userid

答案 3 :(得分:0)

使用CASE的解决方案怎么样?

SELECT userId,
CASE
  WHEN IFNULL(followerid, 0) = 0 THEN 0
  ELSE count(*)
END
FROM UserMovie
LEFT JOIN Follows on followingid=userID
GROUP BY userId;

似乎在SQLite3中工作正常,只需用ISNULL(如果是SQLServer)或任何其他等效项替换IFNULL。它与你所做的非常相似。