SQL 删除重复项 - 保留已删除的 ID

发布于 2024-12-12 05:01:20 字数 1096 浏览 0 评论 0原文

我需要在表中查找并删除重复项,同时保留要删除的记录的 ID 记录,并将它们添加到“deleted_ids”字段中。

例如,这是我当前拥有的表格的一部分:

+--------+-------------------------------------------------------+
| Id     | Name                                                  |
+--------+-------------------------------------------------------+
|   9627 |  John Smith                                           |
|   9657 |  John Smith                                           |
|  24455 |  John Smith                                           |
|  67435 |  John Smith                                           |
+--------+-------------------------------------------------------+

我想要实现的结果是:

+--------+-----------------------------+-------------------------+
| Id     | Name                        | Deleted_Ids             |
+--------+-----------------------------+-------------------------+
|   9627 |  John Smith                 | 9657,24455,67435        |
+--------+-----------------------------+-------------------------+

我对任何实用方法持开放态度,如果有人可以提供解决方案或提出建议,我将非常感激。

谢谢, 保罗

I need to find and delete duplicates from within a table, yet keep a record of the IDs of the records being deleted and add them to a 'deleted_ids' field.

For example, here's part of a table I have currently:

+--------+-------------------------------------------------------+
| Id     | Name                                                  |
+--------+-------------------------------------------------------+
|   9627 |  John Smith                                           |
|   9657 |  John Smith                                           |
|  24455 |  John Smith                                           |
|  67435 |  John Smith                                           |
+--------+-------------------------------------------------------+

The result I'd like to achieve is:

+--------+-----------------------------+-------------------------+
| Id     | Name                        | Deleted_Ids             |
+--------+-----------------------------+-------------------------+
|   9627 |  John Smith                 | 9657,24455,67435        |
+--------+-----------------------------+-------------------------+

I'm open to any practical approach, if anyone can provide a solution or give suggestions I'd really appreciate it.

Thanks,
Paul

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

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

发布评论

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

评论(3

¢蛋碎的人ぎ生 2024-12-19 05:01:20

不要对另一列执行此操作 - 将 ID 存储为逗号分隔列表感觉不对,不是吗?

最好是有一个第二个表,其中包含两列 PrimaryUserId 和 DeletedUserId - 例如,PrimaryUserId 将为“9627” - 您实际保留的记录之一,您将为从主表中删除的每个重复项添加一行。这种方法还允许您存储一个知道重复条目何时被删除的列。

Don't do it with another column - storing IDs as comma separated list doesn't feel right, does it?

Best would be to have a second table with two columns PrimaryUserId and DeletedUserId - the PrimaryUserId would be "9627" for example - the one of the record you actually keep and you will add a row for every duplicate you delete from your main table. This approach would also allow for you to store a column that knows when the duplicate entry was deleted.

好菇凉咱不稀罕他 2024-12-19 05:01:20

如果您想像这样继续,这里有一个简单的方法。
确保在一笔交易中完成所有操作。
如果 tbl_del 中已经存在 'John Smith' 条目,则您必须检查并UPDATE 以添加已删除的 ID。

BEGIN;

INSERT INTO tbl_del
SELECT name, GROUP_CONCAT(id) AS deleted
FROM   tbl_live
WHERE  name = 'John Smith'
GROUP  BY 1;

DELETE FROM tbl_live
WHERE  name = 'John Smith';

COMMIT;

但我认为维塞利克的观点是有道理的。

If you want to proceed like this, here is a simple way.
Do it all in one transaction to be sure.
If there could be an entry of 'John Smith' in tbl_del already, you have to check and UPDATE to add the deleted ids instead.

BEGIN;

INSERT INTO tbl_del
SELECT name, GROUP_CONCAT(id) AS deleted
FROM   tbl_live
WHERE  name = 'John Smith'
GROUP  BY 1;

DELETE FROM tbl_live
WHERE  name = 'John Smith';

COMMIT;

But I think Veseliq has a vaid point.

谁人与我共长歌 2024-12-19 05:01:20

基本方法可以是首先从旧表中选择符合删除标准的所有行 -> 。
将这些行插入新表 ->
删除旧表中的所有行,

INSERT INTO new_table
SELECT id,name FROM old_table WHERE id > 
(
  SELECT min(id) FROM old_table B
  WHERE A.name = B.name
);

delete FROM old_table WHERE id > 
(
  SELECT min(id) FROM old_table B
  WHERE A.name = B.name
);

您可以在新表中拥有自动递增的键。
我还没有运行这些查询,如果有一些错误,请原谅我,这只是一个想法。
在这两个表中建立一对多关系在将来真的很方便......并且更容易查询数据

The basic approach can be to first select all rows from old table, that qualifies the deletion criteria ->
Insert these rows into new table ->
delete all rows from old table

INSERT INTO new_table
SELECT id,name FROM old_table WHERE id > 
(
  SELECT min(id) FROM old_table B
  WHERE A.name = B.name
);

delete FROM old_table WHERE id > 
(
  SELECT min(id) FROM old_table B
  WHERE A.name = B.name
);

you can have auto incremented key in new table.
I have not run these queries, pardon me if here are some errors, thats just for an idea.
Having one to many relationship within these two tables can really be handy in future....and easier to query the data

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