2015-07-21 61 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