我有一个机构ID,姓名,地址和另一个课程详细信息表的查找表。
在每个课程记录中,将有两列指向主要和次要机构ID。
我的选择查询应该看起来像 - >
Select course_id,
name,
primary_Institution_id,
Primary_Institution_name,
primary_Institution_address,
Secondary _Institution_id,
Secondary _Institution_name,
Secondary_Institution_address
from [JOIN MAY BE]
where course_id in ('1223','34234','43432')
如何实现这一目标?我无法控制表格,我只能从中选择,不能修改它们的结构。
答案 0 :(得分:1)
如果您正在尝试询问如何进行加入,则可能看起来像这样
Select c.course_id,
c.name,
c.primary_Institution_id,
i.name as primary_Institution_name,
i.address as primary_Institution_address
c.secondary_Institution_id
k.name as Secondary _Institution_name,
k.address as Secondary_Institution_address
from courses as c
join institutions as i
on i.id = c.primary_Institution_id
left
join institutions as k
on i.id = c.secondary_Institution_id
where course_id in ('1223','34234','43432')
这假设第一个机构id是必需的(从不为null),因此连接被暗示为内部连接,但是第二个可能是可选的(允许为null),因此它使用左连接,以防没有任何内容匹配。