简体   繁体   中英

count number of rows associated with their id in another table

Hi We have 3 Table of a music which is something like this in MySql :

1st Table :

the first table is for playlist table where music playlist is exist.

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

2nd Table :

2nd table is for songRelation table where every playlist is associated with thier song

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

3rd Table : the 3rd table is for song table where song detail exist

songId                songTitle

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

actually i am fetching the result something like this:

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

where the every playlistId will be with their first 2 songId and with their categoryId and also with songTitle`.

but i want to count the total song with every playlistId

after getting the total song result i want will be something like this :

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

here is the jsfiddle Demo where query is without totalSong http://sqlfiddle.com/#!9/7eda7/5

What subquery will be added to get the above desired result.

To get exactly the result you asked, use this:

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

Using a group by on the main query would merge the detailed song data, forcing you to run two queries: one for details (first 4 fields) and a second query, to recover the totals (last column). Using this solution, you get all detailed data and totals, the sub-query will recover the count of songs for each playlist, the way you asked.

UPDATE:

This way, suggested by rlanvin, should make the query faster, because instead on computing the subquery for each row, it gets computed only once, and then is joined to the main query. The result is the same:

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

Using Group Functions you can do this:

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

I have added this query to 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

It is giving the required output. Check the Demo Here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM