删除除一条重复记录之外的所有记录
我有一个表,应该记录给定配置文件的访问者(用户 ID 到用户 ID 对)。事实证明我的 SQL 查询有点不对劲,并且生成了多个对,而不是按预期生成单个对。事后看来,我应该对每个 id+id 对强制执行唯一的约束。
现在,我该如何清理桌子呢?我想要做的是删除所有重复的对并只留下一对。
例如,将其更改
23515 -> 52525 date_visited
23515 -> 52525 date_visited
23515 -> 52525 date_visited
12345 -> 54321 date_visited
12345 -> 54321 date_visited
12345 -> 54321 date_visited
12345 -> 54321 date_visited
23515 -> 52525 date_visited
...
为:
23515 -> 52525 date_visited
12345 -> 54321 date_visited
更新: 这是所要求的表结构:
id int(10) UNSIGNED Non Aucun AUTO_INCREMENT
profile_id int(10) UNSIGNED Non 0
visitor_id int(10) UNSIGNED Non 0
date_visited timestamp Non CURRENT_TIMESTAMP
I have a table that is supposed to keep a trace of visitors to a given profile (user id to user id pair). It turns out my SQL query was a bit off and is producing multiple pairs instead of single ones as intended. With hindsight I should have enforced a unique constraint on each id+id pair.
Now, how could I go about cleaning up the table? What I want to do is delete all duplicate pairs and leave just one.
So for example change this:
23515 -> 52525 date_visited
23515 -> 52525 date_visited
23515 -> 52525 date_visited
12345 -> 54321 date_visited
12345 -> 54321 date_visited
12345 -> 54321 date_visited
12345 -> 54321 date_visited
23515 -> 52525 date_visited
...
Into this:
23515 -> 52525 date_visited
12345 -> 54321 date_visited
Update: Here is the table structure as requested:
id int(10) UNSIGNED Non Aucun AUTO_INCREMENT
profile_id int(10) UNSIGNED Non 0
visitor_id int(10) UNSIGNED Non 0
date_visited timestamp Non CURRENT_TIMESTAMP
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
ANSI SQL解决方案
在子查询中使用group by:
您需要某种唯一标识符(这里我使用的是id)。
MySQL 解决方案
正如 @JamesPoulson 所指出的,这会导致 MySQL 出现语法错误;正确的解决方案是(如 James 的回答所示):
ANSI SQL Solution
Use group by in a subquery:
You need some kind of unique identifier(here, I'm using id).
MySQL Solution
As pointed out by @JamesPoulson, this causes a syntax error in MySQL; the correct solution is (as shown in James' answer):
这是 Frank Schmitt 的解决方案,其中有一个小解决方法,利用临时表来允许他的解决方案在 MySQL 上运行:
Here's Frank Schmitt's solution with a small workaround utilizing a temporary table to allow his solution to work on MySQL:
这会起作用:
This will work:
选择所有唯一行
将它们复制到新的临时表
截断原表
将临时表数据复制到原始表
这就是我会做的。我不确定是否有 1 个查询可以为您完成所有这些操作。
Select all unique rows
Copy them to a new temp table
Truncate original table
Copy temp table data to original table
That's what I'd do. I'm not sure if there's 1 query that would do all this for you.
上述答案都不起作用......要求是删除所有重复项,除了每组中的一个之外......但基于多个列数据......
尝试这个,
None of the above answers works..... the requirement is to delete all the duplicates and except one from each set.. but based on more than one column data..
try this,
如果您使用 SQL,您可以手动删除重复的行,保留一个条目,只需按照以下过程操作:
这是一个漫长的过程,但您可以立即实时看到结果。
希望这个解决方案对您有用!
If you are using SQL you can manually delete the duplicate rows keeping one entry just follow this procedure:
It's a long procedure but you can see the results immediately in real-time.
Hope this solution worked for you!!