达梦删除重复数据的问题
达梦7数据库如何删除重复记录,并保留ID最大的一条
比如数据是这样的:

我想删掉BH重复的数据,比如张三的数据是重复的,我想删掉id为5、7的数据,保留id最大的一条9的数据
我来答
添加附件
收藏
复制链接
微信扫码分享
在小程序上查看
分享
添加附件
问题补充
5条回答
默认
最新
我以MySQL 举例
create table test_d
(
id int,
bh int,
xm varchar(10)
);
insert into test_d
select 5,1,'张三' union all
select 7,1,'张三' union all
select 9,1,'张三' union all
select 1,4,'李四' union all
select 2,4,'李四' union all
select 3,4,'李四' union all
select 22,8,'王五' union all
select 23,8,'王五' union all
select 24,8,'王五' ;
delete t1 from test_d t1 inner join
(select bh,xm,max(id) as maxid from test_d group by bh,xm) as t2 on t1.bh = t2.bh and t1.xm = t2.xm and t1.id < t2.maxid;
select * from test_d
删除后的结果

评论
有用 0
回答交流
提交
问题信息
请登录之后查看
邀请回答
暂无人订阅该标签,敬请期待~~
墨值悬赏

