簡體   English   中英

計算與另一個表中的id關聯的行數

[英]count number of rows associated with their id in another table

嗨,我們有3表音樂的MySql中是這樣的:

第一表:

第一個表用於存在音樂播放列表的播放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

第三表:第三表用於存在歌曲詳細信息的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 ID playlistId will be with their first 2 songId and with their categoryId and also with songTitle`。

但我想計算每個playlistId的總song

在獲得全部歌曲結果之后,我想要的將是這樣的:

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

將添加什么子查詢以獲得以上期望的結果。

要獲得准確的結果,請使用以下命令:

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

在主查詢上使用分組依據將合並詳細的歌曲數據,從而迫使您運行兩個查詢:一個查詢詳細信息(前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

使用組功能,您可以執行以下操作:

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

我已將此查詢添加到ur 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

它正在提供所需的輸出。 此處查看演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM