如何查找/删除同一行中的重复记录

发布于 2024-10-14 03:29:16 字数 411 浏览 7 评论 0原文

可以查询同一行是否有重复记录?

我试图找到解决方案,但我能找到的只是检测列中的重复字段,而不是行中的重复字段。

例如,假设我有一个包含行和项目的表:

| id | item1 | item2 | item3 | item4 | item5 | upvotes | downvotes |
--------------------------------------------------------------------
| 1  |  red  | blue  |  red  | black | white |   12    |    5      |

所以我想看看是否可以进行查询来检测具有相同记录(在本例中为红色)的字段并删除它们,和/或如何重新设计我的结构不允许重复。

谢谢。

It's possible to make a query to see if there is duplicated records in the same row?

I tried to find a solution but all I can find is to detected duplicated fields in columns, not in rows.

example, let's say I have a table with rows and items:

| id | item1 | item2 | item3 | item4 | item5 | upvotes | downvotes |
--------------------------------------------------------------------
| 1  |  red  | blue  |  red  | black | white |   12    |    5      |

So I want to see if is possible to make a query to detect the fields with the same record (in this case red) and delete them, and/or how to redesign my structure to not allow the duplicates.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

关于从前 2024-10-21 03:29:16

通过重新设计:

您可以将布局重新设计为如下所示:

| id | item |

使用 (id, item) 作为主键,这将禁止给定 id 拥有两次相同的 item

因此数据将如下所示:

| id | item |
| 1  | red  |
| 1  | blue |
...

尝试插入 | 1 | red | 再次会抛出错误。

无需重新设计:

如果您不想更改布局,此查询将查找其中一个 fieldX 字段等于同一行的另一个 fieldX 的行

SELECT t.id FROM (select 1) dummy
JOIN (SELECT id, item1 as item FROM table
      UNION SELECT id, item2 as item FROM table
      UNION SELECT id, item3 as item FROM table
      UNION SELECT id, item4 as item FROM table
      UNION SELECT id, item5 as item FROM table) t
GROUP BY t.id
HAVING count(*) < 5

:(假设您有 5 个 fieldX 列。)

这实际上会计算每个 id 的不同 fieldX 值。如果计数与字段数不同,则存在重复项。该查询返回存在重复项的行的 ID。

测试后您可以删除行

DELETE FROM table WHERE id IN ( <the previous query here> )

With redesign:

You may redesign the layout to something like this:

| id | item |

With (id, item) as primary key, this will forbid having twice the same item for a given id.

So the data will looks like this:

| id | item |
| 1  | red  |
| 1  | blue |
...

Trying to insert a | 1 | red | again will throw an error.

Without redesign:

If you don't want to change the layout, this query will find rows in which one of the fieldX field equals an other fieldX of the same row:

SELECT t.id FROM (select 1) dummy
JOIN (SELECT id, item1 as item FROM table
      UNION SELECT id, item2 as item FROM table
      UNION SELECT id, item3 as item FROM table
      UNION SELECT id, item4 as item FROM table
      UNION SELECT id, item5 as item FROM table) t
GROUP BY t.id
HAVING count(*) < 5

(Assuming you have 5 of those fieldX columns.)

This actually counts the different fieldX values for each id. If the count is different than the number of fields, then there is a duplicate. The query returns the ids of the rows in which there is a duplicate.

After testing you can delete rows with

DELETE FROM table WHERE id IN ( <the previous query here> )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文