如果记录重复,我想删除并留下一条记录。在我的表中,t_id是一个主键,它是int类型和自动增量,id是序列号并且是varchar类型。 起初,我的sql是这样的,但它似乎僵局,并且运行很长时间,似乎永远不会停止。我的两个sql在Oralce中发现重复记录有什么区别?
delete from tbtest
Where t_id Not In (
select max(t_id) from tbtest having count(id)>1
group by id
)
and id in (
select id from tbtest group by id having count(id)>1
)
and create_time<to_timestamp('2016-02-25 11:26:52','yyyy-mm-dd hh24:mi:ss')
所以我改变它到另一种方式,它运行得非常快?它们之间有什么区别?
delete from tbtest where t_id in (
select t_id from tbtest
Where t_id Not In (
select max(t_id) from tbtest having count(id)>1
group by id
)
and id in (
select id from tbtest group by id having count(id)>1
)
and create_time<=to_timestamp('2016-02-25 11:26:52','yyyy-mm-dd hh24:mi:ss')
)
您没有死锁。这需要两个会话。在你的陈述上运行解释计划并发布结果。 – OldProgrammer
那么,你在桌上只有两列吗?如果你有两个以上的列,你确定知道如果序列号“id”是一样的,那么其他的都是一样的?或者你也不关心 - 如果你有相同的id,你只需要为每个序列号“id”保留最高't_id'的行(可能是最近的那一行)? – mathguy
是的,我想要一个记录,如果ID是相同的 – flower