【问题标题】:select one value if it exists, another if not如果存在则选择一个值,如果不存在则选择另一个
【发布时间】:2014-02-21 16:10:09
【问题描述】:

在此表中:

|value_id|entity_type_id|attribute_id|store_id|entity_id|value
|9289729 |4             |62          |0       |765985   |default value
|9289730 |4             |62          |1       |765985   |value in spanish
|9289731 |4             |62          |2       |765985   |value in dutch

如果不存在,我需要获取 store_id 的值(可以是 1、2 或 3)或默认值 (store_id = 0)。
它必须包含在由几个 INNER JOINS 组成的更大查询中,到目前为止看起来像这样:

SELECT DISTINCT `p2c`.`product_id`, `name_id`.`value` AS `name`, `short_description_id`.`value` AS `short_description` FROM `catalog_category_product` AS `p2c`
 INNER JOIN `catalog_product_entity_int` AS `status` ON p2c.product_id = status.entity_id AND status.value = 1
 INNER JOIN `catalog_product_entity_int` AS `visibility` ON p2c.product_id = visibility.entity_id AND visibility.value = 4
 INNER JOIN `catalog_product_entity_varchar` AS `name_id` ON p2c.product_id = name_id.entity_id AND name_id.attribute_id = 60
 INNER JOIN `catalog_product_entity_text` AS `short_description_id` ON p2c.product_id = short_description_id.entity_id AND short_description_id.attribute_id = 62
WHERE (category_id IN ('1224'))

所以我想要的是这样的:

SELECT DISTINCT `p2c`.`product_id`, `name_id`.`value` AS `name`, `short_description_id`.`value` AS `short_description` FROM `catalog_category_product` AS `p2c`
 INNER JOIN `catalog_product_entity_int` AS `status` ON p2c.product_id = status.entity_id AND status.value = 1
 INNER JOIN `catalog_product_entity_int` AS `visibility` ON p2c.product_id = visibility.entity_id AND visibility.value = 4
 INNER JOIN `catalog_product_entity_varchar` AS `name_id` ON p2c.product_id = name_id.entity_id AND name_id.attribute_id = 60
 INNER JOIN `catalog_product_entity_text` AS `short_description_id` ON p2c.product_id = short_description_id.entity_id 
 AND short_description_id.attribute_id = 62
//start of not real mysql code
 AND ((short_description_id.store_id = 3) IF THERE IS A VALUE FOR THIS STORE_ID
      OR (short_description_id.store_id = 0) OTHERWISE)
//end of not real mysql code
WHERE (category_id IN ('1224'))

哦,我正在使用 Zend 的 mysql,如果这很重要的话。
有什么想法吗?

【问题讨论】:

标签: mysql not-exists


【解决方案1】:

一种方法是左连接两次到 catalog_product_entity_text。一次用于 ID 0,另一次用于 ID 3,然后在您的选择中执行 COALESCE

SELECT
..
 COALESCE(`short_description_id`.`value` , `short_description_id_DEFAULT`.`value`) AS `short_description`
..
FROM 
...

     LEFT JOIN `catalog_product_entity_text` AS `short_description_id` 
     ON p2c.product_id = short_description_id.entity_id 
         AND short_description_id.attribute_id = 62
         AND (short_description_id.store_id = 3)

     LEFT JOIN `catalog_product_entity_text` AS `short_description_id_DEFAULT` 
     ON p2c.product_id = short_description_id.entity_id 
         AND short_description_id.attribute_id = 62
         AND (short_description_id.store_id = 0) 

【讨论】:

  • 谢谢你的回答康拉德,我今天早上有几张票要处理,然后我会尝试你的解决方案,我会回复你
猜你喜欢
  • 1970-01-01
  • 2020-01-01
  • 2019-03-05
  • 1970-01-01
  • 1970-01-01
  • 2018-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多