求一删除语句

arfeng75 2006-11-21 04:36:06
有下面的表TEST:
-------------------------
序号 工号
1 D01
2 D02
3 E07
4 E08
5 D02
6 E07

--------------------------

求一删除语句,可以删除上表中“工号”重复且“序号”大的记录,只保留“序号”最小的记录,比方说上表删除以后的结果是:

序号 工号
1 D01
2 D02
3 E07
4 E08
...全文
160 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
mengwuxing 2006-11-21
  • 打赏
  • 举报
回复
=========================================================
delete from test
where 序号 not in (select min(序号) from test group by 工号)

marco08 2006-11-21
  • 打赏
  • 举报
回复
delete test where 序号 not in(
select min(序号) from test group by 工号
)
chd2001 2006-11-21
  • 打赏
  • 举报
回复
delete from tb a
where xh in
(select xh from tb where a.xh>xh group by gh having count(1)>1)
dulei115 2006-11-21
  • 打赏
  • 举报
回复
delete from test
where 序号 not in (select min(序号) from test group by 工号)
chd2001 2006-11-21
  • 打赏
  • 举报
回复
delete from tb a
where xh in
(select xh from tb where not exists(a.xh<xh) group by gh having count(1)>1)
CSDMN 2006-11-21
  • 打赏
  • 举报
回复
or:

delete a
from test a
where 序号<> (
select min(序号) from test
where 工号=a.工号
)
CSDMN 2006-11-21
  • 打赏
  • 举报
回复
delete a
from test a
where exists (
select 1 from test
where 工号=a.工号
and 序号<a.序号
)
arfeng75 2006-11-21
  • 打赏
  • 举报
回复
请注意是删除语句。谢谢

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧