sql查询 - 比较后加入两个结果

时间:2015-12-02 14:07:43

标签: mysql sql magento

我需要使用SQL从我的magento网站获取产品和价格列表。我有两个问题。第一个允许我从表中选择entity_id和value,值是价格。

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匹配将其与价格相对应。

所以基本上将一列与另一列进行比较并相应地加入它们。

2 个答案:

答案 0 :(得分: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' 

答案 1 :(得分: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