内部联接表基于列值

时间:2015-05-26 09:15:07

标签: sql sql-server

我有一个名为Payment的表,其中包含2列id和triptype。我的要求是,如果triptype为1,那么我应该用table1执行内连接,但如果它是2,那么我应该用table2执行连接。我尝试过使用UNION,但我无法解决此问题。我需要做什么?

3 个答案:

答案 0 :(得分:0)

要使def receive_heartbeet(hb) q.push(hb) nActive[hb.client]++ def find_inactive while q.front().time < currentTime - threshold hb = q.pop() nActive[hb.client]-- if nActive[hb.client] == 0 mark hb.client as inactive 正常工作,您必须确保所有集合

的列结构和类型相同
UNION

答案 1 :(得分:0)

这是解决方案:

SELECT A.ID, 
           CASE WHEN A.TRIPTYPE = 1 THEN B.[YOUR_COLUMN_TABLE_1]
                WHEN A.TRIPTYPE = 2 THEN C.[YOUR_COLUMN_TABLE_2]
           END
    FROM PAYMENT AS A LEFT OUTER JOIN TABLE_1 AS B ON A.ID = B.ID
                      LEFT OUTER JOIN TABLE_2 AS C ON A.ID = C.ID

答案 2 :(得分:-1)

这样做:

select t.id, 
       case t.triptype when 1 then t1.SomeColumn
                       when 2 then t2.SomeColumn end as SomeColumn
from table t
left join table1 t1 on t.id = t1.id
left join table2 t2 on t.id = t2.id