apache表行?

lawou6xi  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(333)

我在mariadb和apache中使用最新版本的xampp 2018。
我正在执行以下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\u id条目,包括使用count(*)>1,这也会返回一个错误。

1yjd4xko

1yjd4xko1#

MySQL 不允许你 DELETE 以及 SELECT 在同一张table上。
您可以尝试在中使用子查询 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
 )

db fiddle视图

ecbunoof

ecbunoof2#

如果要删除除第一行以外的所有行或行的任何小子集,以下是一种更快的方法:

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;

相关问题