mysql - 如何在此 SELECT 语句的结果集中设置字段?
问题描述
所以我有一个 SQL 查询,我在 Magento 2 数据库上运行。它成功列出了“状态”为“已禁用”(2) 且库存数量大于 0 的所有项目。
select
`eav_attribute`.`attribute_id` AS `attribute_id`,
`catalog_product_entity_int`.`entity_id` AS `entity_id`,
`catalog_product_entity_int`.`value` AS `value`,
`eav_attribute`.`attribute_code` AS `attribute_code`,
`catalog_product_entity`.`sku` AS `sku`,
`catalog_product_entity`.`type_id` AS `type_id`,
`cataloginventory_stock_item`.`qty` AS `qty`
from
(((`eav_attribute`
join `catalog_product_entity_int` on ((`eav_attribute`.`attribute_id` = `catalog_product_entity_int`.`attribute_id`)))
join `catalog_product_entity` on ((`catalog_product_entity_int`.`entity_id` = `catalog_product_entity`.`entity_id`)))
join `cataloginventory_stock_item` on ((`catalog_product_entity_int`.`entity_id` = `cataloginventory_stock_item`.`product_id`)))
where
((`eav_attribute`.`attribute_code` = 'status') and
(`catalog_product_entity_int`.`value` = 2)) and
(`cataloginventory_stock_item`.`qty` > 0 )
它适用于选择结果集,并为我提供符合这两个标准的准确项目列表。我将如何修改它以将此结果集中的项目的“状态”设置为“启用”(1)。所以本质上,我只需要运行这些标准,然后为每个结果设置列catalog_product_entity_int
。value
改为 1 而不是 2。
解决方案
您可以使用update
:
update eav_attribute ea join
catalog_product_entity_int cpei
on ea.attribute_id = cpei.attribute_id join
catalog_product_entity cpe
on cpei.entity_id = cpe.entity_id join
cataloginventory_stock_item cisi
on cpei.entity_id = cisi.product_id
set cpei.value = 2
where ea.attribute_code = 'status' and
cpei.value = 2 and
ci.qty > 0 ;
请注意,我添加了表别名并删除了括号和反引号——因此查询更易于编写和阅读。
推荐阅读
- angular - 如何在 Ionic 4 Angular 中的标签页之间设置导航?
- javascript - Redux 消费者没有将更新的 props 传递给 React 组件
- python - 如何控制从模块中导入哪些对象
- c# - 聚合的自定义名称
- python - 为什么在训练过程中损失曲线有很大的跳跃(上升)?
- javascript - 从页面重新加载 DIV,而不更改页面
- python - 干净地停止 QML 应用程序而不引用重复计时器
- python - python2.7-dev 不会安装
- r - R中的igraph是否可以删除不满足某些条件的子图?
- c++ - C++ CMake 项目:目标“main_app”的配方失败