我有两张表,即
uid | firstname
1 | John
2 | Bob
3 | Paul
4 | Peter
cid | assigned_to | caller_id
1 | 2 | 1
2 | 1 | 3
3 | 2 | 4
4 | 4 | 2
assigned_to和caller_id只是用户的uid。
我只想显示每次通话的结果:
call_id | username(assigned_to) | username(caller_id)
如何在SQL中执行此操作?
谢谢,
答案 0 :(得分:3)
试试这个:
select
cid as call_id,
A.username, -- assingned to
B.username -- caller id
from calls
left join users A on calls.assigned_to = A.uid
left join users B on calls.caller_id = B.uid