2

我有两个表,products(product_id) 和 sex(product_id,sex_id)。我正在尝试从具有 product_id 的两个表中删除行,以便在 sex 表中存在具有此 product_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 查询无效?

4

1 回答 1

5

因为DELETE不同的语法。您必须指定要从哪些表中删除。

所以做一个

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;
于 2012-05-13T00:02:00.610 回答