gpt4 book ai didi

mysql - 我正在尝试使用 INNER JOIN 将 4 个表链接在一起

转载 作者:行者123 更新时间:2023-11-30 00:26:55 24 4
gpt4 key购买 nike

我的代码如下所示

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;

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

关于mysql - 我正在尝试使用 INNER JOIN 将 4 个表链接在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22802497/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com