簡體   English   中英

將一個表的一列連接到另一表的多列

[英]joining one table one column to another table multiple column

我嘗試搜索許多示例,但是由於有很多專欄文章,我的問題要復雜一些。 這是一個簡化的版本:

使用者

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

類型

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

電腦

+------+--------+-------------------+------------------+--------------------+--------------------+
| 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                  |
+------+--------+-------------------+------------------+--------------------+--------------------+

我想要達到的結果是

+------+---------+----------------+---------------+-----------------+-----------------+
| 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             |
+------+---------+----------------+---------------+-----------------+-----------------+

我已經嘗試過使用多個ON,如下所示:

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 

但這沒用...停頓嗎?

您需要在users表上進行多個聯接:

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

對於computers表中的四個用戶列中的每一個,您都執行一個單獨的聯接到用戶表。 這些聯接表中的每一個都有不同的別名,因此在您的select語句中,您可以訪問聯接表的任何列值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM