2012-12-01 86 views
1

我需要進行復雜的查詢。 我有表:選擇藝術家專輯列表並顯示每個專輯中已經請求了藝術家和專輯的列表中的曲目數量

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

索裏對問題的髒標題,但我不明白如何更簡要解釋它。

問題: 如何顯示請求的藝術家的專輯列表和每個顯示的專輯計算在單個查詢中請求當前專輯中的藝術家的曲目的計數。

我已經試過這樣的事情:

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": syntax error:

回答

2

不知道,爲什麼這是複雜的。下面的查詢應該返回每個搜索藝術家專輯曲目的計數:

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

非常感謝你,查詢比我想象的更容易。 – testCoder

0

這並不複雜,但很難閱讀。我第一眼就看到了這個錯誤。這裏:

as albtbl, FROM tracks 

逗號是多餘的。所以把它改爲:

as albtbl FROM tracks 

希望它有幫助。

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