0

我有三张桌子:

"products" -         contains products
"location" -         store location
"product_orders" -   joins data from products and location 

SQL转储:

--
-- Table structure for table `location`
--

CREATE TABLE IF NOT EXISTS `location` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=77 ;

--
-- Dumping data for table `location`
--

INSERT INTO `location` (`id`, `name`) VALUES
(1, 'Miami'),
(2, 'Denver');

-- --------------------------------------------------------

--
-- Table structure for table `products`
--

CREATE TABLE IF NOT EXISTS `products` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `type` varchar(255) NOT NULL DEFAULT '',
  `product` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=6014552 ;

--
-- Dumping data for table `products`
--

INSERT INTO `products` (`id`, `type`, `product`) VALUES
(1, 'shirt', 'red shirt'),
(2, 'shirt', 'red shirt'),
(3, 'pants', 'blue pants'),
(4, 'pants', 'blue pants');

-- --------------------------------------------------------

--
-- Table structure for table `product_orders`
--

CREATE TABLE IF NOT EXISTS `product_orders` (
  `product_id` int(11) NOT NULL,
  `location_id` int(11) NOT NULL,
  `status` varchar(255) NOT NULL,
  PRIMARY KEY (`product_id`,`location_id`),
  KEY `ix_product_orders` (`location_id`),
  KEY `ix_product_orders_1` (`product_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `product_orders`
--

INSERT INTO `product_orders` (`product_id`, `location_id`, `status`) VALUES
(1, 1, 'sold'),
(2, 2, 'sold'),
(1, 2, 'available'),
(2, 1, 'sold');

如果我想从商店'miami 中选择所有已售出的产品-此查询有效:

SELECT * FROM `products`
INNER JOIN `product_orders`
ON `product_orders`.`product_id`= `products`.`id`
WHERE `product_orders`.`location_id` = '1'
AND
status = 'sold'

您将如何重写以从迈阿密商店中删除所有已售出的产品?我的查询不起作用:

 DELETE `products` FROM `products`
INNER JOIN `product_orders`
ON `product_orders`.`location_id` = `products`.`id`
WHERE `product_orders`.`location_id` = '2'
AND
status = 'sold'
4

3 回答 3

1
DELETE FROM products WHERE id IN (SELECT location_id FROM products_orders WHERE location = 'miami' AND status = 'sold');
于 2012-12-06T15:59:05.923 回答
1

试试这个,

DELETE `products` FROM `products` INNER JOIN `products_orders` 
ON `products_orders`.`location_id` = `products`.`id` 
WHERE `products_orders`.`location` = 'miami' 
AND
status = 'sold'

或者

DELETE prod FROM `products` AS prod INNER JOIN `products_orders` 
ON `products_orders`.`location_id` = `products`.`id` 
WHERE `products_orders`.`location` = 'miami' 
AND
status = 'sold'
于 2012-12-06T15:58:48.217 回答
1

我不确定您是如何在表中设置外键的。但是从最基本的假设来看,产品表保存产品数据,位置保存位置数据。在您的公用表产品订单中,您有一个按产品 id 指向产品表的外键,以及一个按位置与位置表的关系。

因此,为了支持我的评论,我发布了以下查询。这是您的查询,在您的查询中更改了我认为是错字的字段。:-) 就像泡沫堆说的那样,我可能会坚持不懈地这样做......

 DELETE FROM `products`
 INNER JOIN `products_orders` 
 ON `products_orders`.`product_id` = `products`.`id`
 WHERE `products_orders`.`location` = 'miami' 
 AND `products_orders`.status = 'sold'
 ;
于 2012-12-06T16:26:43.520 回答