[英]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 : 嗨,我们有3表音乐的MySql中是这样的:
1st Table : 第一表:
the first table is for playlist
table where music playlist is exist. 第一个表用于存在音乐播放列表的播放
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
2nd Table : 第二表:
2nd table is for songRelation
table where every playlist is associated with thier song 第二个表用于
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
3rd Table : the 3rd table is for song
table where song detail exist 第三表:第三表用于存在歌曲详细信息的
song
表
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`. 其中每个
playlistId will be with their first 2
ID 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
但我想计算每个
playlistId
的总song
数
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 这是jsfiddle演示,其中查询没有
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). 在主查询上使用分组依据将合并详细的歌曲数据,从而迫使您运行两个查询:一个查询详细信息(前4个字段),第二个查询恢复总计(最后一列)。 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. 由rlanvin建议,这种方式应该使查询速度更快,因为在计算每一行的子查询时,它只计算一次,然后加入主查询。 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 . 我已将此查询添加到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
在此处查看演示
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.