1

I've got two tables on my MySQL server (5.5.25). One table is holding product information the other table keeps information on stock levels for those products. I've not defined any foreign keys so far:

mysql> DESCRIBE product;
+---------------------+--------------+------+-----+---------+----------------+
| Field               | Type         | Null | Key | Default | Extra          |
+---------------------+--------------+------+-----+---------+----------------+
| id                  | int(11)      | NO   | PRI | NULL    | auto_increment |
| product_no          | varchar(45)  | YES  |     | NULL    |                |
| product_group       | int(11)      | NO   |     | NULL    |                |
+---------------------+--------------+------+-----+---------+----------------+

mysql> DESCRIBE product_stock;
+--------------+---------+------+-----+---------+----------------+
| Field        | Type    | Null | Key | Default | Extra          |
+--------------+---------+------+-----+---------+----------------+
| id           | int(11) | NO   | PRI | NULL    | auto_increment |
| product_id   | int(11) | NO   |     | NULL    |                |
| amnt_stocked | int(11) | NO   |     | NULL    |                |
+--------------+---------+------+-----+---------+----------------+

Now my job is to delete any product_stock entries that are a member of product_group 2. My syntax is:

DELETE PS FROM product_stock PS LEFT JOIN product P ON PS.product_id = P.id 
WHERE P.product_group = 2;

The problem is: MySQL won't delete any row. Because of performance problems that might occur I don't want to use a subquery. I found so many examples of left joins combined with delete statements and just don't get what's going wrong here....

4

1 回答 1

0
DELETE product_stock
FROM product_stock
INNER JOIN product  ON product_stock.product_id = product.id
WHERE product.product_group = 2;
于 2013-08-16T12:23:26.423 回答