我有一个包含productId,颜色,大小和其他字段的表。我从帖子值创建了以下数组。我使用匹配字段product_id,color_id,size_id更新表行。但我需要删除不匹配的其他2行。我试过下面的查询。
DELETE FROM fc_product_quantities WHERE product_id=18 AND color_id NOT IN (9,1) AND size_id NOT IN (23,24)
但它没有像我预期的那样工作,也没有删除任何记录。有没有其他方法可以使用mysql和php删除不匹配的记录。
(
[0] => Array
(
[pid] => 18
[list] => 1
[color] => 9
[size] => 23
[qty] => 10
[price] => 522.22
[offerprice] => 250.00
[default] => 1
)
[1] => Array
(
[pid] => 18
[list] => 1
[color] => 1
[size] => 24
[qty] => 25
[price] => 145.22
[offerprice] => 222.22
[default] => 0
)
)
我的桌子是
CREATE TABLE IF NOT EXISTS `fc_product_quantities` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`product_id` int(11) NOT NULL DEFAULT '0',
`color_id` int(11) NOT NULL DEFAULT '0',
`size_id` int(11) NOT NULL DEFAULT '0',
`quantity` int(11) NOT NULL DEFAULT '0',
`qty_price` float(11,2) NOT NULL DEFAULT '0.00',
`offer_price` float(11,2) NOT NULL,
`default` tinyint(1) NOT NULL DEFAULT '0',
`sold` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
INSERT INTO `fc_product_quantities` (`id`, `product_id`, `color_id`, `size_id`, `quantity`, `qty_price`, `offer_price`, `default`, `sold`) VALUES
(1, 18, 9, 23, 10, 522.22, 250.00, 1, 0),
(2, 18, 9, 22, 25, 245.55, 354.22, 0, 0),
(3, 18, 1, 22, 74, 444.44, 777.77, 0, 0),
(4, 18, 1, 24, 25, 145.22, 222.22, 0, 0);
PHP代码
$totalSizesArr = array();
$totalColorsArr = array();
foreach ($quantities as $xKey => $xValue) {
$pid = $xValue['pid'];
$colorId = $xValue['color'];
$sizeId = $xValue['size'];
$qty = $xValue['qty'];
$qPrice = $xValue['price'];
$qOfferPrice = $xValue['offerprice'];
$qDefault = $xValue['default'];
$qtyDetails = $this->product_model->get_product_quantity($product_id,$colorId,$sizeId);
if($qtyDetails->num_rows > 0) {
$updateCondition = array(
'product_id'=>$product_id,
'color_id'=>$colorId,
'size_id'=>$sizeId
);
$updateValues = array(
'quantity'=>$qty,
'qty_price'=>$qPrice,
'offer_price'=>$qOfferPrice,
'default'=>$qDefault
);
$this->product_model->update_details(PRODUCT_QUANTITIES,$updateValues,$updateCondition);
echo $this->db->last_query();
echo '</br>';
} else {
$uCondition = array();
$uDatarr = array('product_id'=>$product_id,'color_id'=>$colorId,'size_id'=>$sizeId,'quantity'=>$qty,'qty_price'=>$qPrice,'offer_price'=>$qOfferPrice,'default'=>$qDefault);
$this->product_model->commonInsertUpdate(PRODUCT_QUANTITIES,'insert',$iuExcludeArr,$uDatarr,$uCondition);
echo $this->db->last_query();
echo '</br>';
}
$totalSizesArr[] = $sizeId;
$totalColorsArr[] = $colorId;
}
$totalSizesStr = implode(',', $totalSizesArr);
$totalColorsStr = implode(',', $totalColorsArr);
$this->product_model->quantityDelete(PRODUCT_QUANTITIES,$product_id,$totalColorsStr,$totalSizesStr);
我想删除id为2和3的行。这两个不匹配。
我知道一种解决方案是删除product_id 18的所有行并插入新行。但是,如果没有删除所有记录并插入新行,还有其他方法吗?
抱歉我的英文不好
答案 0 :(得分:2)
我不确定,但你可能想看看你的逻辑。你不是故意这样做:
DELETE FROM fc_product_quantities WHERE product_id=18 AND (color_id NOT IN (9,1) OR size_id NOT IN (23,24))
这只是一个猜测,因为所有的AND可能会让你没有任何东西要删除。
答案 1 :(得分:1)
您当前的查询会从删除中排除多行。
DELETE
FROM fc_product_quantities
WHERE product_id=18
AND color_id NOT IN (9,1)
AND size_id NOT IN (23,24)
示例有这样一行:
Array
(
[pid] => 18
[color] => 9
[size] => 50
)
由于您在color_id
中排除了值9,因此不会删除此行。
这将有效:
DELETE
FROM fc_product_quantities
WHERE product_id = 18
AND (
(color_id != 9 AND size_id != 23)
OR
(color_id != 1 AND size_id != 24)
)