0

I have a table e.g.

Artist     Title      Song Key
A          Title A    A
A          Title A    B
A          Title A    F
B          Title B    A

I want to return each individual song but also show how many versions there are of that song e.g. Ideal results would be:

Artist     Title       How_many
A          Title A     3
B          Title B     1

This is what I'm trying but the count counts all tracks and not just from the current line:

SELECT *, 
       ( select count(*) 
           from tracks 
          where artist=artist 
            AND title=title ) as How_many 
  from tracks
 where easytosing='Yes'
 group by title, artist
 order by track_id desc

Is this possible using a nested query or is there a better way?

4

5 回答 5

1

尝试这个:

SELECT Artist, title, count(*) as How_many 
FROM tracks
WHERE easytosing='Yes'
GROUP BY Artist,Title
于 2013-09-02T07:20:32.377 回答
1
SELECT t.*, how_many
        from tracks t
inner join 
( select title,artist, count(*)  ,how_many
           from tracks 
          group by artist,title ) as temp
temp.title=t.title and temp.artist=t.artist
 where easytosing='Yes'
于 2013-09-02T07:33:57.810 回答
0
SELECT Artist,Title, COUNT(*) FROM TABLE1 GROUP BY Artist,Title

样品小提琴

于 2013-09-02T07:21:34.120 回答
0

只需简单地您可以使用 group by 执行以下查询

  select Artist,Title,count(Song_Key) as How_many
  from Tracks
  group by Artist,Title

执行上述查询后,您将获得所需的结果。

   ARTIST   TITLE   HOW_MANY
     A      Title A     3
     B      Title B     1

0.00 秒内返回 2 行

于 2013-09-02T08:59:30.890 回答
-1

在具体情况下:

SELECT title, artist, count(*)
from tracks
where easytosing='Yes'
group by title, artist

因为您可以为每个艺术家/标题获得歌曲的数量。

在这种情况下,您不需要子查询。

您可以执行的另一个查询是主要选择基于磁盘表的查询(我认为您有一个磁盘表,其中您在艺术家处有一个外键和一个磁盘名称),您可以在其中获得许多包含的歌曲每个艺术家/磁盘。

这样,如果一张磁盘没有歌曲,您将获得零价值。

于 2013-09-02T07:20:34.173 回答