如何在这个select语句的结果集中设置字段?

ghhaqwfi  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(278)

所以我有一个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 从2变为1。

oaxa6hgo

oaxa6hgo1#

你可以用 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 ;

请注意,我添加了表别名,删除了括号和反勾号——因此查询更易于编写和读取。

相关问题