1
這個問題讓人討厭,因爲我還沒有在sql中使用中間表的煩惱。但是,我在一時間趕時間,所以我選擇反問。從第三張表中檢索數據
我希望結合這些SQL代碼,這樣我就可以在同一個轉發器中從「老師」以及「團隊」和「級別」中檢索數據。
我是從「老師」表通過FK_teacher從middletable「teacher_team」這是通過FK_team與我的球隊表醒目的名稱
難道任何機會有可能這些所謂聯合,我可以通過一箇中繼器提取它?
// Team <-> level relation
SELECT
team.team_id as team_id,
level.level as level,
FROM team
INNER JOIN level ON level.level_id = team.team_FK_level
WHERE team.team_FK_type = @id
// Team <-> Team_Teacher <-> Teacher relation
SELECT teacher.teacher_name as name
FROM teacher WHERE teacher.teacher_id
IN (
SELECT teacher_team.FK_teacher
FROM teacher_team
INNER JOIN team ON team.team_id = teacher_team.FK_team
WHERE team.team_FK_type = @id
)
*編輯* 得到這個代碼的基礎上拉維·辛格的答案工作。我遇到了另一個問題。 如果在teacher_teams中有兩位教師與其相關(如果有),我的轉發器會輸出兩次隊伍。有沒有什麼辦法可以合併這些,而不需要在中繼器內部建立中繼器?
SELECT
team.team_id as team_id,
level.level as level,
teacher_team.FK_teacher,
teacher.teacher_name as teacher
FROM team
INNER JOIN level ON level.level_id = team.team_FK_level
LEFT JOIN teacher_team on teacher_team.FK_hold = team.team_id
LEFT JOIN teacheron teacher.teacher_id = teacher_team.FK_teacher
WHERE team.team_FK_type = @id
這不是加盟的水平。 我的團隊<-> Team_Teacher <->老師關係做同樣的事情 – GentlemenFinn
@GentlemenFinn:這有幫助嗎? –
我轉過身來,因爲團隊是我的主桌。 但它的工作是=)我不知道我可以抓住每一張我想通過內部加入中間表的表。 – GentlemenFinn