2015-07-21 53 views
0

我有以下mySQL查询,基本上搜索名称中包含'rawganic'的所有产品。mySQL引用WHERE子句中的别名(EAV模型)

它看起来很混乱,因为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子句,其中的类型必须是“简单”,状态为“1”。

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

回答

1

您可以使用具有:

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 
+0

哦,上帝,这是简单的 - 非常感谢你! – Lee