0

这是我的两张表:
歌曲:

+--------+---------------------+
| SongID |    SongDeviceID     |
+--------+---------------------+
|   3278 | 1079287588763212246 |
|   3279 | 1079287588763212221 |
|   3280 | 1079287588763212230 |
+--------+---------------------+

投票:

+--------+--------+
| SongID | UserID |
+--------+--------+
|   3278 |     71 |
|   3278 |     72 |
|   3279 |     71 |
+--------+--------+

我正在尝试计算表中的条目数votes(对于每个唯一SongID并返回SongDeviceID最多的条目。

这是我到目前为止所拥有的:

SELECT Songs.SongDeviceID, COUNT(*) FROM Votes INNER JOIN Songs ON Votes.SongID = Songs.SongId GROUP BY Votes.SongID, Songs.SongDeviceID ORDER BY count(*) DESC

返回:

+---------------------+----------+
|    SongDeviceID     | Count(*) |
+---------------------+----------+
| 1079287588763212246 |        2 |
| 1079287588763212221 |        1 |
+---------------------+----------+

更新: 查询已更新以处理获取 SongDeviceID 但我仍然需要帮助仅返回第一行。

4

3 回答 3

1

首先

SELECT  SongID,COUNT(*) from votes GROUP BY SongID order by count(*) DESC LIMIT 1

然后你想要歌曲设备名称

SELECT SongID,COUNT(*),SongDeviceID
from
  votes 
left join 
   Songs on votes.songid = song.songid
GROUP BY
   SongID 
order by 
   count(*) DESC LIMIT 1
于 2013-08-28T07:15:38.437 回答
1

尝试这个

   SELECT Songs.SongDeviceID, COUNT(*) FROM Votes INNER JOIN Songs ON Votes.
    SongID =  Songs.SongId GROUP BY Votes.SongID, Songs.SongDeviceID ORDER BY count(*) 
    DESC LIMIT 1
于 2013-08-28T07:18:21.760 回答
0
select top 1
    S.SongDeviceID, COUNT(distinct V.UserID) as 'UserVotes'
from
   Songs as S
join 
  Votes as V on V.SongID = S.SongID
group by
   S.SongDeviceID
order by
  UserVotes DESC
于 2013-08-28T07:11:19.673 回答