如何在SQL中删除多个MySql Apache表行?

时间:2018-09-26 21:44:05

标签: mysql sql xampp mariadb

我正在将最新版本的XAMPP 2018与MariaDB和Apache结合使用。

我正在执行此sql:

 DELETE FROM customer_info 
 WHERE id NOT IN (SELECT MIN(id) FROM `customer_info` GROUP BY `paypal_id`) ;

并收到此错误

 Error
 SQL query: 

 DELETE FROM customer_info WHERE id NOT IN (SELECT MIN(id) FROM `customer_info` GROUP BY `paypal_id`)
 MySQL said: Documentation

 #1093 - Table 'customer_info' is specified twice, both as a target for 'DELETE' and as a separate source for data

我尝试了多种方法来消除重复的paypal_id条目,包括使用COUNT(*)> 1也会返回错误。

2 个答案:

答案 0 :(得分:0)

MySQL不允许您在同一表格中DELETESELECT

您可以尝试在NOT IN中使用子查询。

DELETE FROM customer_info 
 WHERE id NOT IN (
   SELECT ID FROM (
     SELECT MIN(id) Id
     FROM `customer_info` 
     GROUP BY `paypal_id`
   ) t1
 ) 

View on DB Fiddle

答案 1 :(得分:0)

如果要删除除第一行以外的所有内容,或者要删除其中的任何一小部分,请使用以下更快的方法:

CREATE TABLE new LIKE real;
INSERT INTO new
    SELECT * FROM real WHERE ...;   -- see below
RENAME TABLE real TO old,
             new TO real;
DROP TABLE old;

在您的情况下:

SELECT * FROM real ORDER BY id LIMIT 1;