mySQL引用WHERE子句中的别名(EAV模型)

时间:2015-07-21 08:52:14

标签: mysql magento entity-attribute-value

我有以下mySQL查询,基本上搜索所有包含' rawganic'在名称中。

它看起来很乱,因为Magento使用的Entity-attribute-value模型非常令人困惑,因此我有很多选择查询来提取数据(我确信可能有更好的方法)。

SELECT entity_id As eID, value as PName, 
   (SELECT value from catalog_product_entity_varchar WHERE attribute_id = 152 and entity_id = eID) as Size, 
   (SELECT sku from catalog_product_entity WHERE entity_id = eID) as SKU,
   (SELECT type_id from catalog_product_entity WHERE entity_id = eID) as Type,
   (SELECT value from catalog_product_entity_decimal WHERE attribute_id = 75 and entity_id = eID) as RRP,
   (SELECT value from catalog_product_entity_int WHERE attribute_id = 96 and entity_id = eID) as Status 
FROM catalog_product_entity_varchar 
WHERE (attribute_id = 71 and value like '%rawganic%')

结果是:

Result

这很好,除了现在我想添加另一个WHERE子句,其中Type必须是' simple'状态为' 1'。

SELECT查询引用别名' eID'但是我不能在WHERE子句中使用它。如何添加这些额外的WHERE子句?

1 个答案:

答案 0 :(得分:1)

您可以使用HAVING:

SELECT entity_id As eID, value as PName, 
   (SELECT value from catalog_product_entity_varchar WHERE attribute_id = 152 and entity_id = eID) as Size, 
   (SELECT sku from catalog_product_entity WHERE entity_id = eID) as SKU,
   (SELECT type_id from catalog_product_entity WHERE entity_id = eID) as Type,
   (SELECT value from catalog_product_entity_decimal WHERE attribute_id = 75 and entity_id = eID) as RRP,
   (SELECT value from catalog_product_entity_int WHERE attribute_id = 96 and entity_id = eID) as Status 
FROM catalog_product_entity_varchar 
WHERE (attribute_id = 71 and value like '%rawganic%')
HAVING Type = 'Simple' AND Status = 1