在第三个表中按值从两个不同的表中选择

时间:2014-03-10 14:01:15

标签: sql oracle select

I have next tables.
First one is A.
A have two columns: A_ID and  A_VALUE.
Second table is B. B too have two columns: B_ID and  B_VALUE
In additional I have table C. Table C have C_ID and bool columns C_BOOL
If C_BOOL value == true i need select value from A with given ID.
If C_BOOL value == false i need select value from B.
How I can write SELECT for this?
I use oracle db.

感谢您的建议。

3 个答案:

答案 0 :(得分:4)

SELECT CASE C.BOOL WHEN 1 THEN A.ID ELSE B.ID END
FROM A
JOIN B 
  ON B.ID = A.ID
JOIN C
  ON C.ID = A.ID

答案 1 :(得分:0)

尝试此查询:

SELECT C_ID,CASE WHEN C_BOOL = 1 THEN T3.A_VALUE ELSE T2.B_VALUE END 
FROM TABLE_C T1 LEFT OUTER JOIN TABLE_B T2 ON T1.C_ID = T2.B_ID
LEFT OUTER JOIN TABLE_A T3 T2 ON T1.C_ID = T3.A_ID

答案 2 :(得分:0)

select decode(C.BOOL,1,A.ID,B.ID) FROM C
JOIN A
ON A.ID=C.ID
JOIN B
ON B.ID=C.ID;

我认为T McKeown的答案是有效的,我认为这只是等效的(但更紧凑)。