【发布时间】:2017-09-30 19:40:29
【问题描述】:
我在匹配来自 2 个不同表的数据时遇到问题。
table1: a,b,c,d,e (col)
table2: a,d,e,f,g (col)
如何将table1 col a,d,e中的数据与table2 col a,d,e进行匹配
如果 table1 中的行与 table2 中的行匹配然后停止循环?
在我的脚本中,结果总是在匹配数据时重复(当 table1 中的数据匹配时,它的循环仍然没有与 table2 中的其他数据锁定)。
select distinct x.a, y.a, x.d, y.d, x.e, y.e
from table1 x,
table2 y
where x.a = y.a(+) and x.d = y.d(+) and x.e = y.e(+)
数据样本...
table1
col a--b--c--d--e
'Ryan'--'Sofia'--'Bulgaria'--'January'--'107'
'Dony'--'Vienna'--'Austria'--'March'--'103'
'Ryan'--'Berlin'--'Germany'--'January'--'107'
'Dony'--'Milan'--'Italy'--'March'--'103'
table2
col a--d--e--f--g
'Ryan'--'January'--'107'--'Travel'--'5'
'Ryan'--'January'--'107'--'Bussiness'--'4'
'Dony'--'March'--'103'--'Bussiness'--'9'
'Dony'--'March'--'103'--'Bussiness'--'3'
查询
select distinct x.a, y.a, x.d, y.d, x.e, y.e
from table1 x,
table2 y
where x.a = y.a(+) and x.d = y.d(+) and x.e = y.e(+)
结果是
table1 1st_row 与 table2 1st_row 匹配
table1 2nd_row 与 table2 3rd_row 匹配
table1 3rd_row 与 table2 1st_row 匹配(匹配重复)
table1 4th_row 与 table2 3rd_row 匹配(匹配重复)
但需要的结果是
table1 1st_row 与 table2 1st_row 匹配
table1 2nd_row 与 table2 3rd_row 匹配
table1 3rd_row 与 table2 2nd_row 匹配
table1 4th_row 与 table2 4th_row 匹配
我不明白如何使用程序或案例
请帮助解决这个问题...谢谢
【问题讨论】:
-
请修正您的格式,告诉我们您正在使用什么数据库,并使用
(+)摆脱那种陈旧的连接语法。 -
对不起蒂姆,我一周前刚刚学习 oracle 数据库,我不太了解你对古连接语法的意思.. 抱歉
-
哦,thanx kinchit dalwani
-
您当前的连接条件不区分
table2中的第 1,2 和 3,4 行。您必须添加逻辑才能以您想要的方式加入。