使用 MS Access 查找重复项并将其删除

发布于 2024-11-28 21:36:39 字数 342 浏览 1 评论 0原文

我正在尝试从数据库中删除所有重复项。我在这个网站上找到了下面的代码。问题是它删除了我的整个数据库。遗憾的是,我对 MySQL 的了解不够,无法解释为什么它删除了所有条目而不仅仅是重复项。

DELETE FROM RosterPool AS t1
WHERE EXISTS (SELECT 1 from RosterPool t2
WHERE t1.Rate = t2.Rate
AND t1.FullName = t2.FullName
AND t1.Last4 = t2.Last4
AND t1.Graduated = t2.Graduated);

有没有办法将所有重复项移动到另一个表并删除该表?

I'm trying to delete all duplicates from my database. I found the code below on this website. Problem is it deleted my entire database. Sadly I don't know enough about MySQL to tell why it got rid of all entries and not just the duplicates.

DELETE FROM RosterPool AS t1
WHERE EXISTS (SELECT 1 from RosterPool t2
WHERE t1.Rate = t2.Rate
AND t1.FullName = t2.FullName
AND t1.Last4 = t2.Last4
AND t1.Graduated = t2.Graduated);

Is there a way to move all the duplicates to another table and just delete that table?

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

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

发布评论

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

评论(2

荒岛晴空 2024-12-05 21:36:39

SQL 数据库中不存在“移动”操作之类的东西。只有插入、更新和删除。

如果您想安全起见,您可以将删除查询转换为选择查询,以准确查看如果将其作为删除查询运行,将会删除什么,但是没有办法将行从一个表“移动”到另一个表,无需执行插入/选择+删除操作序列。

There's no such thing as a "move" operation in SQL databases. There's insert, update, and delete only.

If you want to play it safe, you can turn the delete query into a select query to see exactly WHAT will be nuked if you run it as a delete query, but there's no way to "move" a row from one table to another, without doing a insert/select + delete operation sequence.

杀手六號 2024-12-05 21:36:39

只需在要从中删除重复项的列上强制使用唯一键即可。
为您编写的列速率添加唯一键:

ALTER TABLE RosterPool ADD UNIQUE (Rate);

如果您已经有重复的行,则会出现错误,
但是通过添加关键字 IGNORE,您可以强制它添加 uniqe 并忽略警告

ALTER IGNORE TABLE RosterPool ADD UNIQUE (Rate);

just force a uniqe key on the column you want to remove duplicates from.
to add a unique key for column rate you write:

ALTER TABLE RosterPool ADD UNIQUE (Rate);

that going to give you an error if you already have duplicate rows,
but by adding the keyword IGNORE, you can force it to add the uniqe and ignore the warnings

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