在mysql上新建,使用内部联接获取错误的结果

时间:2017-03-08 20:57:03

标签: mysql database inner-join

我有一个包含4个表的数据库,我想加入,但是我得到了错误的结果,我不明白为什么

这是我运行的mysql查询

 SELECT co_id, u_name, c_name, e_status 
 FROM users 
 INNER JOIN company_owners ON u_id = co_userFk 
 INNER JOIN company ON co_companyFk = c_id 
 INNER JOIN employees ON u_id = userFk WHERE c_id = "1" 

这是我得到的结果

 +------------+-------------+-----------+------------------+
 | co_id      | u_name      | c_name    | e_status         |  
 +------------+-------------+-----------+------------------+
 | 1          | Simon       | SimonCorp | Chef             |                       
 | 1          | Simon       | SimonCorp | Chef             |                                                
 +------------+-------------+-----------+------------------+ 

这是我想要的结果

 +------------+-------------+-----------+------------------+
 | co_id      | u_name      | c_name    | e_status         |  
 +------------+-------------+-----------+------------------+
 | 1          | Simon       | SimonCorp | Chef             |                       
 +------------+-------------+-----------+------------------+

这是我的数据库结构

    Users
    +------------+-------------+----------+------------------+
    | u_id       | u_name      | u_address| u_email          |  
    +------------+-------------+----------+------------------+
    | 1          | Simon       | street 1 | Simon@gamil.com  |          
    | 2          | Andrew      | street 2 | Andrew@gmail.com |          
    | 3          | Tom         | street 3 | Tom@gmail.com    |          
    | 4          | Charlie     | street 4 | charlie@gmail.com|              
    +------------+-------------+----------+------------------+

   Company
   +------------+-------------+----------+--------------------------+
   | c_id       | c_name      | c_address| c_email                  |
   +------------+---------------+----------+------------------------+
   | 1          | SimonCorp     | street 1 | simoncorp@gamil.com    |
   | 2          | SimonotherCorp| street 2 |simonothercorp@gmail.com|
   +------------+---------------+----------+------------------------+



   Employees
   +------------+-------------+----------+----------------------+
   | e_id       | companyFk   | userFk   |  e_status            |  
   +------------+-------------+----------+----------------------+
   | 1          | 1           | 1        | Chef                 |          
   | 2          | 2           | 2        | employee             |
   | 3          | 2           | 1        | employee             |          
   | 4          | 1           | 3        | employee             |                       
   +------------+-------------+----------+----------------------+
   Simon is chef of first company and the employee in the second



    Company_Owners
    +------------+--------------+-------------+
    | co_id      | co_companyFk | co_userFk   | 
    +------------+--------------+-------------+
    | 1          | 1            | 1           |         
    | 2          | 2            | 1           |                       
    +------------+--------------+-------------+
    Simon owns both companies

2 个答案:

答案 0 :(得分:0)

如果您需要不同的(不是重复行),则必须添加distinct子句

 SELECT distinct co_id, u_name, c_name, e_status 
 FROM users 
 INNER JOIN company_owners ON u_id = co_userFk 
 INNER JOIN company ON co_companyFk = c_id 
 INNER JOIN employees ON u_id = userFk WHERE c_id = "1" 

答案 1 :(得分:0)

这是因为您的表员工仅考虑u_id = userFk作为加入条件。

表Employees有2条链接到该员工(Simon):

 Employees
   +------------+-------------+----------+----------------------+
   | e_id       | companyFk   | userFk   |  e_status            |  
   +------------+-------------+----------+----------------------+
   | 1          | 1           | 1        | Chef                 |          
   | 2          | 2           | 2        | employee             |
   | 3          | 2           | 1        | employee             |          
   | 4          | 1           | 3        | employee             |                       
   +------------+-------------+----------+----------------------+

因此,对于每一行,将考虑联合作为公司所有者投影结果。

要处理此问题,您可以限制联接以考虑companyFK

就像这样:

SELECT co_id, u_name, c_name, e_status 
 FROM users 
 INNER JOIN company_owners ON u_id = co_userFk 
 INNER JOIN company ON co_companyFk = c_id 
 INNER JOIN employees ON (u_id = userFk and c_id = companyFk) WHERE c_id = "1"