当前位置: 首页 > 知识库问答 >
问题:

无法删除具有GROUP BY和HAVING子句的语句

洪增
2023-03-14

我试图通过下面的SQL语句删除一些记录,但得到错误#1064-您的SQL语法中有错误;查看与您的MySQL服务器版本相对应的手册,了解在第12行“group BY tsa_.task_attachment_id h”附近使用的正确语法:

有没有人知道我怎么解决错误?

DELETE
    ta
FROM `tasks_recursive` AS tr
INNER JOIN `tasks_attachments` AS tsa ON tsa.task_id = tr.id
INNER JOIN `task_attachments` AS ta ON tsa.task_attachment_id = ta.id AND ta.creator_id = 279
INNER JOIN (
    `tasks_attachments` AS tsa_
    INNER JOIN `tasks_recursive` AS tr_ ON tr_.id = tsa_.task_id
) ON tsa_.task_attachment_id = tsa.task_attachment_id
WHERE
    ta.file_name IN ( '000531994879c3bf.pdf', '000531994879c5a8.pdf' )
GROUP BY
    tsa_.task_attachment_id
HAVING
    COUNT( DISTINCT( tr_.task_id ) ) = 1

我还尝试了下面的句子,在那里我得到了这个错误#1064-您的SQL语法中有一个错误;查看与您的MySQL服务器版本相对应的手册,了解在“group BY tsa_.task_attachment_id HAVING COUNT(DISTINCT(tr_.task_id'at line 12)附近使用的正确语法:

DELETE
    ta
FROM `tasks_recursive` AS tr
INNER JOIN `tasks_attachments` AS tsa ON tsa.task_id = tr.id
INNER JOIN `task_attachments` AS ta ON tsa.task_attachment_id = ta.id AND ta.creator_id = 279
INNER JOIN (
    `tasks_attachments` AS tsa_
    INNER JOIN `tasks_recursive` AS tr_ ON tr_.id = tsa_.task_id
) ON tsa_.task_attachment_id = tsa.task_attachment_id
WHERE
    ta.file_name IN ( '000531994879c3bf.pdf', '000531994879c5a8.pdf' )
GROUP BY
    tsa_.task_attachment_id
HAVING
    COUNT( DISTINCT( tr_.task_id ) ) = 1

上面的句子在many_to_manytasks_attachments表中查找nobody ecxept本身未使用的task_attachments行,该表将从属task_attachments表链接到主tasks_recursive表。

为了解决上述问题,我尝试包装上面的查询但我得到以下错误#1093-您不能在FROM子句中指定目标表“TA_”以进行更新:

DELETE ta_
                FROM `task_attachments` AS ta_
                WHERE ta_.id IN
(
                    SELECT
                        ta.id
                    FROM `tasks_recursive` AS tr
                    INNER JOIN `tasks_attachments` AS tsa ON tsa.task_id = tr.id
                    INNER JOIN `task_attachments` AS ta ON tsa.task_attachment_id = ta.id AND ta.creator_id = 279
                    INNER JOIN (
                        `tasks_attachments` AS tsa_
                        INNER JOIN `tasks_recursive` AS tr_ ON tr_.id = tsa_.task_id
                    ) ON tsa_.task_attachment_id = tsa.task_attachment_id
                    WHERE
                        ta.file_name IN ( '000531994879c3bf.pdf', '000531994879c5a8.pdf' )
                    GROUP BY
                        tsa_.task_attachment_id
                    HAVING
                        COUNT( DISTINCT( tr_.task_id ) ) = 1
                )

最后尝试了双重包装,但也不起作用,这是错误#1064-您的SQL语法中有一个错误;查看与您的MySQL服务器版本相对应的手册,了解第25行“as sub)”附近使用的正确语法:

DELETE
FROM `task_attachments`
WHERE `task_attachments`.id IN
(
SELECT
ta_.id
FROM `task_attachments` AS ta_
WHERE ta_.id IN 
(
    SELECT
        ta.id AS taId
    FROM `tasks_recursive` AS tr
    INNER JOIN `tasks_attachments` AS tsa ON tsa.task_id = tr.id
    INNER JOIN `task_attachments` AS ta ON tsa.task_attachment_id = ta.id AND ta.creator_id = 279
    INNER JOIN (
        `tasks_attachments` AS tsa_
        INNER JOIN `tasks_recursive` AS tr_ ON tr_.id = tsa_.task_id
    ) ON tsa_.task_attachment_id = tsa.task_attachment_id
    WHERE
        ta.file_name IN ( '000531994879c3bf.pdf', '000531994879c5a8.pdf' )
    GROUP BY
        tsa_.task_attachment_id
    HAVING
        COUNT( DISTINCT( tr_.task_id ) ) = 1
    ) AS sub
)

共有2个答案

连成益
2023-03-14

试试看

DELETE
FROM `task_attachments`
WHERE `task_attachments`.id IN
(
SELECT
taId
FROM 
(
    SELECT
        ta.id AS taId
    FROM `tasks_recursive` AS tr
    INNER JOIN `tasks_attachments` AS tsa ON tsa.task_id = tr.id
    INNER JOIN `task_attachments` AS ta ON tsa.task_attachment_id = ta.id AND ta.creator_id = 279
    INNER JOIN (
        `tasks_attachments` AS tsa_
        INNER JOIN `tasks_recursive` AS tr_ ON tr_.id = tsa_.task_id
    ) ON tsa_.task_attachment_id = tsa.task_attachment_id
    WHERE
        ta.file_name IN ( '000531994879c3bf.pdf', '000531994879c5a8.pdf' )
    GROUP BY
        tsa_.task_attachment_id
    HAVING
        COUNT( DISTINCT( tr_.task_id ) ) = 1
    ) AS sub
)
杨雪松
2023-03-14

没错,MySQL的DELETE支持从SELECT语法中借用的几个子句,如JOIN和LIMIT,但不支持所有子句。不支持GROUP BY和HAVING。

当我读到你的问题时,它似乎比你所做的要简单。您确实希望删除只属于一个task_id的task_attachments。并且对文件名和创建者ID有一些条件。

您可以为此使用排除联接。也就是说,尝试连接到引用相同附件id的不同任务,如果没有找到,则外部连接将返回NULL。然后在WHERE子句中测试NULL,当这种情况发生时,您发现了一个仅由一个task_id引用的附件。

DELETE 
    ta
FROM task_attachments AS ta
INNER JOIN tasks_attachments AS tsa1 
    ON tsa1.task_attachment_id = ta.id
LEFT OUTER JOIN tasks_attachments AS tsa2 
    ON tsa1.task_attachment_id = tsa2.task_attachment_id
    AND tsa1.task_id <> tsa2.task_id 
WHERE
    ta.file_name IN ( '000531994879c3bf.pdf', '000531994879c5a8.pdf' )
    AND ta.creator_id = 279
    AND tsa2.task_attachment_id IS NULL;

我认为在这个查询中没有引用task_recursive的必要,因为task_id已经存在于tasks_attachments中。

在你删除任何东西之前,它当然会更好的尝试作为一个选择第一,以双重检查我的重新制定给你你需要的。

SELECT 
    ta.*
FROM task_attachments AS ta
INNER JOIN tasks_attachments AS tsa1 
    ON tsa1.task_attachment_id = ta.id
LEFT OUTER JOIN tasks_attachments AS tsa2 
    ON tsa1.task_attachment_id = tsa2.task_attachment_id
    AND tsa1.task_id <> tsa2.task_id 
WHERE
    ta.file_name IN ( '000531994879c3bf.pdf', '000531994879c5a8.pdf' )
    AND ta.creator_id = 279
    AND tsa2.task_attachment_id IS NULL;
 类似资料:
  • 问题内容: select SUM (Bill) from ProductSaleReport group by PCI having MONTH(Date) between 1 and 3 有人可以帮我发现问题吗? 我得到了错误: 消息8121,级别16,状态1,行1 列“ ProductSaleReport.Date”在HAVING子句中无效,因为它既不包含在聚合函数中也不在GROUP BY子句

  • 问题内容: 我一直在通过一些sql语法来学习oracle sql考试,但发现有些令人困惑 根据官方参考,选择语法如下: 基于此,您不能在GROUP BY子句之前有HAVING子句。但是,如果我要在测试服务器中执行以下sql: 它不会产生语法错误,有人可以帮忙解释一下吗?我不认为参考文档是错误的,但是如果是这样,我需要一些确认。 问题答案: 如前所述这里: 使用HAVING子句将返回的行的组限制为指

  • 主要内容:语法,实例HAVING 子句允许指定条件来过滤将出现在最终结果中的分组结果。 WHERE 子句在所选列上设置条件,而 HAVING 子句则在由 GROUP BY 子句创建的分组上设置条件。 语法 下面是 HAVING 子句在 SELECT 查询中的位置: 在一个查询中,HAVING 子句必须放在 GROUP BY 子句之后,必须放在 ORDER BY 子句之前。下面是包含 HAVING 子句的 SELECT

  • 主要内容:Oracle HAVING子句简介,Oracle HAVING子句的例子在本教程中将学习如何使用Oracle 子句过滤由子句返回分组。 Oracle HAVING子句简介 子句是SELECT语句的可选子句。它用于过滤由GROUP BY子句返回的行分组。 这就是为什么子句通常与子句一起使用的原因。 以下说明了Oracle子句的语法: 在这个语句中,子句紧跟在子句之后。如果使用不带子句的子句,则子句将像WHERE子句那样工作。 请注意,子句过滤分组的行,而子句过滤行。这是

  • HAVING 子句 在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与聚合函数一起使用。 HAVING 子句可以让我们筛选分组后的各组数据。 SQL HAVING 语法SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP

  • 我正在运行此查询, 这是您在图片中看到的id为19的唯一删除记录。 id的列类型是整数。