簡體   English   中英

SQL使用ID選擇記錄

[英]SQL select records using id

我有一張桌子叫娛樂。 娛樂中的列包括來自不同表的主鍵

娛樂中的列為:

EntertainmentID|AgentID|GenreID

我還有一個名為“ Band”的表,它是娛樂娛樂的子組之一(不需要另一個)

我想要一個查詢,輸出:代理詳細信息和樂隊名稱,流派=搖滾

我不確定該怎么做

代理中的列是

AgentID|AgentName|AgentMobile

類型中的列是

GenreID|GenreName

帶中的列是

EntertainmentID|BandName

這是到目前為止我得到的:

SELECT Concat(a.AgentFName,' ', a.AgentLName) AS 'Agent Name', a.AgentMobile, a.AgentEmail, b.BandName
FROM Agent a 
Join Band b on a.AgentID = b.EntertainmentID
WHERE Genre IN
(SELECT GenreName
FROM Genre
WHERE Genre='rock');

我收到一條錯誤消息,提示GenreName是未知列

干得好:

SELECT Concat(a.AgentFName,' ', a.AgentLName) AS 'Agent Name', a.AgentMobile, a.AgentEmail, b.BandName
FROM Agent a
--Join Entertainment on Agent AS Entertainment holds a foreign key to Agent
JOIN Entertainment e on e.AgentId = a.AgentId 
--Join Band on Entertainment as Band holds a foreign key to Entertainment
Join Band b on b.EntertainmentId = e.EntertainmentId
--Finally join Genre on Entertainment as Entertainment holds a foreign key to Genre
JOIN Genre g on g.GenreId = e.GenreId
WHERE g.GenreName = 'rock' --Filter down to only rows where GenreName is 'rock

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM