选择一个值(如果存在),如果另一个不存在 [英] select one value if it exists, another if not

查看:80
本文介绍了选择一个值(如果存在),如果另一个不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此表中:

|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组成的较大查询中,到目前为止,看起来像这样:

I need to get the value for the store_id (that can be 1, 2 or 3) or the default value (store_id = 0) if it doesn't exist.
And it has to be included in a larger query composed of several INNER JOINS, which looks like this so far:

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'))

所以我想要的是这样的:

So what I want is something like:

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.
有什么想法吗?

Oh, and I'm using Zend's mysql, if that matters.
Any ideas?

推荐答案

一种方法是使两次连接进入catalog_product_entity_text.一次输入ID 0,另一次输入ID 3,然后选择一次COALESCE

One approach is to left join twice to catalog_product_entity_text. Once for ID 0 and another for ID 3 and then do a COALESCE in your select

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) 

这篇关于选择一个值(如果存在),如果另一个不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆