我有以下查询:
delete from customers_cards where id not in ( select min(id) from customers_cards group by number_card ) and belongs_to = "ezpay"
它报错:
#1093 - 在FROM子句中,不能为更新指定目标表'customers_cards'
我猜我需要使用join
作为一种解决方法,但老实说,我无法使用join
重写相同的逻辑。有没有办法用join
来编写上面的查询?
P粉0716263642024-01-17 17:19:02
这里是另一种方法:
删除属于 'ezpay' 的任何行 c1
,前提是存在另一行 c2
,它具有相同的 number_card
和较小的 id
。
DELETE c1 FROM customer_cards AS c1 LEFT OUTER JOIN customers_cards AS c2 ON c1.number_card = c2.number_card AND c1.id > c2.id WHERE c2.id IS NOT NULL AND c1.belongs_to = 'ezpay';
P粉8685860322024-01-17 16:32:20
这个连接应该类似于你在一个表中选择行但不在另一个表中选择的方法。
DELETE c1 FROM customers_cards AS c1 LEFT JOIN ( SELECT MIN(id) AS id FROM customer_cards GROUP BY number_card ) AS c2 ON c1.id = c2.id WHERE c2.id IS NULL AND c1.belongs_to = 'ezpay'