我正在尝试从我的Album
和ArtistBand
表中进行选择,并按字母顺序显示Name
和Bandname
。当尝试使用下面的select语句时,它会显示每个乐队的所有专辑。任何帮助都将受到学校的赞赏。
CREATE TABLE ArtistBand
(
ArtistID INT AUTO_INCREMENT PRIMARY KEY,
BandName VARCHAR(255) NOT NULL,
NameOfMembers VARCHAR(255),
NumberOfMembers INT
);
CREATE TABLE Genre
(
GenreID INT AUTO_INCREMENT PRIMARY KEY,
GenreType VARCHAR(255) NOT NULL
);
CREATE TABLE Album
(
AlbumID INT AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR (255) NOT NULL,
ReleaseDate DATE NOT NULL,
Producers VARCHAR (255),
ArtistID INT,
GenreID INT,
FOREIGN KEY(ArtistID) REFERENCES ArtistBand(ArtistID),
FOREIGN KEY (GenreID) REFERENCES Genre (GenreID)
);
CREATE TABLE LookUp
(
AlbumID INT,
GenreID INT,
FOREIGN KEY (AlbumID) REFERENCES Album (AlbumID),
FOREIGN KEY (GenreID) REFERENCES Genre (GenreID)
);
INSERT INTO ArtistBand
VALUES
(NULL,'ACDC','BrianJohnson',4),
(NULL,'Metallica','JamesHeitfield',4),
(NULL,'TheBeatles','JohnLennon',4),
(NULL,'JayZ','ShawnCarter',1),
(NULL,'DangerMouse','BrianBurton',1);
INSERT INTO Album
VALUES
(NULL,'BackInBlack','1980-01-01',NULL,NULL,NULL),
(NULL,'TheBlackAlbum','1991-01-01',NULL,NULL,NULL),
(NULL,'WhiteAlbum','1968-01-01',NULL,NULL,NULL),
(NULL,'TheBlackAlbum','2003-01-01',NULL,NULL,NULL),
(NULL,'TheGreyAlbum','2004-01-01',NULL,NULL,NULL);
INSERT INTO Genre
VALUES
(NULL,'HeavyMetal'),
(NULL,'HeavyMetal'),
(NULL,'Pop'),
(NULL,'HipHop'),
(NULL,'HipHop');
SELECT
ArtistBand.BandName, Album.Name
FROM
ArtistBand
JOIN
Album
ORDER BY
ArtistBand.BandName;
答案 0 :(得分:3)
您需要在联接中添加条件,ArtistID
在两个表中应该相同。因此,您的查询将如下所示:
SELECT ArtistBand.BandName, Album.Name
FROM ArtistBand
JOIN Album ON ArtistBand.ArtistID = Album.ArtistID
ORDER BY ArtistBand.BandName;