是这样的:
在数据库中有一个表:t(id,a,b,c,d,e,f,g),id是主键。
由于某些原因,现在要进行下列操作:
----------------------------------------------------------------
将数据库中像这样的数据删除到只剩下一个data1:
如: data1、data2、data3 ……它们的字段a,b相同,即:
data1.a = datas.a = data3.a =……
data1.b = datas.b = data3.b =……
要删除掉data2、data3 ……
--------------------------------------------------------------------
我现在做到了这里:
select a,b,count(*) as cc ,max(id) as maxid
from t
group a,b
having count(*) > 1
思维卡壳,现在应该这样进行下去呢?求大神指导,谢谢
我用的事Oracle数据库。谢谢
2 个解决方案
#1
假设ID唯一
delete from tt a1 where exists(select 1 from tt where a1.a=a and a1.b=b and a1.id<id)
delete from tt a1 where exists(select 1 from tt where a1.a=a and a1.b=b and a1.id<id)
#2
谢谢
#1
假设ID唯一
delete from tt a1 where exists(select 1 from tt where a1.a=a and a1.b=b and a1.id<id)
delete from tt a1 where exists(select 1 from tt where a1.a=a and a1.b=b and a1.id<id)
#2
谢谢