left join多表

ouyang4683 2010-10-21 11:04:58
select * from
table1 left join table2 on 条件1
left join table3 on 条件2
left join table4 on 条件3
where 条件4

理论上是这样的,但得出来的结果有冗余,

我是想 table1 是最大集合

table2 table3 table4 之间没有关系

但它们都和table1 的ID 有关系 要怎么写呢

嗯 。。。。table2 table3 table4 都是用with as 写出来的。。。
...全文
518 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
ouyang4683 2010-10-21
  • 打赏
  • 举报
回复
。。。确定 table1有多少条数据是知道的

再一个, 明显的结果中 第一列有空值
Diza1986 2010-10-21
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 ouyang4683 的回复:]
条件4 我只写了关于table1 的 因为它是最大集
结果就变成

select * from
table1 left join table2 on table1.id = table2.id

left join table3 就变成 table1和table2的并集再和table3 并
[/Quote]

left join结果怎么会是并集,你看清楚了么
ouyang4683 2010-10-21
  • 打赏
  • 举报
回复
条件4 我只写了关于table1 的 因为它是最大集
结果就变成

select * from
table1 left join table2 on table1.id = table2.id

left join table3 就变成 table1和table2的并集再和table3 并
dawugui 2010-10-21
  • 打赏
  • 举报
回复
最好给出完整的表结构,测试数据,计算方法和正确结果.


发帖注意事项
http://topic.csdn.net/u/20091130/21/fb718680-98ff-4afb-98d8-cff2f8293ed5.html?24281

Diza1986 2010-10-21
  • 打赏
  • 举报
回复
table2 table3 table4 都是用with as 写出来的
table2 table3 table4的id有重复吧,看看是不是有欠妥的地方
delphisanding1 2010-10-21
  • 打赏
  • 举报
回复
路过学习
要多去掉冗余,条件不够吧
ngx20080110 2010-10-21
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ouyang4683 的回复:]
select * from
table1 left join table2 on table1.id = table2.id
left join table3 on table1.id = table3.id
left join table4 on table1.id = table4.id
where 条件4

试过了,冗余
[/Quote]
除了這些條件,應該還有其他條件去掉冗餘
ouyang4683 2010-10-21
  • 打赏
  • 举报
回复
select * from
table1 left join table2 on table1.id = table2.id
left join table3 on table1.id = table3.id
left join table4 on table1.id = table4.id
where 条件4

试过了,冗余
dawugui 2010-10-21
  • 打赏
  • 举报
回复
select * from
table1 left join table2 on table1.id = table2.id
left join table3 on table1.id = table3.id
left join table4 on table1.id = table4.id
where 条件4

gelyon 2010-10-21
  • 打赏
  • 举报
回复
发帖注意你规范
ouyang4683 2010-10-21
  • 打赏
  • 举报
回复
发了个问题
with table2 as(...)
select table2.*,table1.* from
table1 left join table2 on table1.id = table2.id
where 条件4

居然也是个迪卡尔。。。
left join 不是以左边做基础么?
换成inner join 结果显示了table2 的条数, 也没分清是inner join 默认是rignt ?还是并集?

高手们给解释一下呗^_^



Sophiajupiter 2010-10-21
  • 打赏
  • 举报
回复
select * from
table1 left join table2 on table1.id = table2.id
union
select * from
table1 left join table3 on table1.id = table3.id
union
select * from
table1 left join table4 on table1.id = table4.id
hailang1118 2010-10-21
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 dawugui 的回复:]

SQL code
最好给出完整的表结构,测试数据,计算方法和正确结果.


发帖注意事项
http://topic.csdn.net/u/20091130/21/fb718680-98ff-4afb-98d8-cff2f8293ed5.html?24281
[/Quote]
重复一下。简单的数据也可以的,不然怎么给你答?答了也有可能不正确,你还不如不要这种答案呢。

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧