mysql无法在FROM子句中指定要更新的目标表

时间:2017-02-10 06:22:13

标签: mysql

我必须检查一些不需要的记录并将其删除,因为我使用了以下查询

delete from contain_tab 
where org_id = "RTY" 
      AND id_contain 
           NOT IN (
               select id_contain from(
                  SELECT id_contain FROM contain_tab c 
                  WHERE org_id = "RTY" 
                  AND (c.cu_date,c.cv_date) 
                  in(
                       select max(cu_date),max(cv_date) from contain_tab cs 
                       where org_id = "RTY" 
                       AND cs.container = c.container 
                       and cs.mfest_id = c.mfest_id 
                       and cs.sl_number = c.sl_number)
                     ) 
              as id_contain)

结构

CREATE TABLE `contain_tab` (
`id_contain` int(11) NOT NULL,
`org_id` varchar(5) NOT NULL,
`container` varchar(24) DEFAULT NULL,
`eta_dest` date DEFAULT NULL,
`mfest_id` varchar(30) DEFAULT NULL,
`sl_number` varchar(30) DEFAULT NULL,
`orig_id` varchar(8) DEFAULT NULL,
`cu_date` date DEFAULT NULL,
`cv_date` date DEFAULT NULL,
`vehicle` varchar(32) DEFAULT NULL,
`user_defined_field1` varchar(35) DEFAULT NULL,
`user_defined_field2` varchar(35) DEFAULT NULL
`created_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP) ENGINE=InnoDB DEFAULT CHARSET=utf8;

我收到了错误 MySQL说:文档

1093 - 您无法在FROM子句

中为更新指定目标表'contains'

请帮我修复错误

2 个答案:

答案 0 :(得分:0)

为您的表名contain提供反引号,因为它是关键字

`contain`

答案 1 :(得分:0)

尝试使用反引号,因为您的名字包含保留关键字。我冒昧地格式化你的查询。

查看MySQL reserved keywords并尝试在数据库/表/列的名称中避免使用它们。

delete from `contain_tab`
where `org_id` = "RTY"
      AND `id_contain`
          NOT IN (
            select `id_contain`
            from(
                  SELECT `id_contain`
                  FROM `contain_tab` c
                  WHERE `org_id` = "RTY"
                        AND (c.`cu_date`, c.`cv_date`)
                            in (
                              select max(`cu_date`), max(`cv_date`)
                              from `contain_tab` cs
                              where `org_id` = "RTY"
                                    AND cs.`container` = c.`container`
                                    and cs.`mfest_id` = c.`mfest_id`
                                    and cs.`sl_number` = c.`sl_number`
                            )
                ) as "id_contain"
          );