如何从每个组中选择前两行

时间:2015-08-20 06:44:10

标签: mysql sql

您好我们在MySql中有3个music表,如下所示:

第一桌

第一个表用于playlist表,其中music playlist存在。

playlistId           playlistTitle          categoryId

1                    hello                  0
2                    wow                    0
3                    wi-fi                  0
4                    awesome                0
5                    sixer                  1
6                    four                   1
7                    boundary               2

第二张表:

第二个表用于`songRelation表,其中每个播放列表与其歌曲

相关联
playlistId            songId

1                     4
1                     3
1                     43
1                     57
1                     98
2                     56
2                     67
2                     90
2                     78
3                     98
3                     78
3                     89
43                    90

第3表: 第3个表用于存在歌曲细节的song

songId                songTitle

4                     hello
3                     real hero
43                    singalone
57                    awesom
98                    really
78                    sakaka
98                    shikwa
89                    moha
90                    hello2
67                    Sneh

实际上我想得到这样的结果:

playlistId  songId    categoryId songTitle

1           4         0          hello
1           3         0          real hero
2           56        0          singalone
2           67        0          Sneh
3           78        0          sakaka
3           98        0          Shikwa

每个playlistId的前两个songId以及categoryIdsongTitle

3 个答案:

答案 0 :(得分:3)

您可以使用变量:

SELECT playlistId, songId, categoryId, songTitle
FROM (
  SELECT p.playlistId, s.songId, p.categoryId, s.songTitle,
         @r := IF (@pid = p.playlistId,
                   IF (@pid := p.playlistId, @r+1, @r+1),
                   IF (@pid := p.playlistId, 1, 1)) AS rn
  FROM playlist AS p
  CROSS JOIN (SELECT @r:=0, @pid:=0) AS vars
  INNER JOIN songRelation AS sr ON p.playlistId = sr.playlistId
  INNER JOIN song AS s ON sr.songid = s.songid
  ORDER BY p.playlistId, s.songId ) AS t
WHERE t.rn <= 2

变量@r用于枚举每个playlistId切片中的记录。在外部查询中使用它,我们可以轻松地为每个playlistId切片获取2条记录。

Demo here

答案 1 :(得分:1)

您可以使用JOIN执行此操作,请尝试以下代码: -

SELECT playlist.playlistid, songRelation.songId, song.songTitle
FROM playlist JOIN songRelation JOIN song
WHERE playlist.playlistId=songRelation.playlistId 
AND songRelation.songId=song.songId LIMIT 2

您可以添加类别表并使用相同的方式获取所需的结果表,您也可以通过在代码之前编写此代码将此查询保存为虚拟表VIEW: -

CREATE VIEW myView AS 

编辑: -

SELECT playlist.playlistId from playlist INNER JOIN( SELECT playlist.playlistId, songRelation.songId, song.songTitle
    FROM playlist JOIN songRelation JOIN song
    WHERE playlist.playlistId=songRelation.playlistId 
    AND songRelation.songId=song.songId 
    GROUP BY playlist.playlistId LIMIT 2)
    WHERE playlist.playlistId=songRelation.playlistId

答案 2 :(得分:0)

请尝试一下,因为它没有经过数据测试,因此如果您遇到任何问题,请创建一个sqlfiddle,以便我可以解决问题。

SELECT x.*
FROM (SELECT pl.playlistid, sng.songId, sng.songTitle,
               CASE 
                 WHEN @category != pl.playlistid THEN @rownum := 1 
                 ELSE @rownum := @rownum + 1 
               END AS rank,
               @category := pl.playlistid AS play_list
    FROM playlist AS pl 
    JOIN songRelation AS sr ON sr.playlistid=pl.playlistid 
    JOIN song AS sng ON sng.songid=sr.songid  
        JOIN (SELECT @rownum := NULL, @category := '') r     
      ORDER BY pl.playlistid,sng.songid) X
      WHERE x.rank<=2