MySql如何在该表中基于组更新表?

时间:2014-06-19 18:45:15

标签: mysql sql sql-update

当我运行以下查询时,我得到了

  

错误#1093 - 您无法在FROM子句

中为更新指定目标表'x2'
update contacts 
set duplicates = 1
where email in ( select email from contacts 
    group by email
    having count(*) > 1)

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:0)

我认为你可以通过嵌套子查询来欺骗它。

update contacts set duplicates = 1 where email in 
(select email 
  from  
    (select email from contacts group by email having count(*) > 1) as a)

答案 1 :(得分:0)

您可以通过JOIN查找具有相同电子邮件和不同主键的其他行。

例如,假设您的主键名为id

UPDATE contacts AS c1
JOIN contacts AS c2 ON (c1.email = c2.email AND c1.id <> c2.id)
SET c1.duplicates = 1;