选择艺术家专辑列表并显示已在列表中请求艺术家和专辑的每个专辑曲目计数

时间:2012-12-01 21:52:42

标签: sql sqlite select group-by

我需要进行复杂的查询。 我有桌子:

CREATE TABLE IF NOT EXISTS tracks
     ( id INTEGER PRIMARY KEY, path TEXT,
     title TEXT, artist TEXT,
     album TEXT, genre TEXT, 
    duration INTEGER, rating INTEGER );

Sory提出了问题的肮脏标题,但我不知道如何更快地解释它。

问题: 如何显示所请求艺术家的专辑列表以及每个显示的专辑,计算单个查询中当前专辑中请求艺术家的曲目数。

我尝试过这样的事情:

SELECT albtbl.album as album, albtbl.artist as artist, 
( SELECT count(*) FROM trackstbl WHERE trackstbl.artist = albtbl.aritst ) as tracksCount 
FROM ( SELECT tbl.album as album, tbl.artist as artist, count(title) as tracksCount FROM tracks as tbl  WHERE tbl.artist = 'requested_artist'
 GROUP BY tbl.album ) as albtbl, FROM tracks as trackstbl ORDER BY album ASC

但未编译:

  

SQLiteException:near“FROM”:语法错误:

3 个答案:

答案 0 :(得分:2)

不确定,为什么这很复杂。以下查询应返回每个搜索艺术家专辑的曲目计数:

     select artist, album, count(*) as tracksCount
     from tracks
     where artist = 'requested_artist'
     group by artist, album;

答案 1 :(得分:1)

select artist, album, count(1) as countTracks
from tracks
where artist = @requestedArtist
group by artist, album

答案 2 :(得分:0)

它并不复杂,但很难阅读。我第一眼就看到了这个错误。这里:

as albtbl, FROM tracks 

逗号是额外的。所以改成它:

as albtbl FROM tracks 

希望它有所帮助。