[英]How can I swap a value from one table to that of another table in the same select query?
My database consists of two tables 我的数据库由两个表组成
users table 用户表
+---------+----------+---------------+-----------+
| user_id | username | password_hash | user_type |
+---------+----------+---------------+-----------+
| 1 | Admin | hash | 0 |
| 2 | Student | hash | 1 |
| 3 | Teacher | hash | 2 |
+---------+----------+---------------+-----------+
tickets table 门票表
+-----------+---------+-----------------+----------------+--------------------+---------------+-------------+
| ticket_id | user_id | ticket_category | ticket_summary | ticket_description | ticket_status | assignee_id |
+-----------+---------+-----------------+----------------+--------------------+---------------+-------------+
| 1 | 2 | Network | Test Network | Lorem Ipsum | 1 | 1 |
| 2 | 3 | Printer | Test Printer | Lorem Ipsum | 1 | |
+-----------+---------+-----------------+----------------+--------------------+---------------+-------------+
Both tickets.user_id and tickets.assignee_id are foreign keys to users.user_id tickets.user_id和tickets.assignee_id都是users.user_id的外键
I am querying these tables using the following select statement 我正在使用以下选择语句查询这些表
SELECT username AS `Opened By`, assignee_id AS `Assigned To`, ticket_category AS `Category`, ticket_summary AS `Summary`, ticket_description AS `Description`, IF(ticket_status = 1,'Open','Closed') AS `Status` FROM tickets INNER JOIN users ON tickets.user_id = users.user_id;
This builds a dataset like so 这样会建立一个像这样的数据集
+-----------+-------------+----------+--------------+-------------+--------+
| Opened By | Assigned To | Category | Summary | Description | Status |
+-----------+-------------+----------+--------------+-------------+--------+
| Student | 1 | Network | Test Network | Lorem Ipsum | Open |
| Teacher | | Printer | Test Printer | Lorem Ipsum | Open |
+-----------+-------------+----------+--------------+-------------+--------+
In my select statement, I would like to replace the assignee_id number with the username that it corresponds too in the users table. 在我的select语句中,我想用用户表中也对应的用户名替换assignee_id号。
For example, the first ticket (ticket_id = 1) has an assignee_id of 1 and I would like the Assigned To column to display the username "Admin" instead of 1 例如,第一个票证(ticket_id = 1)的被分配人ID为1,我希望“分配给”列显示用户名“ Admin”而不是1
How could I modify my select query to accomplish this? 如何修改选择查询以完成此操作?
Just join to the users table again for the assignee_id
. 只需再次加入到users表以获取assignee_id
。 Note it needs to be a LEFT JOIN
in case no one has been assigned to the ticket. 请注意,如果没有人分配给LEFT JOIN
,它必须是LEFT JOIN
。 You may also want to use COALESCE
on u2.username
to output a blank instead of (null)
(ie COALESCE(u2.username, '')
). 您可能还想在u2.username
上使用COALESCE
输出空白而不是(null)
(即COALESCE(u2.username, '')
)。 SQLFiddle . SQLFiddle 。
SELECT u1.username AS `Opened By`,
u2.username AS `Assigned To`,
ticket_category AS `Category`,
ticket_summary AS `Summary`,
ticket_description AS `Description`,
IF(ticket_status = 1,'Open','Closed') AS `Status`
FROM tickets
JOIN users u1 ON tickets.user_id = u1.user_id
LEFT JOIN users u2 ON tickets.assignee_id = u2.user_id
Output for your sample data: 输出样本数据:
Opened By Assigned To Category Summary Description Status
Student Admin Network Test Network Lorem Ipsum Open
Teacher (null) Printer Test Printer Lorem Ipsum Open
select T.OpenedBy,u.username as AssignedTo,Category,Summary,Description,Status from
(
SELECT username AS `OpenedBy`, assignee_id , ticket_category AS `Category`, ticket_summary AS `Summary`, ticket_description AS `Description`, IF(ticket_status = 1,'Open','Closed') AS `Status` FROM tickets INNER JOIN users ON tickets.user_id = users.user_id
) as T
inner join
users u on T.assignee_id=u.user_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.