计算与另一个表中的id关联的行数

时间:2015-08-20 14:11:48

标签: php mysql sql

嗨我们在MySql中有3个音乐表:

第一张表:

第一个表用于存在音乐播放列表的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 will be with their first 2 songId and with their categoryId and also with songTitle`。

但我希望每song

计算总playlistId 获得我想要的总歌曲结果之后

会是这样的:

playlistId  songId    categoryId songTitle       totalSong

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

这里是jsfiddle演示,其中查询没有totalSong http://sqlfiddle.com/#!9/7eda7/5

将添加什么子查询以获得上述所需结果。

3 个答案:

答案 0 :(得分:1)

要准确获得您要求的结果,请使用以下命令:

select p.playlistId, 
       s.songId, 
       p.categoryId, 
       s.songTitle, 
       (select count(*) from songRelation where playlistId = p.playlistId) totalSong
from playlist p 
inner join songRelation r on p.playlistId = r.playlistId
inner join song s on r.songId = s.songId

在主查询上使用group by将合并详细的歌曲数据,强制您运行两个查询:一个用于详细信息(前4个字段)和第二个查询,以恢复总计(最后一列)。使用此解决方案,您可以获得所有详细数据和总计,子查询将按照您提出的方式恢复每个播放列表的歌曲数量。

<强>更新

这种方式,由rlanvin建议,应该使查询更快,因为相反计算每行的子查询,它只计算一次,然后加入主查询。结果是一样的:

select p.playlistId, 
       s.songId, 
       p.categoryId, 
       s.songTitle, 
       r1.totalSong
from playlist p 
inner join songRelation r on p.playlistId = r.playlistId
inner join song s on r.songId = s.songId
inner join (SELECT playlistid, COUNT(songId) as totalSong from songRelation group by playlistid) r1 on p.playlistId = r1.playlistId

答案 1 :(得分:0)

使用组功能可以执行以下操作:

SELECT `playlistid`, COUNT(`songid`)
  FROM `playlist`
  GROUP BY `playlistid`

答案 2 :(得分:0)

我已将此查询添加到您的SQLFIDDLE

SELECT p.playlistId, s.songId, p.categoryId, s.songTitle,
    (select count(sr1.songId) from songRelation sr1 
     where sr1.playlistid=p.playlistid 
     group by sr1.playlistId) as total,
       @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

它提供所需的输出。查看Demo Here