简体   繁体   中英

relate 2 columns of another table

I have the following 2 tables in Mysql:

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. But you should join users twice as you want names for both emp and 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM