MySQL条件连接

时间:2018-01-09 18:12:43

标签: mysql left-join

我想有条件地加入mysql中的多个列。基本上,我想看看,如果表格A与该列的表格B不匹配,我想尝试让它与下一列匹配。我的第一次检查是:

Join A.Acct = B.Acct.  

如果表B没有与A匹配的记录,则在该过程中无法加入

Left Join A.Phone=B.Phone OR LEFT Join A.Phone=B.Phone2 OR 

LEFT JOIN     A.Phone = B.PHONE3

1 个答案:

答案 0 :(得分:0)

只需使用多个left join,然后在select

中选择正确的值
select a.*,
       coalesce(b.?, b1.?, b2.?, b3.?) as whatever
from a left join
     b
     on a.acct = b.acct left join
     b b1
     on a.phone = b.phone left join
     b b2
     on a.phone = b.phone2 left join
     b b3
     on a.phone = b.phone3;

?是您要从b中选择的列。每个表单都需要一个单独的coalesce()表达式。