当等效的SELECT工作时,为什么这个DELETE查询失败?

时间:2012-05-12 23:58:37

标签: mysql sql mysql-error-1064

我有两个表,产品(product_id)和性别(product_id,sex_id)。我试图从两个具有product_id的表中删除行,以便在sex_id和sex = 1的性别表中存在一行。到目前为止我已经

CREATE TEMPORARY TABLE tempTable
      SELECT p.product_id  
        FROM products AS p
  INNER JOIN sex AS s
          ON p.product_id = s.product_id 
       WHERE s.sex = 1;

 DELETE FROM products AS p
NATURAL JOIN tempTable AS t

DELETE查询抛出此错误:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NATURAL JOIN tempTable AS t' at line 2

此SELECT查询正常工作:

SELECT * FROM products
NATURAL JOIN tempTable

为什么SELECT查询有效,但DELETE查询不起作用?

1 个答案:

答案 0 :(得分:5)

因为DELETE有一个different syntax。您必须指定要从哪个表中删除。

所以做一个

DELETE p, t FROM products AS p NATURAL JOIN tempTable AS t;
DELETE p FROM products AS p NATURAL JOIN tempTable AS t;
DELETE t FROM products AS p NATURAL JOIN tempTable AS t;