使用 MySQL 从表中删除重复数据

发布于 2025-01-01 22:30:36 字数 984 浏览 1 评论 0原文

我正在尝试从数据库中删除重复的数据。我发现这里有一个很好的示例,说明如何在 Oracle 数据库上执行此操作。

该答案中的底部查询(仅选择重复的行)在 MySQL 中有效,但删除查询(见下文)却不起作用...

"DELETE FROM studios as a
 WHERE a.id >
       ANY (SELECT b.id
              FROM studios as b
             WHERE a.name = b.name
               AND a.email  = b.email
            )"

我得到的错误是:

You have an error in your SQL syntax; check the manual that corresponds to your 
MySQL server version for the right syntax to use near 'a
 WHERE a.id >
       ANY (SELECT b.id
              FROM studios as b
           ' at line 1

所以,我寻找了正确的 删除语法任何语法使用,但不能'没有发现我的查询有任何问题...有什么想法吗?

I am trying to remove duplicate data from my database. I found a nice example on here of how to do this on an oracle database.

The bottom query from that answer (only selecting the duplicate rows) works in MySQL, but the delete query (see below) does not...

"DELETE FROM studios as a
 WHERE a.id >
       ANY (SELECT b.id
              FROM studios as b
             WHERE a.name = b.name
               AND a.email  = b.email
            )"

The error I get is:

You have an error in your SQL syntax; check the manual that corresponds to your 
MySQL server version for the right syntax to use near 'a
 WHERE a.id >
       ANY (SELECT b.id
              FROM studios as b
           ' at line 1

So, I had a look for the right delete syntax and any syntax to use, but couldn't find anything wrong with my query... Any ideas?

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

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

发布评论

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

评论(2

七分※倦醒 2025-01-08 22:30:36

尝试这个查询 -

DELETE t1 FROM studios t1
  JOIN (SELECT MIN(id) id, name, email FROM studios GROUP BY name, email) t2
    ON t1.id <> t2.id AND t1.name = t2.name AND t1.email = t2.email;

Try this query -

DELETE t1 FROM studios t1
  JOIN (SELECT MIN(id) id, name, email FROM studios GROUP BY name, email) t2
    ON t1.id <> t2.id AND t1.name = t2.name AND t1.email = t2.email;
安稳善良 2025-01-08 22:30:36

AS 运算符不适用于 MySQL 中的 DELETE 语句。

试试这个(未验证):

DELETE FROM a using studios a
WHERE a.id >
ANY 
(
    SELECT b.id
    FROM studios as b
    WHERE a.name = b.name
    AND a.email  = b.email
)

The AS operator doesn't work with DELETE statements in MySQL.

Try this (unverified):

DELETE FROM a using studios a
WHERE a.id >
ANY 
(
    SELECT b.id
    FROM studios as b
    WHERE a.name = b.name
    AND a.email  = b.email
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文