带SELECT的Mysql DELETE(?)

时间:2012-01-05 12:03:47

标签: mysql sql

我有两张桌子,我有点困惑......

      |products|                          
________________________
|  id  | products_name |
|______________________|
|   1  |  iphone4      |
|   2  |  htc desire   |
    etc.............


     |special_price|
________________________
|  id  | products_price|
|______________________|
|   1  |  400          |
|   2  |  500          |
    etc.............

我想做的就是:

DELETE (ALL) FROM special_price WHERE products_name='iphone4'

这怎么可能发生?

2 个答案:

答案 0 :(得分:4)

试试这个:

DELETE sp FROM special_price sp
INNER JOIN products p ON p.id = sp.id
WHERE p.products_name = "iphone4";

答案 1 :(得分:4)

您需要找到该product_name的ID,请尝试:

IN :(适用于复杂条件)

DELETE FROM special_price 
WHERE id IN ( SELECT id FROM products WHERE products_name = 'iphone4' )