【问题标题】:mySQL refer to alias in WHERE clause (EAV Model)mySQL 在 WHERE 子句中引用别名(EAV 模型)
【发布时间】:2015-07-21 08:52:14
【问题描述】:

我有以下 mySQL 查询,它基本上搜索名称中包含“rawganic”的所有产品。

它看起来很乱,因为 Magento 使用的是相当混乱的实体-属性-值模型,所以我有很多选择查询来提取数据(我相信可能有更好的方法)。

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

结果是:

这很好,除了现在我想添加另一个 WHERE 子句,其中类型必须为“简单”且状态为“1”。

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

【问题讨论】:

    标签: mysql magento entity-attribute-value


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2012-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-04
      相关资源
      最近更新 更多