MySql 删除 - 与选择(加入)相同

发布于 2024-11-05 20:54:42 字数 874 浏览 3 评论 0原文

我将一些记录从一个表复制到另一个表 使用此查询:

insert into pages_finished (keyword,pages,resultlist,done,current)
select keyword,pages,resultlist,done,current 
 from pages_done o  
 where  (select count(*) as totalPages from pages_done x  where x.keyword = o.keyword)-1 = pages 

现在我想从源表中删除相同的记录, 我以为这很简单:

delete from pages_done o  
 where  (select count(*) as totalPages from pages_done x  where x.keyword = o.keyword)-1 = pages 

但这行不通。 谁能告诉我这样做的正确方法是什么?

在@bgdrl回答之后,我正在考虑只运行选择, 获取应复制的所有记录的 id, 然后删除; 但我认为一定有一个更简单的解决方案,有人吗?

尽管将 @bgdrl 答案标记为正确答案, 正因为如此,这是一个事实。

对于任何对我最终所做的事情感兴趣的人: 我做了与开始时相同的选择(但只选择了 id 列,因为选择所有列会杀死我可怜的计算机), 将其导出到 INSERT STATMENTS(使用 mysqlworkbench), 在记事本中打开文本文件, 将所有 INSERT INTO... 替换为 DELETE FROM WHERE ID=, 并在 mysql 中运行该查询。

我觉得用这种方式很愚蠢,但显然别无选择。

I copied some records from one table to another
with this query :

insert into pages_finished (keyword,pages,resultlist,done,current)
select keyword,pages,resultlist,done,current 
 from pages_done o  
 where  (select count(*) as totalPages from pages_done x  where x.keyword = o.keyword)-1 = pages 

Now I want to delete the same records from the source table,
I was thinking it would be simple as:

delete from pages_done o  
 where  (select count(*) as totalPages from pages_done x  where x.keyword = o.keyword)-1 = pages 

but that doesn't work.
Could anyone tell me what is the right way to do that?

After @bgdrl answer, I'm thinking about running only the select,
get the id's of all records that should be copied,
and then delete;
but I think there must be an easier solution, anyone?

Even though marked @bgdrl answer as the right answer,
it is only because of that a fact.

To anyone interested with what I ended up doing :
I did the same select I started with (but selected only the id column, since selecting all the columns would have killed my poor computer),
exported it to an INSERT STATMENTS (using mysqlworkbench),
opened the text file in notepad,
replaced all the INSERT INTO... with DELETE FROM WHERE ID=,
and run that query in mysql.

I feel so stupid using this way, but had no other choice apparently.

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

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

发布评论

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

评论(2

花之痕靓丽 2024-11-12 20:54:42

来自 mysql 站点:“您不能修改表并在子查询中从同一个表中进行选择。这适用于 DELETE、INSERT、REPLACE、UPDATE 等语句”。 http://dev.mysql.com/doc/refman/5.1/en /子查询.html

From mysql site: "You cannot modify a table and select from the same table in a subquery. This applies to statements such as DELETE, INSERT, REPLACE, UPDATE". http://dev.mysql.com/doc/refman/5.1/en/subqueries.html

郁金香雨 2024-11-12 20:54:42

在执行这些步骤之前请先备份表。

按照以下步骤操作

第 1 步

CREATE TABLE pages_done_ids 
    SELECT o.id FROM pages_done AS o  
    WHERE 
    (
        SELECT count(*) AS totalPages 
        FROM pages_done AS x  
        WHERE x.keyword = o.keyword
    )-1 = o.pages

第 2 步

DELETE FROM pages_done AS o  
WHERE o.id IN (SELECT id FROM pages_done_ids)

第 3 步

DROP TABLE pages_done_ids;

好的,您可以使用临时表通过一个事务来完成它。

快乐查询!

PLEASE BACKUP THE TABLE BEFORE FOLLOWING THE STEPS.

Follow the following STEPS

STEP 1

CREATE TABLE pages_done_ids 
    SELECT o.id FROM pages_done AS o  
    WHERE 
    (
        SELECT count(*) AS totalPages 
        FROM pages_done AS x  
        WHERE x.keyword = o.keyword
    )-1 = o.pages

STEP 2

DELETE FROM pages_done AS o  
WHERE o.id IN (SELECT id FROM pages_done_ids)

STEP 3

DROP TABLE pages_done_ids;

OK, you may accomplish it with one transaction using TEMPORARY TABLES.

Happy Querying!

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