php - 将一个表的一列连接到另一个表的多列

标签 php mysql database

我已经尝试搜索许多示例,但我的示例有点复杂,因为我有很多列。这是一个简化版本:

用户

+--------+----------+
| 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 表中的四个用户列中的每一个,您都单独连接到用户表。每个连接表都有不同的别名,因此在您的选择语句中,您可以访问连接表的任何列值。

关于php - 将一个表的一列连接到另一个表的多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33273276/

相关文章:

mysql - vb.net 在两个日期之间选择

mysql - Left Join with Where 子句返回空结果

mysql - 了解为什么外键引用同一张表中的主键?

mysql - MySQL 从什么时候开始像处理 TEXT 列一样处理 VARCHAR 列?

PHP 表单/准备好的语句错误

php - 在 PHPSpreadsheet 中垂直和水平居中文本

c# - 使用 openssl_seal 加密的 php 数据。如何在 C# 中解码?

mysql - 如何从 sql 中的事件日志中找到两次平均差异?

php - 并发用户可以通过 Apache + PHP 解决方案支持

sql - 计算 SQL 语句中返回的行数