通过连接查询删除产品表和产品图像表

时间:2015-04-05 15:06:44

标签: mysql

我用过这个

 DELETE p.* , pm.* FROM product p  , productimage pm
 WHERE p.p_id = '1' and pm.p_id = '1';   

但没有结果

CREATE TABLE IF NOT EXISTS `product` (
      `p_id` int(10) NOT NULL AUTO_INCREMENT,
      `p_names` varchar(50) NOT NULL,
      `p_price` int(10) NOT NULL,
      `p_description` varchar(100) NOT NULL,
      PRIMARY KEY (`p_id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;


    CREATE TABLE IF NOT EXISTS `productimage` (
      `pimage_id` int(10) NOT NULL AUTO_INCREMENT,
      `img` varchar(100) NOT NULL,
      `p_id` int(10) NOT NULL,----------------------------foreign Key 
      PRIMARY KEY (`pimage_id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 

1 个答案:

答案 0 :(得分:0)

DELETE p, pm 
FROM product p
LEFT JOIN productimage pm ON p.p_id = pm.p_id
WHERE p.p_id = 1