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
left join occupations o on uf.mother_occupation = o.occupation_id
where user_id = 1;
4

1 回答 1

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;
于 2013-11-13T07:00:13.720 回答