【问题标题】:SQL how to remove duplicate recordsSQL如何删除重复记录
【发布时间】:2020-03-31 06:25:18
【问题描述】:

如何通过删除重复记录来清理表?

+----------+--------+------------+
| clientID | status | Insertdate |
+----------+--------+------------+
|        1 | new    |   20191206 |
|        1 | new    |   20191206 |
|        2 | old    |   20191206 |
|        2 | old    |   20191206 |
|        3 | new    |   20191205 |
|        3 | new    |   20191205 |
+----------+--------+------------+

我没有任何身份字段。

【问题讨论】:

  • 这能回答你的问题吗? Remove duplicate rows in MySQL
  • 你的 rdbms , oracle,sql-server,mysql 是什么?
  • 您使用的是哪个DBMS 产品? “SQL”只是一种查询语言,而不是特定数据库产品的名称。请为您正在使用的数据库产品添加tagWhy should I tag my DBMS
  • 通常你会做一些研究,试一试,然后发布你的问题尝试。

标签: sql sql-server duplicates sql-delete


【解决方案1】:

希望这对您正在运行 MySQL 数据库有所帮助

SELECT clientID, status, Insertdate, count(*)
FROM table_name
GROUP BY clientID, status, Insertdate
having count(*) > 1

【讨论】:

  • 不仅不是MySQL,OP还问过如何删除重复记录。
  • $limit = $row["count(*)"] - 1; mysqli_query($link, "DELETE FROM table_name WHERE clientID='$clientID' AND status='$status' AND Insertdate='$Insertdate' LIMIT $limit");
【解决方案2】:

请找到以下查询。您可以使用行号。

;WITH cte as (
  select clientid
    , status, Insertdate
    , ROW_NUMBER() over (partition by clientid, status, Insertdate order by clientid) RowNumber 
  from Yourtable
) 
delete from cte where RowNumber > 1

【讨论】:

  • 好的,谢谢!通过您的查询,我只能选择第一条记录。我怎样才能删除其他人?
  • 使用相同的查询执行删除,并将 end where 更改为 a.RowNumber > 1 使用删除查询。
  • 从 Yourtable 中删除 (select clientid, status, Insertdate ,ROW_NUMBER() over (partition by clientid,status, Insertdate order by clientid) RowNumber from Yourtable) a.RowNumber >1 不起作用。 ..
  • @developerEmiliano 我已经编辑了查询。请看看它是否有效。谢谢。
  • @developerEmiliano 如果它解决了您的问题,请将其标记为答案,以便其他人也可以从中受益。
猜你喜欢
  • 2016-01-07
  • 2010-10-24
  • 2019-10-07
  • 1970-01-01
  • 2020-07-16
  • 2021-02-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多