从表的复杂情况中检索数据

时间:2012-03-17 10:34:24

标签: mysql join

我有两张桌子帐户& accounts_csm具有以下结构

accounts

id   name 
1    Dr.Roy
2    Noble Hospital
3    Dr.Ran     

在账户表中,存储了医生和医院的条目。

accounts_csm
account_id_c    doctor_hospital_id_c
1                     2
3                     2

现在我想检索doctor_name并尊重hospital_name意味着

结果应为

Doctor_name    Hospital_name 
Dr.Roy         Noble Hospital
Dr.Ran         Noble Hospital     

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

尝试以下

SELECT acct1.name AS doc_name, 
       acct2.name AS hospital_name
FROM accounts_csm AS acsm
LEFT JOIN accounts AS acct1 
   on acsm.account_id_c = acct1.id
LEFT JOIN accounts AS acct2 
   on acsm.doctor_hospital_id_c = acct2.id