mysql - 如何转换此 SQL Select 查询以对结果集的表值执行更新?
问题描述
下面的查询正确地给了我一个启用的可配置项目的列表,其中所有相关的简单项目都被禁用。对于此列表中的可配置项目,我如何修改它以将“状态”属性值更新为“2”。(他们没有子项目,所以需要禁用)
我很感激我将表命名和引用的方法混合在一起。我对此很陌生,并且结合了不同解决方案的元素。
SELECT `mgic_catalog_product_entity`.`entity_id` FROM (((`mgic_eav_attribute`
join `mgic_catalog_product_entity_int` on ((`mgic_eav_attribute`.`attribute_id` = `mgic_catalog_product_entity_int`.`attribute_id`)))
join `mgic_catalog_product_entity` on ((`mgic_catalog_product_entity_int`.`entity_id` = `mgic_catalog_product_entity`.`entity_id`)))
join `mgic_cataloginventory_stock_item` on ((`mgic_catalog_product_entity_int`.`entity_id` = `mgic_cataloginventory_stock_item`.`product_id`)))
WHERE `mgic_catalog_product_entity`.`type_id` = 'configurable' AND ((`mgic_eav_attribute`.`attribute_code` = 'status') and
(`mgic_catalog_product_entity_int`.`value` = 2)) AND NOT EXISTS(
SELECT *
FROM mgic_catalog_product_super_link cpsl
INNER JOIN mgic_catalog_product_entity_int cpei ON cpei.entity_id = cpsl.product_id
WHERE
parent_id = `mgic_catalog_product_entity`.`entity_id`
AND cpei.attribute_id = 97
AND cpei.value = 1
);
解决方案
使用禁用的 simples 获取可配置项的 ID
由于status
属性将始终具有 id 97,您可以使用以下 SQL:
SELECT entity_id FROM `catalog_product_entity` cpe
WHERE `type_id` = 'configurable' AND NOT EXISTS(
SELECT *
FROM catalog_product_super_link cpsl
INNER JOIN catalog_product_entity_int cpei ON cpei.entity_id = cpsl.product_id
WHERE
parent_id = cpe.entity_id
AND cpei.attribute_id = 97
AND cpei.value = 1
);
按 ID 禁用产品
要回答您更新的问题,我建议您获取所有实体 ID,然后使用 Magento 模型更改每个产品的状态,如此处建议的https://magento.stackexchange.com/questions/152263/how-to-disable-enable- a-product-programatically-in-magento2。例如:
<?php
class Example
{
/**
* @var \Magento\Catalog\Model\ProductRepository
*/
protected $productRepository;
/**
* @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
*/
protected $productCollectionFactory;
/**
* @param \Magento\Catalog\Model\ProductRepository $productRepository
*/
public function __construct(
\Magento\Catalog\Model\ProductRepository $productRepository,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
) {
$this->productRepository = $productRepository;
$this->productCollectionFactory = $productCollectionFactory;
}
/**
* Disable all products by IDs
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function disableProducts($productIds)
{
$productCollection = $this->productCollectionFactory()->create();
$productCollection->addAttributeToFilter('type_id', array('eq' => 'configurable'))
->setPageSize(999);
$productCollection->getSelect()
->where('NOT EXISTS(
SELECT *
FROM catalog_product_super_link cpsl
INNER JOIN catalog_product_entity_int cpei ON cpei.entity_id = cpsl.product_id
WHERE
parent_id = e.entity_id
AND cpei.attribute_id = 97
AND cpei.value = 1
)');
foreach ($productCollection as $p) {
$product = $this->productRepository->getById(
$p->getId(),
true /* edit mode */,
0 /* global store*/,
true/* force reload*/
);
$product->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED);
$this->productRepository->save($product);
}
}
}
推荐阅读
- javascript - jQuery MS Edge:trigger('blur') 为箭头、退格和删除保持焦点
- php - PHP Curl 无法发布数据
- c# - 是否可以在 ASP.NET MVC 中的 Selenium 中获得 AJAX 响应?
- javascript - 有没有办法确定我是在本地工作还是使用 Javascript 在互联网上工作
- javascript - 如何在普通 JS 中使用 SmoothDnD 库?
- javascript - 如何使用 REST API 链接来自 django 和 extjs 字段的字段值
- css - 如何在以下定义网格行模板:重复css网格拼贴布局
- javascript - 工具提示显示内容
- android - PopupWindow 在 Android API 28 的自定义键盘上被剪辑
- javascript - 更新到 6.3.3 后,我进入 pipe() 错误 TS2557: Expected at least 0 arguments, but got 1 or more。