2017-05-28 165 views
0

我只是想創建一個新的播放列表,其中包含所有存儲的曲目中最長的10首歌曲。這裏是我的代碼和下面的架構,請解釋一下,請解釋我如何將播放列表和軌道的foriegn鍵關聯到PlaylistTrack?在SQLite3中關聯外鍵

INSERT INTO Playlist (PlaylistID, Name) 
VALUES (19, 'Background Music'); 

SELECT TrackID, Name, Millisecond 
FROM Track 
ORDER BY Millisecond DESC 
LIMIT 10 
JOIN Track.TrackID = PlaylistTrack.TrackID 
WHERE Playlist.Name = 'Background Music'; 

架構:

CREATE TABLE Playlist 
(
    PlaylistID INTEGER PRIMARY KEY NOT NULL, 
    Name TEXT 
); 



    CREATE TABLE PlaylistTrack 
    (
     PlaylistID INTEGER NOT NULL, 
     TrackID INTEGER NOT NULL, 
     PRIMARY KEY (PlaylistID, TrackID), 
     FOREIGN KEY (PlaylistID) REFERENCES Playlist (PlaylistID) 
        ON DELETE NO ACTION ON UPDATE NO ACTION, 
     FOREIGN KEY (TrackID) REFERENCES Track (TrackID) 
        ON DELETE NO ACTION ON UPDATE NO ACTION 
    ); 




    CREATE TABLE Track 
(
    TrackID INTEGER PRIMARY KEY NOT NULL, 
    Name TEXT NOT NULL, 
    AlbumID INTEGER, 
    MediaTypeID INTEGER NOT NULL, 
    GenreID INTEGER, 
    Composer TEXT, 
    Millisecond INTEGER NOT NULL, 
    Byte INTEGER, 
    UnitPrice REAL NOT NULL, 
    FOREIGN KEY (AlbumID) REFERENCES Album (AlbumID) 
       ON DELETE NO ACTION ON UPDATE NO ACTION, 
    FOREIGN KEY (GenreID) REFERENCES Genre (GenreID) 
       ON DELETE NO ACTION ON UPDATE NO ACTION, 
    FOREIGN KEY (MediaTypeID) REFERENCES MediaType (MediaTypeID) 
       ON DELETE NO ACTION ON UPDATE NO ACTION 
); 

感謝。

回答

0

嘗試放置LIMIT條款在它所屬的查詢的末尾:

SELECT 
    t.TrackID, t.Name, t.Millisecond 
FROM Track t 
INNER JOIN PlaylistTrack p 
    ON t.TrackID = p.TrackID 
WHERE p.Name = 'Background Music' 
ORDER BY t.Millisecond DESC 
LIMIT 10 
+0

嘿感謝FOT的幫助下,我得到了一個錯誤與代碼,所以我試圖測試一些變化。此代碼給我沒有錯誤,但也沒有答案哈哈,你能給我任何指針? SELECT t.TrackID,t.Name,t.Millisecond FROM軌道T INNER JOIN PlaylistTrack p ON t.TrackID = p.TrackID WHERE p.PlaylistID = 19 ORDER BY t.Millisecond DESC LIMIT 10; –

+0

@JamesPeters我的答案有一個流浪分號。如果你盲目地剪下和粘貼上面給出的東西,你會得到一個錯誤。關於你的第二個查詢,你的表中可能沒有匹配的記錄。 –