现在要删除表中重复的记录。条件是:3个字段重复,同时比较第4个字段的值,保留值比较小的那一条
例如:
字段1 字段2 字段3 字段4
数值1 数值2 数值3 1
数值1 数值2 数值3 4
因为前3个字段都重复,第一条记录的字段4值为1,比第二条记录的字段4的值小,所以删除第二条,保留第一条。
请问这个sql怎么写。
------解决方案--------------------
- SQL code
delete tablename twhere exists(select 1 from tablename t2 where t.col1=t2.col1 and t.col2=t2.col2 and t.col3=t2.col3 and t.col4>t2.col4)
------解决方案--------------------
delete from ttab a
where a.c4 != (select min(b.c4)
from ttab b
where a.c1 = b.c1
and a.c2 = b.c2
and a.c3 = b.c3);
------解决方案--------------------
------解决方案--------------------
- SQL code
--用rowid就米有重复了delete from tab a where rowid > (select min(rowid) rowid from tab b where ...)