Mysql连接表A的多个列与另一个表B.

时间:2013-11-13 06:55:48

标签: mysql sql database

我有一个相同的解决方案,加入同一个表两次,但我想知道下面的查询是否可以优化?

select oc.name as father_occupation, o.name as mother_occupation 
from user_family uf 
left join occupations oc on uf.father_occupation = oc.occupation_id
left join occupations o on uf.mother_occupation = o.occupation_id
where user_id = 1;

1 个答案:

答案 0 :(得分:0)

您可以在一个左连接中简单地检查两个条件

   select oc.name as father_occupation, o.name as mother_occupation 
    from user_family uf 
    left join occupations oc on uf.father_occupation = oc.occupation_id and uf.mother_occupation = oc.occupation_id where user_id = 1;