首页 > 解决方案 > 选择排除冗余字段时进行多个左连接的简单方法

问题描述

Input 

t1
a  b  c 

t2 
a  d  e

t3 
a  f  g

预期多个左连接

a b c d e f g

select t1.*, t2.d,t2.e,t3.f,t3.g
from t1 left join (t2, t3) 
on (
t1.a=t2.a
and t1.a= t3.a )

如果要加入的字段超过 10 个,则不方便获取结果。

如果我按如下方式运行代码,它将返回冗余字段a

select * 
from t1 left join (t2, t3) 
on (
t1.a=t2.a
and t1.a= t3.a )


a b c a(1) d e a(2) f g

标签: mysql

解决方案


如果您使用 USING 子句而不是 ON,它不会返回冗余字段。

select * 
from t1 
left join t2 using (a)
left join t3 using (a);

这将返回:abcdefg


推荐阅读