gpt4 book ai didi

MySQL DELETE 使用 Having 和 Count 的子查询

转载 作者:可可西里 更新时间:2023-11-01 07:45:34 28 4
gpt4 key购买 nike

我正在尝试使用以下查询删除多个条目:

首先,我使用此查询找到要删除的条目:

SELECT guid FROM account GROUP BY guid,type HAVING count(type) > 1);

然后我将此查询添加到 DELETE 语句中:

DELETE FROM account WHERE guid IN (SELECT guid FROM account GROUP BY guid,type HAVING count(type) > 1);

但是我得到这个错误:

您不能在 FROM 子句中指定要更新的目标表“account”

最佳答案

我认为您需要使用临时表来实现您的需求,如下所示:

  1. 第一步:创建临时表

    CREATE TEMPORARY TABLE MyTemp
    SELECT guid FROM account
    GROUP BY guid,type HAVING count(type) > 1;
  2. 在删除语句中使用临时表

    DELETE FROM account 
    WHERE guid IN (SELECT guid FROM MyTemp);
  3. 删除临时表

    DROP TEMPORARY TABLE MyTemp;

编辑:我认为使用 *两个嵌套表 也可以:

  DELETE FROM account 
WHERE guid IN
(SELECT guid FROM
(SELECT guid FROM account
GROUP BY guid,type HAVING count(type) > 1) as MyTemp
)

关于MySQL DELETE 使用 Having 和 Count 的子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4936838/

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