请看一下我在MySql Workbench中写的这句话:
update customer set flag = 1
where id in (select id from customer
group by phoneNumber
having count(phoneNumber) > 1);
显示以下消息:
Error Code: 1093. You can't specify target table 'customer' for update in FROM clause
我做错了什么?
答案 0 :(得分:1)
试试:
update customer set flag = 1
where id in (select * from (select id from customer
group by phoneNumber
having count(phoneNumber) > 1)t ) ;
答案 1 :(得分:0)
这是另一个试图避免陷阱的变体(我使用stmt的另一个组,见下文。它可能无法反映你想要做的事情):
update customer a
join ( select id
from customer b
group by id
having count(1) > 1
) b on a.id = b.id
set a.flag = 1;
你的陈述select id from customer group by phoneNumber
的一部分是另一个mysql的怪癖。在sql92中它是无效的,除非id在功能上依赖于phonenumber,否则它在sql99及更高版本中也是无效的。您可以通过在@@ sql_mode中设置ONLY_FULL_GROUP_BY来强制执行更严格的行为。