SQL 一行有来自不同列的多个连接

SQL One row that has multiple joins from different columns

Mapping Table 

Desciption  ToID FromID 
Map A       2    1 


BaseTable

Id, Deccription, subTypeID
1   ValueA       9
2   ValueB       10`enter code here`

SubTypeTable
id  Description
9   SubType 9
10  Subtype 10 

我要return的是下面的Mappingtable

MapDescription,ToID 的 SubTpeDescription,FromID 的 SubTpeDescription

所以基本上 MapA、Subtype9、Subtype 10 作为输出

到目前为止我所做的是

Select m.Description, st.Description from Mapping m 
right join BaseTable bt where m.toID = bt.id
right join BaseTable bt where m.FromID = bt.id,
inner join SubTypeTable stt on bt.subTypeID = stt.id

第二次引用时,您需要给 table 一个不同的别名。

Select m.Description, st.Description 
FROM BaseTable bt 
LEFT JOIN Mapping m where m.toID = bt.id
LEFT JOIN BaseTable bt2 where m.FromID = bt2.id,
inner join SubTypeTable stt on bt2.subTypeID = stt.id

我个人会从主要 table(基础 table)开始,然后朝着您需要的方向努力。我尽量避免右连接——通常我使用它们是因为计划不周。