2015-01-08 19 views
1

下面的查詢讓我ENTITY_ID和name列SQL:無法連接這兩個表(EAV Architechture,Magento的)

SELECT e.entity_id, eav.value AS name 
FROM catalog_product_entity e 
JOIN catalog_product_entity_varchar eav 
    ON e.entity_id = eav.entity_id 
JOIN eav_attribute ea 
    ON eav.attribute_id = ea.attribute_id 
    WHERE ea.attribute_code = 'name' 

下面的查詢讓我ENTITY_ID和價格列

SELECT e.entity_id, eav.value AS price 
FROM catalog_product_entity e 
JOIN catalog_product_entity_decimal eav 
    ON e.entity_id = eav.entity_id 
JOIN eav_attribute ea 
    ON eav.attribute_id = ea.attribute_id 
    WHERE ea.attribute_code = 'price' 

我無法加入這兩個,一次獲得entity_id,名稱和價格列,有人可以幫我弄清楚這一點嗎?

回答

1

嘗試:

SELECT e.entity_id, eav.value AS name, eav2.value as price 
    FROM catalog_product_entity e 
    JOIN catalog_product_entity_varchar eav 
    ON e.entity_id = eav.entity_id 
    JOIN eav_attribute ea 
    ON eav.attribute_id = ea.attribute_id 
    JOIN catalog_product_entity_decimal eav2 
    ON e.entity_id = eav2.entity_id 
    JOIN eav_attribute ea2 
    ON eav2.attribute_id = ea2.attribute_id 
WHERE ea.attribute_code = 'name' 
    and ea2.attribute_code = 'price' 
+0

謝謝你這個工作,我嘗試使用where子句以不同的方式,所以我無法弄清楚..現在我知道了。 – brucekaushik

+0

請注意,內部聯接意味着這將返回具有「name」屬性和「price」屬性的實體的行。如果缺少這些屬性中的任何一個,則該實體的一行不會被返回。 (這不一定是個問題;實際上取決於規範......但是有關EAV模型的一件事情是它通常允許某些屬性值「丟失」。 – spencer7593

+0

值得注意的是@brucekaushik if it如果某些東西可能沒有這兩個屬性的值,請確保也將條件從WHERE子句移動到外部連接的ON子句中,否則行仍將被過濾掉。 –