简体   繁体   English

复杂的MySQL Select查询

[英]Complicated MySQL Select query

Here is my table layout 这是我的桌子布置

![enter image description here][1] ![在此处输入图片描述] [1]

What I would like to do: 我想做的是:

  1. Count the number of votes for each songID in a particular room (passed in as a parameter). 计算特定房间中每个songIDvotes数(作为参数传递)。
  2. Find out if the userID has at least one vote for a song in a particular room. 找出用户ID在特定房间中是否至少对一首歌有投票权。 UserID is also passed in as a parameter. UserID也作为参数传递。
  3. An extension of the two above. 以上两个的扩展。 If there are no votes for a song, I would like to return 0 for those entries. 如果某首歌没有投票,我想为这些条目返回0。

What I have: 我有的:

select a.SongID, count(*) as voteCount, b.UserID 
  from votes a left join votes b 
    on a.songID = b.songID 
   and b.RoomID = 178 
   and b.UserID = 71 
 where a.RoomID = 178 
 group by a.SongID, b.UserID

(feel free to use those RoomID and UserID numbers in examples) (在示例中可以随意使用这些RoomIDUserID号)

The above SQL statement does 1 and 2 but not 3. Is there any way to extend it to do 3? 上面的SQL语句执行1和2,但不执行3。是否可以将其扩展为3? It would be nice to get all this done in a single query. 最好在单个查询中完成所有这些操作。 Right now it returns all songs that have a vote, the number of votes and if the UserID in question has voted (null otherwise). 现在,它将返回所有具有投票的歌曲,投票数以及相关的UserID是否已投票(否则为null)。 Thanks in advance. 提前致谢。

EDIT : Just to clarify a bit, currently this statement will only output songs with entries in the votes table, im trying to modify it to include songs that do not have entries in the votes table (that is, to include all songs in Select SongID from songs where RoomID = 178 ). 编辑 :只是为了澄清一点,当前此语句将仅输出在votes表中具有条目的歌曲,即时通讯试图将其修改为包括在votes表中没有条目的歌曲(即,将所有歌曲包括在Select SongID from songs where RoomID = 178 )。 ALso keeping in mind everything is room-specific. 还请记住,所有内容都是特定于房间的。 Songs are matched to a certain room by their SongID . 歌曲通过其SongID匹配到某个房间。

Here is my song table for example: 例如,这是我的歌曲表:

+--------+--------+
| SongID | RoomID |
+--------+--------+
|   2835 |    178 |
|   2836 |    178 |
|   2837 |    178 |
|   2838 |    178 |
|   2839 |    178 |
|   2840 |    178 |
|   2841 |    178 |
|   2842 |    178 |
|   2843 |    178 |
|   2844 |    178 |
|   2845 |    178 |
|   2846 |    178 |
|   2847 |    178 |
|   2848 |    178 |
+--------+--------+

Here is the votes: 这是票数:

+--------+--------+--------+
| SongID | UserID | RoomID |
+--------+--------+--------+
|   2835 |     71 |    178 |
|   2836 |     71 |    178 |
|   2837 |     71 |    178 |
|   2838 |     71 |    178 |
|   2839 |     71 |    178 |
|   2840 |     71 |    178 |
|   2841 |     71 |    178 |
|   2842 |     71 |    178 |
+--------+--------+--------+

And finally, the result of the query I have so far: 最后,我到目前为止的查询结果:
Only the first 7 songs have votes and only those are returned. 仅前7首歌曲具有投票,并且仅返回。 I would like to modify this query to include ALL songs that are in the room, even if they have no votes (and return the vote count as 0 or null). 我想修改此查询,以包括房间中的所有歌曲,即使它们没有投票(并将投票计数返回0或null)也是如此。

+--------+-----------+--------+
| SongID | voteCount | UserID |
+--------+-----------+--------+
|   2835 |         1 |     71 |
|   2836 |         1 |     71 |
|   2837 |         1 |     71 |
|   2838 |         1 |     71 |
|   2839 |         1 |     71 |
|   2840 |         1 |     71 |
|   2841 |         1 |     71 |
+--------+-----------+--------+

I believe your query might look like this 我相信您的查询可能如下所示

SELECT s.SongID, 
       COALESCE(v.allVotes, 0) allVotes, 
       COALESCE(v.userVotes, 0) userVotes
  FROM songs s LEFT JOIN 
(
  SELECT SongId, 
         COUNT(*) allVotes,
         SUM(CASE WHEN UserID = 71 THEN 1 ELSE 0 END) userVotes
    FROM votes
   WHERE RoomID = 178 
   GROUP BY SongID
) v 
    ON s.songID = v.songID 
 WHERE s.RoomID = 178 

Here is SQLFiddle demo 这是SQLFiddle演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM