jdbc数据比较

时间:2011-03-23 03:49:13

标签: db2

我想比较两行中15列的内容。 我正在使用db2 9和jdbc。 我可以使用sql来获得“匹配或不匹配”等结果吗


我怎样才能让列不同?

1 个答案:

答案 0 :(得分:1)

您可以使用EXCEPT运算符执行此操作。

在下面的示例中,我使用公共表表达式来获取单行(在这种情况下,假设id是主键。

with r1 as (select c1, c2, ..., c15 from t where id = 1),
     r2 as (select c1, c2, ..., c15 from t where id = 2)
select * from r1
except 
select * from r2

如果返回0行,则行相同。如果它返回一行,则两行不同。

如果你真的希望结果是'MATCH'或'NOT MATCH':

with r1 as (select c1, c2, ..., c15 from t where id = 1),
     r2 as (select c1, c2, ..., c15 from t where id = 2),
     rs as (select * from r1 except select * from r2)
select
   case when count(*) = 0 then 'MATCH' 
        else 'NOT MATCH' 
        end as comparison 
from
   rs;