显示表格的基本信息

时间:2010-09-25 23:39:04

标签: sql

我有两张表,即

用户

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中执行此操作?

谢谢,

1 个答案:

答案 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
相关问题