从Table获取每个用户的最后5行

时间:2018-06-06 15:35:01

标签: mysql sql

我需要你帮助解决这个问题。

情境:

用户可以投票任意数量的歌曲,但我只想选择每位用户最近5票。我不想选择最后5票。

表格结构:

klick here

示例:

3位用户

  • Karl,投了10首歌
  • 罗伯特,投了6首歌
  • Joanne,投了3首歌

我想让Karls获得最后5票,Roberts获得5票,Joannes获得3票。

当前SQL查询:

SELECT songs.genre, songs.title, songs.band, COUNT(*) AS anzahlVotes, users.name
FROM T_Songs AS songs
RIGHT JOIN T_Votes AS votes ON songs.P_Song_id = votes.F_Song_id
LEFT JOIN T_Users AS users ON votes.F_User_id = users.P_User_id
WHERE votes.P_Vote_id IN (
    SELECT P_Vote_id
    FROM T_Votes
    GROUP BY F_User_id
    HAVING P_Vote_id > MAX(P_Vote_id)-5

);

但是此查询不会返回正确的投票计数。

解决方案(感谢Gordon Linoff和Paul Spiegel)

SELECT songs.genre, songs.title, songs.band, COUNT(*) AS anzahlVotes, users.name
FROM T_Songs AS songs
RIGHT JOIN T_Votes AS votes ON songs.P_Song_id = votes.F_Song_id
LEFT JOIN T_Users AS users ON votes.F_User_id = users.P_User_id
WHERE users.nobility_house IS NOT NULL
AND votes.P_Vote_id >= coalesce(
(select votes2.P_Vote_id
from T_Votes votes2
where votes2.F_User_id = votes.F_User_id
order by votes2.P_Vote_id desc
limit 1 offset 4
), 0)
GROUP BY votes.F_User_id

2 个答案:

答案 0 :(得分:1)

假设"过去5"表示具有最高ID的五个,您可以这样做:

select v.*
from t_votes v
where v.p_vote_id >= (select v2.p_vote_id
                      from t_votes v2
                      where v2.f_user_id = v.f_user_id
                      order by v2.p_vote_id desc
                      limit 1 offset 4
                     );

我将让你弄清楚如何从其他表中引入列。

编辑:

如果少于5行:

select v.*
from t_votes v
where v.p_vote_id >= coalesce( (select v2.p_vote_id
                                from t_votes v2
                                where v2.f_user_id = v.f_user_id
                                order by v2.p_vote_id desc
                                limit 1 offset 4
                               ), p_vote_id
                             );

答案 1 :(得分:0)

您可以使用同一个表上的联接进行过滤:

SELECT songs.genre, songs.title, songs.band, COUNT(*) AS anzahlVotes, users.name
    FROM T_Songs AS songs
    RIGHT JOIN T_Votes AS votes ON songs.P_Song_id = votes.F_Song_id
    LEFT JOIN T_Users AS users ON votes.F_User_id = users.P_User_id        
LEFT JOIN (
    SELECT F_User_id, Max(P_Vote_id)-5 as MaxVoteId
    FROM T_Votes
    GROUP BY F_User_id
    ) DF ON ((DF.F_User_id = votes.F_User_id) AND (P_Vote_id >= MaxVoteId));