【发布时间】:2018-03-14 19:10:48
【问题描述】:
我有桌子:
CREATE TABLE MyTable (
RootId int,
Direction bit,
....
);
现在,我必须从这个表中编写 select 并将一些表连接到它。连接表取决于 Direction 参数。如何像这里一样加入 MyTable3:
select
Root,
Direction,
Type
from MyTable
join MyTable1 on
MyTable1.Id = RootId
join MyTable2 on
MyTable2.Id = RootId
join MyTable3 on
...
case select when Direction = 1
MyTable3.TypeId = MyTable1.TypeId
else
MyTable3.TypeId = MyTable2.TypeId
【问题讨论】:
-
编辑您的问题并包含完全限定的列名,以便清楚列的来源。
-
on (Direction = 1 and myTable3.TypeId = myTable1.TypeId) or (Direction != 1 and myTable3.TypeId = myTable2.TypeId)