找到三个表的输出

时间:2017-05-30 08:22:46

标签: sql oracle join

我有三张桌子。 1. Emp,2。薪水,3。Commison。 EMP表有四列,即

ID        Name      Gender  department.
101     Tarun        M        10
102     Sahil        M        10
103     Manju        F        10
104     Dipika       F        10
105     Parvinder    M        20
106     Akhilesh     M        20
107     Shanti       F        20
108     Shivani      F        30
109     Bhuvan       M        30
110     babita       F        30

薪资表有两列,即

ID       Salary . 
101      3000
102      5420
103      8954 
104      10000 
105      8574
106      9965
107      9000
108      7500
109      9658 
110      9800

Commison Table有两列,即

ID    Commission.
101      2500
104      2000
107      3000 
109      5000 

输出

ID       Name         Salary+Commision

101     Tarun              5500     
102     Sahil              5420
103     Manju              8954    
104     Dipika             12000   
105     Parvinder          8574     
106     Akhilesh           9965     
107     Shanti             12000    
108     Shivani            7500    
109     Bhuvan             14658 
110     babita             9800

4 个答案:

答案 0 :(得分:1)

您的查询将如下所示:

select e.id, e.name, s.salary + c.Commission as total
from salary s,
     Commission c,
     emp e
where e.id = s.id
  and c.id = s.id

答案 1 :(得分:0)

假设我已正确理解您的问题,您只需执行以下操作:

SELECT 
E.ID
,E.Name
,S.Salary + C.Commission
FROM Emp E
LEFT JOIN Salary S on E.ID = S.ID
LEFT JOIN Commission C on E.ID = C.ID

答案 2 :(得分:0)

SELECT ID, Name, s.Salary + c.Commission AS Total FROM EMP e 
INNER JOIN Salary s ON e.ID = s.ID
INNER JOIN Commison c ON s.ID = c.ID

答案 3 :(得分:0)

考虑到每个员工都有自己的工资表,

SELECT 
E.ID as ID
,E.Name as Name
,nvl(S.Salary,0) + nvl(C.Commission,0) as "Salary+Commision"
FROM Emp E INNER JOIN Salary S on E.ID = S.ID
LEFT JOIN Commission C on E.ID = C.ID

如果您认为他们可能是某个员工,他的记录未在薪资表中加载,但您希望他们在您的结果中。

SELECT 
E.ID as ID,E.Name as Name,nvl(S.Salary,0) + nvl(C.Commission,0) as "Salary+Commision"
FROM Emp E LEFT JOIN Salary S on E.ID = S.ID
LEFT JOIN Commission C on E.ID = C.ID

如果你不想写“JOIN” -

SELECT 
E.ID as ID
,E.Name as Name
,nvl(S.Salary,0) + nvl(C.Commission,0) as "Salary+Commision"
FROM Emp E ,Salary S,Commission C
WHERE E.ID = S.ID(+)
AND E.ID = C.ID(+)

你可以做任何事情,但是处理来自 Commision 表的NULL值,因为每个员工都不会得到佣金。