简体   繁体   English

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

[英]New at mysql, Gets wrong result using inner joins

I have a database with 4 tables that i want to join but I'm getting the wrong result and i don't understand why 我有一个包含4个表的数据库,我想加入但是我得到了错误的结果,我不明白为什么

This is the mysql query i run 这是我运行的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" 

This is the result i get 这是我得到的结果

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

This is the result i want 这是我想要的结果

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

This is my database structure 这是我的数据库结构

    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

If you need distinct (not repetead rows) you must add the distinct clause 如果需要不同(不是重复行),则必须添加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" 

It's because your table Employees is considering only u_id = userFk as join condition. 这是因为您的表Employees只考虑u_id = userFk作为连接条件。

The table Employees has 2 lines linked to this employee (Simon): 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             |                       
   +------------+-------------+----------+----------------------+

So, for each line, will be project a result with Simon as the Company Owner, considering that joint. 因此,对于每一行,将考虑联合,将Simon作为公司所有者投影结果。

To handle this, you could restrict the join to consider companyFK too. 要处理此问题,您可以限制companyFK以考虑companyFK

It would be like this: 它会是这样的:

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" 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM