0

我的代码看起来像这样

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 很陌生,任何帮助和解释都会很棒!

4

2 回答 2

0

尝试这个

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;

检查连接中表的顺序和出现

于 2014-04-02T05:48:52.800 回答
0

Songs在第一行加入中提到了该表。但是你ON在最后一行加入函数表就像你的代码一样,你应该试试这个方法:

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;
于 2014-04-02T05:52:10.553 回答