简体   繁体   English

如何在同一选择查询中将一个表中的值交换到另一表中的值?

[英]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.

相关问题 如何更新表,但需要从同一表的另一个选择和另一个表中获取更新值? - how can I update a table but needing to get the update value from another select of the same table and other one? 从一个表中选择,并从同一查询中的另一个表计数 - Select from one table, and count from another table in same query 如何基于另一个表中相同值的行为从一个表中选择值? - How to SELECT value from one table based on same value's behavior in another table? 如何在一个 mysql 查询中从一个表中获取值以及从另一个连接表中获取值数组? - How can I get value from one table and array of values from another join table in one mysql query? mysql从同一表中选择一个值作为另一个值 - mysql select one value as another value from same table 如何显示一个表中下拉框的第一个值和另一个表中相同下拉列表的其他值 - How can I display first value of drop down box from one table and other values of same drop down from another table MySQL我可以从一个表还是另一个表中选择数据? - MySQL Can I Select Data from One table OR another Table? Select 具有一个值但没有来自同一个表的另一个值的行 - Select rows that has one value but not another from same table 从一个表中选择随机值并插入到同一数据库中的另一个?? - select randum value from one table and inserted to another in the same database?? 如何根据一个值从表一中获取 select,然后从同一表中获取 select 以及该表中的值? - How to select all from table one based on a value, then select from same table with value from that table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM