我有这样的表:
表 1:
id | item_name | entered_by | modify_by
1 | banana | 2 | 1
2 | apple | 4 | 3
3 | orance | 1 | 1
4 | pineapple | 5 | 3
5 | grape | 6 | 1
表 2:
id | username
1 | admin
2 | jack
3 | danny
4 | dummy
5 | john
6 | peter
我如何将表 1 的 entered_by 和 modify_by 加入表 2 上对应的用户名和 ID 替换。
谢谢
最佳答案
试试这个:
SELECT t1.id, t1.item_name,
t2enteredBy.username enteredBy,
t2modifyBy.username modifyBy
FROM table1 t1
JOIN table2 t2enteredBy ON t1.entered_by = t2enteredBy.id
JOIN table2 t2modifyBy ON t1.modify_by = t2modifyBy.id
fiddle here .
简而言之,每个字段都需要一个连接。这就是 table2
上存在双重连接的原因。
关于mysql - SQL:加入2个表并将id更改为用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19700337/