简体   繁体   English

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

[英]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.

相关问题 在Codeigniter中,获取包含ID号的行数,该ID号是另一个表中的主键 - In Codeigniter, get the count of rows that including an ID number which is primary key in another table php计算数组字段的出现并递增另一个字段的值以打印与ID相关的行 - php count occurences of an array field and increment the value of another field to print rows associated to ID 如何计算一列与另一张表的一列匹配的行数 - How to COUNT the number of rows where a column matches a column of another table 计算单个查询中另一个表的行数 - Count number of rows from another table in single query 计算特定ID的行数 - Count number of rows for specific ID MySQL:如何返回表中的所有行,并从另一个表中计算具有匹配ID的行数 - MySQL: How to return all rows in a table and count the amount of rows with matching ID from another table 尝试返回一个表中的所有行并计算另一个表中具有匹配ID的行数 - try to return all rows in a table and count the amount of rows with matching ID from another table COUNT()正在选择表中的行数 - COUNT() is selecting number of rows in table 计算具有一个值的行数和具有另一个值的行数,并输出表中出现的最低值 - Count number of rows with one value and rows with another value, and output lowest appearing value in table 计算一个表中的家庭成员数,用于另一张名为 house 的表中的每一行? - Count number of family members from one table for each rows in another table called house?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM