我知道有很多答案使用rank()或row_number()表示,但是,SQL-FRONT或Navicat不支持它们,我尝试使用Microsoft SSMS,但它不支持我的sql。我尝试了很多方法,但是没有用。所以我现在无望了。
表如下所示,我只想知道如何删除具有相同名称和时间的重复记录。谢谢。
答案 0 :(得分:1)
使用EXISTS
子句查找重复项。
delete from mytable
where exists
(
select *
from mytable other
where other.name = mytable.name
and other.time = mytable.time
and other.id < mytable.id
);
对于插入:在两列上放置唯一约束。通常使用唯一索引来完成此操作:
create unique index idx_unique_name_time on mytable (name, time);
答案 1 :(得分:0)
在MySQL中,您可以使用join
和聚合:
delete t
from t join
(select name, time, min(id) as min_id
from t
group by name, time
) tt
on t.name = tt.name and t.time = tt.time and
t.id > tt.min_id;