基于父表中的列的SQL查询 - 父子关系

时间:2017-10-23 16:46:04

标签: sql oracle

我有以下三个表(帐户,客户,员工),我想根据列AGENT_CODE&列加入他们。 AGENT_TYPE并实现以下目标。

当CUSTOMER& A中的AGENT_CODE相同时,加入这些表的最佳方式是什么?员工表?

我有这个查询给我错误的结果

 SELECT ac.AGENT_CODE,
 ac.WORKING_AREA,
 ac.AGENT_TYPE,
 CONCAT(c.FIRST_NAME,c.LASTNAME_NAME),
 e.EMP_NAME
FROM ACCOUNTS ac,
CUSTOMER c,
EMPLOYEE e 
WHERE ac.AGENT_CODE = e.AGENT_CODE 
OR ac.AGENT_CODE = c.AGENT_CODE

GETTING_WRONG_RESULTS_WITH_THE_ABOVE_QUERY

+------------+--------------------+------------+--------------+--------------+
| AGENT_CODE | WORKING_AREA       | AGENT_TYPE | CUSTOMER_NAME| EMP_NAME     |
+------------+--------------------+------------+--------------+--------------+
| A007       | Bangalore          |   CUSTOMER |Walter Holmes |Walter Holmes | 
| A007       | London             |   EMPLOYEE |Walter Holmes |Peter Sam     |
| A008       | New York           |   CUSTOMER |Micheal Junior|Micheal Junior| 
| A007       | Bangalore          |   EMPLOYEE |Walter Holmes |John Tyler    | 
| A010       | Chennai            |   CUSTOMER |Micheal       |Micheal       | 
| A007       | San Jose           |   EMPLOYEE |Walter Holmes |Albert        | 
+------------+--------------------+------------+--------------+--------------+ 

期待结果

+------------+--------------------+------------+--------------+
| AGENT_CODE | WORKING_AREA       | AGENT_TYPE | AGENT_NAME   |
+------------+--------------------+------------+--------------+
| A007       | Bangalore          |   CUSTOMER |Walter Holmes | 
| A003       | London             |   EMPLOYEE |Peter Sam     |
| A008       | New York           |   CUSTOMER |Micheal Junior| 
| A011       | Bangalore          |   EMPLOYEE |John Tyler    | 
| A010       | Chennai            |   CUSTOMER |Micheal       | 
| A012       | San Jose           |   EMPLOYEE |Albert        | 
+------------+--------------------+------------+--------------+   

ACCOUNTS(AGENT_CODE -PrimaryKey)

+------------+--------------------+------------+
| AGENT_CODE | WORKING_AREA       | AGENT_TYPE |
+------------+--------------------+------------+
| A007       | Bangalore          |   CUSTOMER |
| A003       | London             |   EMPLOYEE |
| A008       | New York           |   CUSTOMER |
| A011       | Bangalore          |   EMPLOYEE |
| A010       | Chennai            |   CUSTOMER |
| A012       | San Jose           |   EMPLOYEE |
| A005       | Brisban            |   EMPLOYEE |
+------------+--------------------+------------+

CUSTOMER(AGENT_CODE -ForeignKey)

+-----------+-------------+-------------+------------+  
|CUST_CODE  | FIRST_NAME   | LAST_NAME  | AGENT_CODE |
+-----------+-------------+-------------+------------+
| C00013    | Walter      | Holmes      | A007      |
| C00001    | Micheal     | Junior      | A008       |
| C00020    | Albert      | Skyler      | A010       |
+-----------+-------------+-------------+------------+

员工(AGENT_CODE -ForeignKey)

EMP_NAME    EMP_CODE       AGENT_CODE 
----------  --------------- ----------
Peter Sam    C00054          A003
John Tyler   C00023          A011
White Bolt   C00043          A012

1 个答案:

答案 0 :(得分:1)

如果您想要合并结果,可能需要UNION您的结果。

SELECT a.AGENT_CODE, a.WORKING_AREA, a.AGENT_TYPE, c.FIRST_NAME || ' ' || c.LAST_NAME AS AGENT_NAME
FROM ACCOUNTS a
JOIN CUSTOMER c ON c.AGENT_CODE = a.AGENT_CODE
UNION
SELECT a.AGENT_CODE, a.WORKING_AREA, a.AGENT_TYPE, e.EMP_NAME
FROM ACCOUNTS a
JOIN CUSTOMER e ON e.AGENT_CODE = a.AGENT_CODE