Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySql Delete - same like select ( join )

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.

like image 605
Rafael Herscovici Avatar asked Jan 28 '26 00:01

Rafael Herscovici


1 Answers

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

like image 101
dabuno Avatar answered Feb 01 '26 08:02

dabuno



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!