碰到这么个问题,
假定表 tb 字段a,b,c,d 其中主键(a,b,c)
因其中有些脏数据要处理,如字段b有 数据‘ 45’,‘ 45’这种情况要改成‘45’
但是直接update的话会报主键冲突,说明已经有a,'45',c主键的存在了。怎么样能完成update 当a,trim(b),c有相同主键时进行下一条操作,当没有相同主键时,update
假定表 tb 字段a,b,c,d 其中主键(a,b,c)
因其中有些脏数据要处理,如字段b有 数据‘ 45’,‘ 45’这种情况要改成‘45’
但是直接update的话会报主键冲突,说明已经有a,'45',c主键的存在了。怎么样能完成update 当a,trim(b),c有相同主键时进行下一条操作,当没有相同主键时,update
set b=ltrim(b)
where not exists(select 1 from tb t2 t2.a=t1.a and t2.c=t1.c and t2.b=ltrim(t1.b))试试吧,没测试
update tb a set a.b=trim(a.b) where not exists (select 1 from tb b
where a.a=b.a and a.b<>b.b and a.c=b.c and b.b=trim(a.b))
update tb a set a.b=ltrim(a.b)
where not exists (select 1 from tb b
where a.a=b.a and a.b<>b.b and a.c=b.c and b.b=trim(a.b))