2

我正在尝试在mysql中执行这样的命令:

DELETE FROM product_description, product, product_to_store, url_alias WHERE
product_description.name LIKE '%|%' AND product.product_id = 
product_description.product_id AND product_to_store.product_id = 
product_description.product_id AND url_alias.query = 
CONCAT('product_id=',product_description.product_id)

但是它给了我一个错误:

#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 'WHERE product_description.name LIKE '%|%' AND product.product_id = product_descr' at line 1

当我将 'DELETE' 替换为 'SELECT *' 时,查询就像一个魅力。

根据mysql文档

目前,您不能在子查询中从表中删除并从同一个表中选择。

所以现在我不知道如何在不使用 PHP 的情况下执行这样的查询

已编辑

我的最终查询有效:

DELETE product_description, product, product_to_store, url_alias 
FROM product_description, product, product_to_store, url_alias 
  WHERE product_description.name LIKE '%|%' AND product.product_id = 
  product_description.product_id AND product_to_store.product_id = 
  product_description.product_id AND url_alias.query = 
  CONCAT('product_id=',product_description.product_id)

所以提示是在 DELETE 之后添加所有 4 个表名以使其工作,正如 JW 所写

4

2 回答 2

4

您需要指定删除应该发生的位置,在下面的示例中,记录将在表中删除product_description

DELETE product_description
FROM product_description, product, product_to_store, url_alias 
WHERE product_description.name LIKE '%|%' AND product.product_id = 
      product_description.product_id AND product_to_store.product_id = 
      product_description.product_id AND url_alias.query = 
      CONCAT('product_id=',product_description.product_id)

或使用ANSI SQL-92格式

DELETE  product_description
FROM    product_description
        INNER JOIN product 
            ON product.product_id = product_description.product_id
        INNER JOIN product_to_store 
            ON product_to_store.product_id = product_description.product_id
        INNER JOIN url_alias 
            ON url_alias.query = CONCAT('product_id=',product_description.product_id)
WHERE   product_description.name LIKE '%|%'
于 2013-01-11T09:18:22.060 回答
1

Delete syntax is:

DELETE FROM table_name [WHERE Clause]  

you can't give multiple table_names in FROM clause (without JOIN, and USING clause).

DELETE table_name FROM table_name
INNER JOIN ....

or

DELETE [LOW_PRIORITY] [QUICK] [IGNORE]
FROM tbl_name[.*] [, tbl_name[.*]] ...
USING table_references
[WHERE where_condition]

Reference: 13.2.2. DELETE Syntax
And Mr. JW already given you an answer

USING example

DELETE  
FROM    `product_description`, 
        `product`, 
        `product_to_store`, 
        `url_alias`

USING  `product_description`
        INNER JOIN `product` 
         ON `product`.`product_id` = `product_description`.`product_id`
        INNER JOIN `product_to_store` 
         ON `product_to_store`.`product_id` = `product_description`.`product_id`
        INNER JOIN `url_alias` 
         ON `url_alias`.`query` =
               CONCAT('product_id=',`product_description`.`product_id`)
WHERE   `product_description`.`name` LIKE '%|%'  

Side note: Always use (`) for user defined identifiers.

于 2013-01-11T09:25:30.523 回答