使用不同的键加入不同的表

时间:2014-02-06 13:09:57

标签: sql oracle join

请帮我创建一个查询:我有以下4个表:

mast_comp

id --- code

1 --- aa

2 ---bb

3 --- cc

fin_calc

fin_id --- val

11 --- 2

12 --- 4

33 --- 6

fin_id --- code


11 --- aa

12 --- bb

33 --- cc

mark_calc

code --- calc


aa --- 10

bb --- 20

cc --- 30

帮助我获得所需的输出

期望的结果

id---fin_id---code---val---calc---calculated

1---  11---  aa---    2---  10---  12 
2---  12---  bb---    4---  20---  24
3---  33---  cc---    6---  30---  36

其中calculated = val + calc

抱歉,我没有开始查询分享,因为我尝试过的所有尝试都失败了......我想重新开始..谢谢。 (请忽略连字符,我只是用它来分隔条目)

1 个答案:

答案 0 :(得分:0)

尝试:

select mc.id id, f.fin_id fin_id, f.code code, fc.val val, mcc.calc, fc.val + mcc.calc calculated
from mast_comp mc join fin f on mc.code = f.code
join fin_calc fc on f.fin_id = fc.fin_id
join mark_calc mcc on mc.code = mcc.code

Here is a sqlfiddle demo