SQL 删除重复项 - 保留已删除的 ID
我需要在表中查找并删除重复项,同时保留要删除的记录的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要对另一列执行此操作 - 将 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.
如果您想像这样继续,这里有一个简单的方法。
确保在一笔交易中完成所有操作。
如果
tbl_del
中已经存在'John Smith'
条目,则您必须检查并UPDATE
以添加已删除的 ID。但我认为维塞利克的观点是有道理的。
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'
intbl_del
already, you have to check andUPDATE
to add the deleted ids instead.But I think Veseliq has a vaid point.
基本方法可以是首先从旧表中选择符合删除标准的所有行 -> 。
将这些行插入新表 ->
删除旧表中的所有行,
您可以在新表中拥有自动递增的键。
我还没有运行这些查询,如果有一些错误,请原谅我,这只是一个想法。
在这两个表中建立一对多关系在将来真的很方便......并且更容易查询数据
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
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