这个问题很烦人,因为我还没有掌握在 sql 中使用中间表的窍门。但是我赶时间,所以我还是选择了问。
我希望将这些 sql 代码组合起来,这样我就可以在同一个中继器中从“老师”以及“团队”和“级别”中检索数据。
我正在通过中间表“teacher_team”中的 FK_teacher 从“teacher”表中获取名称,该中间表通过 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
)
* 编辑 * 根据 Ravi Singh 的回答让这个代码工作。我遇到了另一个问题。如果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