duanganleng0577 2015-10-22 04:09
浏览 15
已采纳

将一个表一列连接到另一个表多列

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?

  • 写回答

1条回答 默认 最新

  • douling8772 2015-10-22 04:12
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备