mysql - 选择排除冗余字段时进行多个左连接的简单方法
问题描述
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
解决方案
如果您使用 USING 子句而不是 ON,它不会返回冗余字段。
select *
from t1
left join t2 using (a)
left join t3 using (a);
这将返回:abcdefg
推荐阅读
- scala - 如何验证一个方法是否被一个对象调用,该对象的某些字段可能是 anyObject() 而其他字段具有特定值?
- jpa - 使用 CriteriaQuery 的 FROM 子句中的子查询
- facebook - Facebook API me/accounts 并非返回所有页面
- java - Java 在线程终止后检查特定条件
- php - 从非本地网络访问服务器数据,无需端口转发
- http - 如何构造一个不需要正文的 POST 请求
- android - 从本机方法返回 HashMap
- asp.net-core - ASP.NET Core 数据访问层自定义类 AddSingleton
- python - Django - login_required 不更新 URL
- java - 连接 wifi 时获取 Internet 状态更改