1

我需要删除 2012 年之前创建的表 'people' 中属于酒店 id '1' 的记录,并且在相关表 'taskinstance' 中只有 1 条记录。这些表以这种方式相关:people.id = taskinstance.idClient。我想出了以下内容,这给了我一个语法错误。

DELETE people FROM taskinstance people 
INNER JOIN 
    (SELECT * 
     FROM taskinstance
    WHERE substring(people.dateCreated,1,4) < 2012 and people.idHotel = '1'  
     GROUP BY taskinstance.idClient 
     HAVING COUNT(*) = 1) taskinstance 
ON people.id = taskinstance.idClient

如果我只想查看记录,以下工作:

SELECT *
FROM people
INNER JOIN taskinstance ON people.id = taskinstance.idClient
WHERE substring(people.dateCreated,1,4) < 2012 and people.idHotel = '1'
GROUP BY people.id
HAVING COUNT(1) = 1

谢谢你提供的所有帮助。

4

1 回答 1

0

尝试这样的事情:

 DELETE FROM people WHERE people.id IN 
 (SELECT people.id
 FROM people
 INNER JOIN taskinstance ON people.id = taskinstance.idClient
 WHERE substring(people.dateCreated,1,4) < 2012 and people.idHotel = '1'
 GROUP BY people.id
 HAVING COUNT(*) = 1)

编辑:在我自己的数据库上测试,这个被拒绝,因为 WHERE 子句中的表与 DELETE 子句中的表相同。

或这个:

 DELETE people FROM people
 INNER JOIN (
    SELECT people.id
    FROM people
    INNER JOIN taskinstance ON people.id = taskinstance.idClient
    WHERE substring(people.dateCreated,1,4) < 2012 and people.idHotel = '1'
    GROUP BY taskinstance.idClient
    HAVING COUNT(*) = 1
    ) p
 ON p.id = people.id

编辑:我在我自己的数据库中测试了一个几乎相同的查询(仅使用我自己的表),并且查询中没有语法错误。

请发布您的语法错误的全文,以便我们可以追踪问题,特别是它说的部分,"... for the right syntax to use near 'something something something' on line X."something something部分是故障排除的最重要部分。例如,如果它说,"near '' on line 6"那么您几乎可以肯定没有在 1 周围转义单引号。

于 2013-02-28T23:49:48.690 回答