php - MySQL - 内连接 - 添加基于其他值的值的列

标签 php mysql pdo inner-join

我正在为 mysql 连接而苦苦挣扎:/

我在数据库 fe 中有多个表。 任务用户

任务 包含具有各种变量的任务,但最重要的是 - 签署任务的用户 ID(作为任务中的不同角色 - 作者、图形、校正者):

+---------+-------------+--------------+
| task_id | task_author | task_graphic |
+---------+-------------+--------------+
| 444     | 1           | 2            |
+---------+-------------+--------------+

用户

+---------+----------------+------------+-----------+
| user_id | user_nice_name | user_login | user_role |
+---------+----------------+------------+-----------+
| 1       | Nice Name #1   | login1     | 0         |
+---------+----------------+------------+-----------+
| 2       | Bad Name #2    | login2     | 1         |
+---------+----------------+------------+-----------+

使用 PDO 我得到了我想要的全部数据,同时使用 INNER JOIN 和来自不同表的数据(和 $_GET 变量)

SELECT tasks.*, types.types_name, warehouse.warehouse_id, warehouse.warehouse_code, warehouse.warehouse_description
FROM tasks
INNER JOIN types ON types.types_id = tasks.task_id
INNER JOIN warehouse ON warehouse.warehouse_id = tasks.task_id
WHERE tasks.task_id = '".$get_id."'
ORDER BY tasks.task_id

以上查询返回:

+---------+--------------+--------------+----------------+------------+-----------+------------------+------------------------+------------+-------------+-----------------+-----------+----------------+--------------------+---------------------+-----------+---------------------+------------------+---------------------+
| task_id | task_creator | task_graphic | task_purchaser | task_title | task_lang | task_description | task_description_files | task_files | task_status | task_prod_index | task_type | task_print_run | task_print_company | task_warehouse_code | task_cost | task_time_added     | task_deadline    | task_date_warehouse |
+---------+--------------+--------------+----------------+------------+-----------+------------------+------------------------+------------+-------------+-----------------+-----------+----------------+--------------------+---------------------+-----------+---------------------+------------------+---------------------+
| 2       | 1            | 2            | 1              | Test       | PL        | Lorem ipsum (?)  |                        |            | w           | 2222            | 3         | 456546         | Firma XYZ          | 2                   | 124       | 29.09.2016 15:48:20 | 01.10.2016 12:00 | 07.10.2016 14:00    |
+---------+--------------+--------------+----------------+------------+-----------+------------------+------------------------+------------+-------------+-----------------+-----------+----------------+--------------------+---------------------+-----------+---------------------+------------------+---------------------+

我想在 task_creator、task_author 和 task_graphic 之后添加 user_nice_name 的查询 - 显然是根据上述 3 个字段 fe 中提供的 ID 从表 users 中选择的好名字。

+---------+--------------+------------------------------------+--------------+--------------------------------------+
| task_id | task_creator | task_creator_nn                    | task_graphic | task_graphic                         |
+---------+--------------+------------------------------------+--------------+--------------------------------------+
| 2       | 1            | Nice Name (from task_creator ID=1) | 2            | Nice Name (from task_graphic ID = 2) |
+---------+--------------+------------------------------------+--------------+--------------------------------------+

我怎样才能做到这一点?

最佳答案

您需要三个连接:

SELECT t.*,
       uc.user_nice_name as creator_name,
       ug.user_nice_name as graphic_name,
       up.user_nice_name as purchaser_name,
       ty.types_name, w.warehouse_id, w.warehouse_code, w.warehouse_description
FROM tasks t INNER JOIN
     types ty
     ON ty.types_id = t.task_id INNER JOIN
     warehouse w
     ON w.warehouse_id = t.task_id LEFT JOIN
     users uc
     ON uc.user_id = t.task_creator LEFT JOIN
     users ug
     ON ug.user_id = t.task_graphic LEFT JOIN
     users up
     ON up.user_id = t.task_purchaser
WHERE t.task_id = '".$get_id."'
ORDER BY t.task_id;

注意事项:

  • 表别名使查询更易于编写和阅读。它们也是必需的,因为您在 FROM 子句中有三个对 users 的引用。
  • 这对 users 使用 LEFT JOIN 以防某些引用值丢失。
  • 你需要努力命名。 “仓库”ID 与“任务”ID 匹配是没有意义的。或者“任务”ID 与“类型”ID 匹配。但这就是您在问题中表达查询的方式。
  • ORDER BY 实际上什么都不做,因为所有行都有相同的 task_id

关于php - MySQL - 内连接 - 添加基于其他值的值的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40449303/

相关文章:

php - 创建复杂的 SQL 语句 - CakePHP

php - Laravel 5.2.x 在保存相关模型时更改模型中的 Updated_at 字段?

javascript - Node.js 和 mysql 回调 : query in query callback

java - 如何访问vps中的数据库

MySQL:获取最后插入的主键而不使用自动增量

postgresql - PHP PDO PGPOOL PGSQL - SQLSTATE [HY000] : General error: 7 no connection to the server

php - 如何在MYSQLI 中只显示重复的结果?

javascript - 添加 javascript 后 html 表单停止工作

PHP MySQL : issue selecting different columns from the same table

PHP PDO 不工作