我只是想创建一个新的播放列表,其中包含存储的所有曲目中最长的10首歌曲。这是我的代码和下面的架构,请告诉我如何将播放列表和Track的foriegn键与PlaylistTrack关联起来?
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';
SCHEMA:
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 :(得分: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