简体   繁体   English

我正在尝试将4个表与INNER JOIN链接在一起

[英]I am trying to link 4 tables together with INNER JOIN

My code looks like this 我的代码看起来像这样

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;

Looking to try and get the name of the artists, genre and song to all match up and display. 希望尝试获得艺术家的姓名,流派和歌曲以进行匹配和展示。 Yet I get 可是我明白了

Unknown column 'Artists.ArtistID' in 'on clause'

Im fairy new to SQL and to INNER JOINS any help and explanations would be great! 对SQL和INNER JOINS来说,我是新手,任何帮助和解释都很棒!

Try this 尝试这个

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;

check the sequence and occurrence of tables in join 检查联接中表的顺序和出现情况

you are mention the Songs table in first row joining. 您会在第一行连接中提到Songs表。 but your joining the table of ON function in last line only like your code, you should try this method: 但是您只在最后一行中加入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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试使用内部联接将 3 个表联接在一起时出现错误 1052 - Getting Error 1052 When trying to join 3 tables together with inner join MySQL试图一起使用INNER JOIN和UNION吗? - MySQL trying to use INNER JOIN and UNION together? 试图找到如何将三个表链接在一起 - Trying to find how to link three tables together 错误1052我试图使用内部联接联接表,但是我一直收到此消息 - error 1052 im trying to join tables using inner join however i keep getting this message 通过内部联接和左侧外部联接将6个表联接在一起-LINQ - Joining 6 tables together with inner joins & a left outer join - LINQ 内部连接与 3 个表(第二个和第三个“分配”给第一个 - 不是全部在一起) - Inner join with 3 tables (the second and third "assigned" to the first - not all together) 如何在查询中将两个或多个表内部连接在一起? - How to inner join two or more tables together in a query? 我正在尝试使用相同的INNER JOIN函数中的2个,但是我无法使其正常工作 - I am trying to use 2 of the same INNER JOIN function but i cant get it to work 我正在尝试加入其中一个表中不存在用户记录的表 - I am trying to join tables where a user record does not exist in one of the tables 如何将这些表一起加入 - How can I Join this Tables Together
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM