gpt4 book ai didi

sql - 使用重复条目更新行

转载 作者:行者123 更新时间:2023-12-01 00:32:46 25 4
gpt4 key购买 nike

我的情况和this other question一样,但我不想选择行,我想更新这些行。

我使用了 solution Scott Saunders made :

select * from table where email in (
select email from table group by email having count(*) > 1
)

那行得通,但我想更改/更新这些条目中的行值,所以我尝试了:

UPDATE `members` SET `banned` = "1" WHERE `ip` IN (
SELECT `ip` FROM `members` GROUP BY `ip` HAVING COUNT(*) > 1
)

但是我得到这个错误:

You can't specify target table 'members' for update in FROM clause

最佳答案

使用中间子查询来解决 1093 错误:

UPDATE `members` 
SET `banned` = '1'
WHERE `ip` IN (SELECT x.ip
FROM (SELECT `ip`
FROM `members`
GROUP BY `ip`
HAVING COUNT(*) > 1) x)

否则,在派生表上使用 JOIN:

UPDATE MEMBERS 
JOIN (SELECT `ip`
FROM `members`
GROUP BY `ip`
HAVING COUNT(*) > 1) x ON x.ip = MEMBERS.ip
SET banned = '1'

关于sql - 使用重复条目更新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3742916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com