当两列中的值不匹配时,连接两个表

时间:2013-03-19 00:15:02

标签: sql oracle

表1:

onode_c, dnode_c, dist1

表2:

onode_c, dnode_c, dist2

我需要一个返回

的查询
onode_c, dnode_c, dist1, dist2

表示表1和表2中dist1dist2不匹配的记录

select  a.onode_c, a.dnode_c, trunc(a.dist1), trunc(b.dist2)
from table1 a, table2 b
where a.onode_c = b.onode_c and a.dnode_c = b.dnode_c and trunc(a.dist1) != trunc(b.dist2);

上述查询多次返回相同的记录。

3 个答案:

答案 0 :(得分:0)

请尝试以下声明:

select a.onode_c, a.dnode_c, trunc(a.dist), trunc(b.dist2) from table1 a
left join table2 b on a.onode_c = b.onode_c and a.dnode_c = b.dnode_c
where trunc(a.dist1) != trunc(b.dist2);

答案 1 :(得分:0)

尝试使用SELECT DISTINCT或许

答案 2 :(得分:0)

试试这个:

select  DISTINCT a.onode_c, a.dnode_c, trunc(a.dist1), trunc(b.dist2)
from table1 a, table2 b
where a.onode_c = b.onode_c and a.dnode_c = b.dnode_c and trunc(a.dist1) != trunc(b.dist2);
相关问题