简体   繁体   中英

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

This is the mysql query i run

 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

 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.

The table Employees has 2 lines linked to this employee (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.

To handle this, you could restrict the join to consider companyFK too.

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" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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