MySQL REMOVE Using Sub-Query Using Availability and Quantity

I am trying to DELETE multiple entries using the following query:

First, I find the entries I want to delete using this query:

SELECT guid FROM account GROUP BY guid,type HAVING count(type) > 1); 

Then I add this query to the DELETE statement:

 DELETE FROM account WHERE guid IN (SELECT guid FROM account GROUP BY guid,type HAVING count(type) > 1); 

But I get this error:

You cannot specify the "account" of the target table to update in the FROM clause

+4
source share
3 answers

I think you need to use a temporary table to achieve your needs, as shown below:

  • Step 1: create a temporary table

     CREATE TEMPORARY TABLE MyTemp SELECT guid FROM account GROUP BY guid,type HAVING count(type) > 1; 
  • Use the temp table in the delete statement

     DELETE FROM account WHERE guid IN (SELECT guid FROM MyTemp); 
  • Drop temporary table

     DROP TEMPORARY TABLE MyTemp; 

EDIT: I think working with * two nested tables also works:

  DELETE FROM account WHERE guid IN (SELECT guid FROM (SELECT guid FROM account GROUP BY guid,type HAVING count(type) > 1) as MyTemp ) 
+1
source

Your problem is resolved, just follow these steps.

  DELETE FROM account WHERE guid IN (SELECT * FROM (SELECT guid FROM account GROUP BY guid,type HAVING count(type) > 1) AS a); 
0
source

First create a view

 create view view_acct as SELECT guid FROM account GROUP BY guid,type HAVING count(type) > 1; 

After use

 DELETE FROM account WHERE guid in (select * from view_acct); 
-1
source

Source: https://habr.com/ru/post/1338910/


All Articles