[英]Query multiple tables in SQL Server
如果使用聯接而不是1992年前的舊語法編寫查詢,則查詢會更清晰:
select L.Name
from Manager M
join Relation R
on R.PrimaryId = M.Id
join Lead L
on R.SecondaryId = L.Id
where M.Id = 'M101'
您可以添加兩個新的left join
以顯示每個經理的受訓者:
select L.Name as lead
, R.Name as trainee
from Relation ML
join Lead L
on ML.SecondaryId = L.Id
left join
Relation LT
on LT.PrimaryId = L.Id
left join
Trainee T
on LT.SecondaryId = T.Id
where ML.PrimaryId = 'M101'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.