我想从MySQL表中删除重复的行。请点击下面的链接查看表格数据

时间:2019-04-02 09:59:05

标签: mysql-workbench

我尝试使用此查询来执行此操作,但是它不起作用...

DELETE FROM employee 
    WHERE ( SELECT * FROM
            (SELECT row_number() OVER (partition by id) rn FROM employee) alias
          ) > 1;

Please click on this link to view the table

上述查询无法正常工作,并显示以下错误消息:

  

错误代码:1242。子查询返回的行多于1

2 个答案:

答案 0 :(得分:0)

尝试使用子查询,如下所示

   delete from  
      ( 
        select *.row_number() over (partition by id order by id) rn
        from employee  
      ) alias where rn > 1;

答案 1 :(得分:0)

您正在将整数(1)与子查询返回的行集进行匹配,SQL不允许这样做 您可以将整数(1)与子查询返回的单个值进行匹配。

使用以下查询(使用CTE)删除重复项。

;WITH TempEmp (id,duplicateRecCount)
AS
(
SELECT id,ROW_NUMBER() OVER(PARTITION by id ORDER BY id) 
AS duplicateRecCount
FROM employee 
)
DELETE FROM TempEmp
WHERE duplicateRecCount > 1