【问题标题】:Matching data in two different tables (compare data)匹配两个不同表中的数据(比较数据)
【发布时间】:2017-05-02 14:11:30
【问题描述】:

我在匹配来自 2 个不同表的数据时遇到问题。 例子 表 1:a、b、c、d、e(col) 表 2: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(+)

对不起我的英语不好......

编辑:

抱歉,我不能用智能手机很好地打字。 可能是这样的。。

表1

col a--b--c--d--e
1st_row 'Ryan'--'Sofia'--'Bulgaria'--'January'--'107'
2nd_row 'Dony'--'Vienna'--'Austria'--'March'--'103'
3rd_row 'Ryan'--'Berlin'--'Germany'--'January'--'107'
4th_row 'Dony'--'Milan'--'Italy'--'March'--'103'

表2

col a--d--e--f--g
1st_row 'Ryan'--'January'--'107'--'Travel'--'5'
2nd_row 'Ryan'--'January'--'107'--'Bussiness'--'4'
3rd_row 'Dony'--'March'--'103'--'Bussiness'--'9'
4th_row '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 matched with table2 1st_row
table1 2nd_row matched with table2 3rd_row
table1 3rd_row matched with table2 1st_row (match duplicated)
table1 4th_row matched with table2 3rd_row (match duplicated)

但我想要的结果是 table1 1st_row 与 table2 1st_row 匹配 table1 2nd_row 与 table2 3rd_row 匹配 table1 3rd_row 与 table2 2nd_row 匹配 table1 4th_row 与 table2 4th_row 匹配

【问题讨论】:

  • 添加一些示例表数据和预期结果 - 以及格式化文本。
  • jarlh 请在我的回复帖子中查看我的示例数据和预期结果,谢谢

标签: sql oracle


【解决方案1】:


您可以使用以下查询

select distinct x.a, y.a, x.d, y.d, x.e, y.e 
from table1 x
inner join table2 y
on (x.a = y.a(+) and x.d = y.d(+) and x.e = y.e(+));

【讨论】:

  • thanx jim,明天我将在办公室尝试您的查询
【解决方案2】:


抱歉,我不能用智能手机很好地打字。
也许像这样..

table1
col a--b--c--d--e
1st_row 'Ryan'--'Sofia'--'保加利亚'--'January'--'107'
2nd_row '多尼'--'维也纳'--'奥地利'--'三月'--'103'
3rd_row
'Ryan'--'Berlin'--'Germany'--'January'--'107' 4th_row '多尼'--'米兰'--'意大利'--'三月'--'103'

table2
col a--d--e--f--g
1st_row 'Ryan'--'January'--'107'--'Travel'--'5'
2nd_row 'Ryan'--'一月'--'107'--'商务'--'4'
3rd_row 'Dony'--'March'--'103'--'商务'--'9'
4th_row 'Dony'--'March'--'103'--'商务'--'3'

在我的查询中
选择不同的 x.a、y.a、x.d、y.d、x.e、y.e
从 table1 x、table2 y
其中 x.a = y.a(+) 和 x.d = y.d(+) 和 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 匹配

【讨论】:

    猜你喜欢
    • 2017-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多