选择和删除

发布于 2024-09-16 19:10:11 字数 791 浏览 14 评论 0原文

我需要从表中删除某些行。哪些行必须被删除是我通过查询找到的。但是,看来您无法在相同的查询

目前,您无法从 表并从同一个表中选择 在子查询中。

所以我不能这样做:

DELETE
FROM telefono
WHERE telefono_id IN (
    SELECT te.telefono_id
    FROM telefono te
    LEFT JOIN centro_telefono ce ON te.telefono_id=ce.telefono_id AND ce.telefono_id IS NOT NULL
    LEFT JOIN contacto_telefono co ON te.telefono_id=co.telefono_id AND co.telefono_id IS NOT NULL
    WHERE COALESCE(ce.telefono_id, co.telefono_id) IS NULL AND te.fecha_alta < DATE_SUB(NOW(), INTERVAL 1 DAY)
);
-- SQL Error (1093): You can't specify target table for update in FROM clause

如何在纯MySQL中实现这种记录清理?

服务器运行MySQL 5.1.39。

I need to remove certain rows from a table. Which rows must be deleted is something I find out through a query. However, it appears that you cannot do both operations (select and delete) in the same query:

Currently, you cannot delete from a
table and select from the same table
in a subquery.

So I cannot do this:

DELETE
FROM telefono
WHERE telefono_id IN (
    SELECT te.telefono_id
    FROM telefono te
    LEFT JOIN centro_telefono ce ON te.telefono_id=ce.telefono_id AND ce.telefono_id IS NOT NULL
    LEFT JOIN contacto_telefono co ON te.telefono_id=co.telefono_id AND co.telefono_id IS NOT NULL
    WHERE COALESCE(ce.telefono_id, co.telefono_id) IS NULL AND te.fecha_alta < DATE_SUB(NOW(), INTERVAL 1 DAY)
);
-- SQL Error (1093): You can't specify target table for update in FROM clause

How can I implement this record clean-up in pure MySQL?

The server runs MySQL 5.1.39.

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

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

发布评论

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

评论(3

风情万种。 2024-09-23 19:10:11

尝试使用连接执行删除语句

DELETE te
FROM telefono as te
    LEFT JOIN centro_telefono ce
        ON te.telefono_id=ce.telefono_id AND ce.telefono_id IS NOT NULL
    LEFT JOIN contacto_telefono co
        ON te.telefono_id=co.telefono_id AND co.telefono_id IS NOT NULL
WHERE
    COALESCE(ce.telefono_id, co.telefono_id) IS NULL
    AND te.fecha_alta < DATE_SUB(NOW(), INTERVAL 1 DAY)

Try doing the delete statement with the joins

DELETE te
FROM telefono as te
    LEFT JOIN centro_telefono ce
        ON te.telefono_id=ce.telefono_id AND ce.telefono_id IS NOT NULL
    LEFT JOIN contacto_telefono co
        ON te.telefono_id=co.telefono_id AND co.telefono_id IS NOT NULL
WHERE
    COALESCE(ce.telefono_id, co.telefono_id) IS NULL
    AND te.fecha_alta < DATE_SUB(NOW(), INTERVAL 1 DAY)
靖瑶 2024-09-23 19:10:11
 CREATE TEMPORARY TABLE tmptable
 SELECT te.telefono_id
 FROM telefono te
  LEFT JOIN centro_telefono ce ON te.telefono_id=ce.telefono_id AND ce.telefono_id IS NOT NULL
  LEFT JOIN contacto_telefono co ON te.telefono_id=co.telefono_id AND co.telefono_id IS NOT NULL
  WHERE COALESCE(ce.telefono_id, co.telefono_id) IS NULL AND te.fecha_alta < DATE_SUB(NOW(), INTERVAL 1 DAY)

 DELETE FROM telefono te
 WHERE te.telefono_id IN (Select telefono_id from tmptable)
 CREATE TEMPORARY TABLE tmptable
 SELECT te.telefono_id
 FROM telefono te
  LEFT JOIN centro_telefono ce ON te.telefono_id=ce.telefono_id AND ce.telefono_id IS NOT NULL
  LEFT JOIN contacto_telefono co ON te.telefono_id=co.telefono_id AND co.telefono_id IS NOT NULL
  WHERE COALESCE(ce.telefono_id, co.telefono_id) IS NULL AND te.fecha_alta < DATE_SUB(NOW(), INTERVAL 1 DAY)

 DELETE FROM telefono te
 WHERE te.telefono_id IN (Select telefono_id from tmptable)
娇女薄笑 2024-09-23 19:10:11

使用 UPDATE 标记要删除的行,然后使用 DELETE 实际删除它们。

UPDATE telefono SET marker_column='DELETE ME!!!!' WHERE telefono_id IN (...);
DELETE FROM telefono WHERE marker_column='DELETE ME!!!!';

Use UPDATE to mark rows for deletion, then DELETE to actually delete them.

UPDATE telefono SET marker_column='DELETE ME!!!!' WHERE telefono_id IN (...);
DELETE FROM telefono WHERE marker_column='DELETE ME!!!!';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文