我有这样的表:
表格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
Run Code Online (Sandbox Code Playgroud)
表2:
id | username
1 | admin
2 | jack
3 | danny
4 | dummy
5 | john
6 | peter
Run Code Online (Sandbox Code Playgroud)
我如何为table1的entered_by加入这两个表,并且在table2 上将modify_by替换为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
Run Code Online (Sandbox Code Playgroud)
小提琴这里.
简而言之,您需要为每个字段添加连接.这就是为什么有双重加入的原因table2.
| 归档时间: |
|
| 查看次数: |
1328 次 |
| 最近记录: |