MySql 删除 - 与选择(加入)相同
我将一些记录从一个表复制到另一个表 使用此查询:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 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
在执行这些步骤之前请先备份表。
按照以下步骤操作
第 1 步
第 2 步
第 3 步
好的,您可以使用临时表通过一个事务来完成它。
快乐查询!
PLEASE BACKUP THE TABLE BEFORE FOLLOWING THE STEPS.
Follow the following STEPS
STEP 1
STEP 2
STEP 3
OK, you may accomplish it with one transaction using TEMPORARY TABLES.
Happy Querying!