简体   繁体   中英

joining one table one column to another table multiple column

I have tried searching for many examples but my is a little more complicated as I have many columns. Here is a simplified version:

users

+--------+----------+
| userid | username |
+--------+----------+
| 1      | Tom      |
+--------+----------+
| 2      | Dick     |
+--------+----------+
| 3      | Harry    |
+--------+----------+

type

+--------+----------+
| typeid | typename |
+--------+----------+
| 1      | Desktop  |
+--------+----------+
| 2      | Laptop   |
+--------+----------+

computers

+------+--------+-------------------+------------------+--------------------+--------------------+
| pcid | typeid | bought_by_user_id | assigned_user_id | created_by_user_id | updated_by_user_id |
+------+--------+-------------------+------------------+--------------------+--------------------+
| 1    | 2      | 1                 | 3                | 1                  | 3                  |
+------+--------+-------------------+------------------+--------------------+--------------------+
| 2    | 1      | 2                 | 2                | 2                  | 2                  |
+------+--------+-------------------+------------------+--------------------+--------------------+
| 3    | 1      | 3                 | 2                | 3                  | 1                  |
+------+--------+-------------------+------------------+--------------------+--------------------+

the result I would like to achieve is

+------+---------+----------------+---------------+-----------------+-----------------+
| pcid | type    | bought_by_user | assigned_user | created_by_user | updated_by_user |
+------+---------+----------------+---------------+-----------------+-----------------+
| 1    | Desktop | Tom            | Harry         | Tom             | Harry           |
+------+---------+----------------+---------------+-----------------+-----------------+
| 2    | Laptop  | Dick           | Dick          | Dick            | Dick            |
+------+---------+----------------+---------------+-----------------+-----------------+
| 3    | Desktop | Harry          | Dick          | Harry           | Tom             |
+------+---------+----------------+---------------+-----------------+-----------------+

I've tried using multiple ONs like so:

SELECT * FROM computers
LEFT JOIN types ON computers.typeid = types.typeid
LEFT JOIN users
ON users.userid = computers.bought_by_user_id
ON users.userid = computers.assigned_user_id
ON users.userid = computers.created_by_user_id
ON users.userid = computers.updated_by_user_id 

but that didn't work... Halp?

You need multiple join on users table:

SELECT 
    computers.pcid,
    types.typename,
    bought.username as bought_by_user,
    assigned.username as assigned_by_user,
    created.username as created_by_user,
    updated.username as updated_by_user
FROM computers
LEFT JOIN types ON computers.typeid = types.typeid
LEFT JOIN users bought ON computers.bought_by_user_id = bought.userid
LEFT JOIN users assigned ON computers.assigned_by_user_id = assigned.userid
LEFT JOIN users created ON computers.created_by_user_id = created.userid
LEFT JOIN users updated ON computers.updated_by_user_id = updated.userid

For each of your four user columns in computers table you do a separate join to users table. Each of those joined tables has a different alias, so in your select statement you can access any column value of the joined table.

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