[英]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: 我已经尝试过使用多个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
but that didn't work... Halp? 但这没用...停顿吗?
You need multiple join on users
table: 您需要在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
For each of your four user columns in computers
table you do a separate join to users table. 对于computers
表中的四个用户列中的每一个,您都执行一个单独的联接到用户表。 Each of those joined tables has a different alias, so in your select statement you can access any column value of the joined table. 这些联接表中的每一个都有不同的别名,因此在您的select语句中,您可以访问联接表的任何列值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.