如何在UPDATE查询中进行此SELECT查询?

时间:2014-02-23 21:50:48

标签: mysql

SELECT accounts.id, accounts.email FROM accounts
INNER JOIN (SELECT email FROM accounts
GROUP BY email HAVING count(id) > 5) dup ON accounts.email = dup.email

如何仅UPDATE此查询的结果?

3 个答案:

答案 0 :(得分:1)

我认为您可以使用子查询实现此目的,如下所示

UPDATE account a
SET a.active = 0
WHERE a.id IN 
(
    SELECT accounts.id
    FROM accounts
    INNER JOIN
    (
        SELECT email 
        FROM accounts
        GROUP BY email 
        HAVING count(id) > 5
    ) dup 
    ON accounts.email = dup.email
)

答案 1 :(得分:0)

你的意思是这样的:

UPDATE accounts SET active='0' WHERE id IN ((SELECT accounts.id FROM accounts) AS a
INNER JOIN (SELECT email FROM accounts
GROUP BY email HAVING count(id) > 5) dup ON a.accounts.email = dup.email)

答案 2 :(得分:0)

尝试:

UPDATE accounts
INNER JOIN (
  SELECT email FROM accounts
  GROUP BY email HAVING count(id) > 5) dup 
ON accounts.email = dup.email
SET active = 0

演示:http://sqlfiddle.com/#!2/a1533/1