从表中删除重复记录 - SQL查询

时间:2009-11-17 12:14:40

标签: sql sql-server database sql-delete

我只需要从表中删除重复的行,就像我在表中有3个重复的行一样,我的查询将从3个重复的行中删除2行。

我怎么能得到这个?请帮帮我。

7 个答案:

答案 0 :(得分:12)

请尝试以下查询,它绝对符合您的目标

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

测试是您的表名

答案 1 :(得分:4)

这在SQL Server中有效,尽管它不是单个语句:

Declare @cnt int; 
Select @cnt=COUNT(*) From DupTable Where (Col1=1);  -- Assumes you are trying to delete the duplicates where some condition (e.g. Col1=1) is true.
Delete Top (@cnt-1) From DupTable

它也不需要任何额外的假设(比如使每行唯一的另一列的存在)。毕竟,Santanu确实说是重复的而不仅仅是一列。

但是,在我看来,正确答案是获得真正的表格结构。也就是说,在此表中添加IDENTITY列,以便您可以使用单个SQL命令来完成工作。像这样:

ALTER TABLE dbo.DupTable ADD
    IDCol int NOT NULL IDENTITY (1, 1)
GO

然后删除是微不足道的:

DELETE FROM DupTable WHERE IDCol NOT IN 
   (SELECT MAX(IDCol) FROM DupTable GROUP BY Col1, Col2, Col3)

答案 2 :(得分:3)

DELETE FROM Table t1, Table t2 WHERE t1.colDup = t2.colDup AND t1.date < t2.date

将删除Table(列colDup)上的每个重复行,但最旧的行(即低位date)。

答案 3 :(得分:2)

DELETE FROM `mytbl`
    INNER JOIN (
        SELECT 1 FROM `mytbl`
        GROUP BY `duplicated_column` HAVING COUNT(*)=2
    ) USING(`id`)

编辑:

我的不好,以上查询无效。

假设表结构:

id int auto_increment

num int#&lt; - 这是具有重复值的列

以下查询适用于MySQL(我检查过):

DELETE `mytbl` FROM `mytbl` 
    INNER JOIN 
    (
        SELECT `num` FROM `mytbl`
        GROUP BY `num` HAVING COUNT(*)=2
    ) AS `tmp` USING (`num`)

该查询将删除num列中包含2个(不多于或其他)重复值的行。

再次编辑:

我建议在num列添加密钥。

编辑(#3):

如果作者想要删除重复的,以下内容应该适用于MySQL(它适用于我):

DELETE `delete_duplicated_rows` FROM `delete_duplicated_rows`
    NATURAL JOIN (
        SELECT *
        FROM `delete_duplicated_rows`
        GROUP BY `num1` HAVING COUNT(*)=2
    ) AS `der`

虽然假设表结构是:

CREATE TABLE `delete_duplicated_rows` (
  `num1` tinyint(4) DEFAULT NOT NULL,
  `num2` tinyint(4) DEFAULT NOT NULL
) ENGINE=MyISAM;

答案 4 :(得分:1)

如果您要删除要删除的行的ID,那么......

DELETE FROM table WHERE id IN (1, 4, 7, [id numbers to delete...])

答案 5 :(得分:1)

我认为每个表都有唯一的标识符。 因此,如果它存在,那么您可以编写以下查询: 从表1 t1中删除Table1,其中2> =(从表1中选择count(id),其中dupColumn = t1.dupColumn)和 t1.id not in(从Table1中选择max(id)dupColumn = t1.dupColumn)

糟糕似乎只能使用第二个过滤器 从Table1 t1中删除Table1 t1.id not in(从Table1中选择max(id)dupColumn = t1.dupColumn)

答案 6 :(得分:1)

  -- Just to demonstrates Marks example          
    . 
        -- START === 1.0.dbo..DuplicatesTable.TableCreate.sql
    /****** Object:  Table [dbo].[DuplicatesTable] 
        Script Date: 03/29/2010 21:24:02 ******/
      IF EXISTS (SELECT * FROM sys.objects 
     WHERE 
object_id = OBJECT_ID(N'[dbo].[DuplicatesTable]') 
AND type in (N'U'))
        DROP TABLE [dbo].[DuplicatesTable]
    GO

    /****** Object:  Table [dbo].[DuplicatesTable]    
Script Date: 03/29/2010 21:24:02 ******/
    SET ANSI_NULLS ON
    GO

    SET QUOTED_IDENTIFIER ON
    GO

    CREATE TABLE [dbo].[DuplicatesTable](
        [ColA] [varchar](10) NOT NULL, -- the name of the DuplicatesTable
        [ColB] [varchar](10) NULL,  -- the description of the e DuplicatesTable 
     ) 


    /* 
    <doc> 
    Models a DuplicatesTable for 
    </doc>

    */


    GO


    --============================================================ DuplicatesTable START
    declare @ScriptFileName varchar(2000)
    SELECT @ScriptFileName = '$(ScriptFileName)'
    SELECT @ScriptFileName + ' --- DuplicatesTable START =========================================' 
    declare @TableName varchar(200)
    select @TableName = 'DuplicatesTable'

    SELECT 'SELECT name from sys.tables where name =''' + @TableName + ''''
    SELECT name from sys.tables 
    where name = @TableName

    DECLARE @TableCount INT 
    SELECT @TableCount  = COUNT(name ) from sys.tables 
        where name =@TableName

    if @TableCount=1
    SELECT ' DuplicatesTable PASSED. The Table ' + @TableName + ' EXISTS ' 
    ELSE 
    SELECT ' DuplicatesTable FAILED. The Table ' + @TableName + ' DOES NOT EXIST ' 
    SELECT @ScriptFileName + ' --- DuplicatesTable END =========================================' 
    --============================================================ DuplicatesTable END

    GO


    -- END ===  1.0.dbo..DuplicatesTable.TableCreate.sql

    . 
    -- START === 1.1..dbo..DuplicatesTable.TableInsert.sql

    BEGIN TRANSACTION;
    INSERT INTO [dbo].[DuplicatesTable]([ColA], [ColB])
    SELECT   N'ColA', N'ColB' UNION ALL
    SELECT N'ColA', N'ColB' UNION ALL
    SELECT  N'ColA', N'ColB' UNION ALL
    SELECT  N'ColA', N'ColB' UNION ALL
    SELECT  N'ColA', N'ColB' UNION ALL
    SELECT  N'ColA', N'ColB' UNION ALL
    SELECT  N'ColA', N'ColB' UNION ALL
    SELECT  N'ColA1', N'ColB1' UNION ALL
    SELECT  N'ColA1', N'ColB1' UNION ALL
    SELECT  N'ColA1', N'ColB1' UNION ALL
    SELECT  N'ColA1', N'ColB1' UNION ALL
    SELECT  N'ColA1', N'ColB1' UNION ALL
    SELECT  N'ColA1', N'ColB1' UNION ALL
    SELECT  N'ColA1', N'ColB1'
    COMMIT;
    RAISERROR (N'[dbo].[DuplicatesTable]: Insert Batch: 1.....Done!', 10, 1) WITH NOWAIT;
    GO


    -- END ===  1.1..dbo..DuplicatesTable.TableInsert.sql

    . 
    -- START === 2.0.RemoveDuplicates.Script.sql
    ALTER TABLE dbo.DuplicatesTable ADD
            DuplicatesTableId int NOT NULL IDENTITY (1, 1)
    GO

    -- Then the delete is trivial:
    DELETE FROM dbo.DuplicatesTable WHERE DuplicatesTableId NOT IN 
         (SELECT MAX(DuplicatesTableId) FROM dbo.DuplicatesTable GROUP BY ColA , ColB)

         Select * from DuplicatesTable ;  
    -- END ===  2.0.RemoveDuplicates.Script.sql