从表中删除重复记录 - SQL查询 [英] Deleting duplicate record from table - SQL query

查看:119
本文介绍了从表中删除重复记录 - SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从表中删除重复的行,例如我在表中有3个重复的行,我的查询将从3个重复的行中删除2行。



我如何得到这个?请帮助我。

解决方案

请尝试以下查询,它肯定会满足您的目标



SET ROWCOUNT 1
DELETE test
FROM test a
WHERE(SELECT COUNT(*)FROM test b WHERE b.name = a.name)> 1
WHILE @@ rowcount> 0
DELETE test
FROM test a
WHERE(SELECT COUNT(*)FROM test b WHERE b.name = a.name)> 1
SET ROWCOUNT 0

其中 test


I need to delete duplicate rows only from the table, like I have 3 duplicate rows in the table, my query will delete 2 rows from 3 duplicated rows.

How can I get this? Please help me.

解决方案

Please try the below query, it will definitely meet your objective

SET ROWCOUNT 1
DELETE test
FROM test a
WHERE (SELECT COUNT(*) FROM test b WHERE b.name = a.name) > 1
WHILE @@rowcount > 0
  DELETE test
  FROM test a
  WHERE (SELECT COUNT(*) FROM test b WHERE b.name = a.name) > 1
SET ROWCOUNT 0

where test is your table name

这篇关于从表中删除重复记录 - SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆