简体   繁体   English

关联另一个表的 2 列

[英]relate 2 columns of another table

I have the following 2 tables in Mysql:我在 Mysql 中有以下 2 个表:

table name:store (store user id's)
emp dst
1    2
1    3
2    1
3    1
4    2

table name:users
id   name
1    empA
2    empB
3    empC
4    empD

How should my query be to obtain the following result based on the store table?我的查询应该如何根据商店表获得以下结果?

result
emp  dst
empA empB
empA empC
empB empA
empC empA
empD empB

Just join store and users together.只需将storeusers连接在一起。 But you should join users twice as you want names for both emp and dst.但是您应该两次加入users ,因为您需要 emp 和 dst 的名称。

So the query might be:所以查询可能是:

SELECT u1.name AS emp, u2.name AS dst 
FROM store AS s 
JOIN users AS u1 ON s.emp = u1.id 
JOIN users AS u2 ON s.dst = u2.id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 关联同一个表中的两列 - Relate two columns in the same table 如何在具有两列与另一个名称表相关的外键的表上执行mysql select查询 - How to do a mysql select query on a table with two columns of foreign keys that relate to another table of names 将 pivot 表关联到 laravel 中的另一个表 - relate pivot table to another table in laravel 如何将一张桌子与另一张桌子联系起来以备将来记录 - how relate one table to another for future records 从表中获取所有数据,有时将该表与另一个表相关联 - Get all the data from a table and sometimes relate that table with another 将两个字段(彼此依赖)相互关联到另一个表中的相同字段 - Relate two fields (on which is reliant on the other) to the same field in another table 如何在MySQL中将一个表与另一个表关联(最好使用PHPMyAdmin) - How to relate one table to another in MySQL (with PHPMyAdmin, preferably) 串联mysql中的值,其中一个表中的多行与另一表中的一行相关 - Concatenating values in mysql where multiple rows in one table relate to one row in another table 当一个表中的多行与另一表中的一行相关时,在MySQL中联接两个表 - JOINing two tables in MySQL when multiple rows in one table relate to one row in another 试图将两个表关联在一起 - trying to relate two table together
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM