2015-12-02 108 views
2

我需要从我的magento站点使用SQL获取产品和价格的列表。我有两个疑问。第一个允许我从表中选择entity_id和value,该值是价格。sql查询 - 比较后加入两个结果

SELECT DISTINCT `entity_id` , `value` FROM `catalog_product_entity_decimal` 

然后我有另一个查询使用实体ID选择名称。

SELECT var.value AS product_name, e.entity_id 
FROM catalog_product_entity e, 
    eav_attribute eav, 
    catalog_product_entity_varchar var 
WHERE e.entity_type_id = eav.entity_type_id 
    AND eav.attribute_code = 'name' 
    AND eav.attribute_id = var.attribute_id 
    AND var.entity_id = e.entity_id 

http://fishpig.co.uk/magento/tutorials/eav-database-structure/

我有结合这两个SQL查询的主要麻烦。所以我试图找到如何选择产品名称的方法,并根据其实体ID匹配将其与价格对照。

所以基本上比较一列与另一列并相应加入。

+0

如果你喜欢,可以考虑下面这个简单的两步骤:1.如果您尚未这样做,请提供适当的CREATE和INSERT语句(和/或sqlfiddle),以便我们可以更轻松地复制问题。 2.如果您尚未这样做,请提供与步骤1中提供的信息相对应的所需结果集。 – Strawberry

+0

我正在使用Magento,因此我不确定要创建哪些表。 @Strawberry我会更新我的盒子,提供更多信息 – Noob

回答

0

事情是这样的......但没有适当的DDL很难肯定的说...

SELECT DISTINCT x.entity_id 
       , x.value 
       , var.value product_name 
      FROM catalog_product_entity_decimal x 
      JOIN catalog_product_entity e 
      ON e.entity_id = x.entity_id 
      JOIN eav_attribute eav 
      ON eav.entity_type_id = e.entity_type_id 
      JOIN catalog_product_entity_varchar var 
      ON var.attribute_id = eav.attribute_id 
      WHERE eav.attribute_code = 'name' 
0

你可以试试这个:

SELECT DISTINCT `entity_id` , `value` FROM `catalog_product_entity_decimal` table1 
INNER JOIN (SELECT var.value AS product_name, e.entity_id 
FROM catalog_product_entity e, 
    eav_attribute eav, 
    catalog_product_entity_varchar var 
WHERE e.entity_type_id = eav.entity_type_id 
    AND eav.attribute_code = 'name' 
    AND eav.attribute_id = var.attribute_id 
    AND var.entity_id = e.entity_id) table2 

    ON table1.entity_id = table2.entity_id