【问题标题】:MySQL - inner join - add column with value based on other valueMySQL - 内部连接 ​​- 添加具有基于其他值的值的列
【发布时间】:2017-03-19 20:37:33
【问题描述】:

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

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

tasks 包含具有各种变量的任务,但最重要的是 - 签署任务的用户 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) |
+---------+--------------+------------------------------------+--------------+--------------------------------------+

我怎样才能做到这一点?

【问题讨论】:

  • 为包含 task_graphic name 的表添加连接
  • INNER JOIN users ON users.user_nice_name = tasks.task_graphic?

标签: php mysql pdo inner-join


【解决方案1】:

你需要三个连接:

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 的引用。
  • 这会将LEFT JOIN 用于users,以防某些参考值丢失。
  • 您需要进行命名。 “仓库” id 与“任务” id 匹配是没有意义的。或者“任务”ID 与“类型”ID 匹配。但这就是您在问题中表达查询的方式。
  • ORDER BY 实际上什么都不做,因为所有行都有相同的task_id

【讨论】:

  • 谢谢!像魅力一样工作:)
【解决方案2】:

假设task_graphic_name在一个表名task_graphic_table里面,关系字段是task_graphic_id

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

如果您需要列按特定顺序出现,您应该按顺序显式调用列名,例如:

SELECT tasks.col1
      , task.col2
      , types.types_name
      , warehouse.warehouse_id
      , warehouse.warehouse_code
      , task.col2
      , warehouse.warehouse_description
      , task_graphic_table.task_graphic_name

【讨论】:

    【解决方案3】:

    在您的查询中添加两个子查询。喜欢

     SELECT tasks.*,
    ....
    ....,
    (select user_nice_name from users where id = tasks.task_author) AS task_creator_name,
    (select user_nice_name from users where id = tasks.task_graphic) AS task_graphic_name
    FROM tasks
    INNER JOIN types ON types.types_id = tasks.task_id
    ....
    ....
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多