我的代码看起来像这样
CREATE TABLE Genre (
genreID INT NOT NULL DEFAULT 0,
genreName VARCHAR(20) NULL,
PRIMARY KEY (genreID));
CREATE TABLE Artists (
ArtistID INT NOT NULL DEFAULT 0,
name VARCHAR(45) NULL,
Genre_genreID INT NOT NULL,
PRIMARY KEY (ArtistID),
FOREIGN KEY (Genre_genreID)
REFERENCES Genre(genreID));
CREATE TABLE Albums (
albumsID INT NOT NULL DEFAULT 0,
name VARCHAR(45) NULL,
Artists_ArtistID INT NOT NULL,
PRIMARY KEY (albumsID),
FOREIGN KEY (Artists_ArtistID)
REFERENCES Artists(ArtistID));
CREATE TABLE Songs (
songID INT NOT NULL,
name VARCHAR(45) NULL,
length TIME NULL,
Albums_albumsID INT NOT NULL DEFAULT 0,
PRIMARY KEY (songID),
FOREIGN KEY (Albums_albumsID)
REFERENCES Albums (albumsID));
SELECT Artists.name, Genre.genreName, Songs.name
FROM Songs
INNER JOIN Genre ON Artists.ArtistID=Genre.genreID
INNER JOIN Artists ON Albums.Artists_ArtistID=Artists.ArtistID
INNER JOIN Albums ON Songs.Albums_albumID=Albums.albumsID;
希望尝试获得艺术家的姓名,流派和歌曲以进行匹配和展示。可是我明白了
Unknown column 'Artists.ArtistID' in 'on clause'
对SQL和INNER JOINS来说,我是新手,任何帮助和解释都很棒!
最佳答案
尝试这个
SELECT Artists.name, Genre.genreName, Songs.name
FROM Songs
INNER JOIN Albums ON Songs.Albums_albumID=Albums.albumsID
INNER JOIN Artists ON Albums.Artists_ArtistID=Artists.ArtistID
INNER JOIN Genre ON Artists.ArtistID=Genre.genreID;
检查联接中表的顺序和出现情况