2014-02-23 69 views
1
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查詢結果?如何在UPDATE查詢中生成此SELECT查詢?

+2

你想要什麼?日期? – Fabio

+0

UPDATE accounts SET accounts.active = 0 – NeT32

+0

看看我的回答 – Fabio

回答

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

+0

工作完美,謝謝! – NeT32

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) 
+0

是的,但我收到此錯誤:「您不能指定目標表'帳戶'在FROM子句中更新」。 – NeT32

+0

@ NeT32這樣做,這會創建一個隱式臨時表,因此它不會被統計爲同一個表。但是,這是一種性能低下的解決方法......所以如果您絕對需要它,請使用它。 – CodeBird

+0

解決了,謝謝CodeBird! – NeT32

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 
) 
+0

解決了,謝謝法比奧! – NeT32

+0

@ NeT32如果我的答案解決了您的問題,您可能會考慮接受它,您還將幫助人們在將來與類似問題 – Fabio