2017-03-20 44 views
2

我在我的數據庫中有以下表格,我想獲得獨特的零件號碼和他們的品牌名稱。 所有這些表格均基於EAV型號如何在mysql查詢中使用其品牌獲得唯一的零件號?

表1部件號(對於ex 8004)。

ENTITY_ID是唯一的約束

select * from catalog_product_entity_varchar where attribute_id = 961 and value = '8004'; 

+-----------+----------------+--------------+----------+-----------+-------+ | value_id | entity_type_id | attribute_id | store_id | entity_id | value | +-----------+----------------+--------------+----------+-----------+-------+ | 19507400 | 10 | 961 | 0 | 39214 | 8004 | | 19507401 | 10 | 961 | 0 | 281155 | 8004 | | 19507402 | 10 | 961 | 0 | 1926249 | 8004 | | 64825324 | 10 | 961 | 0 | 11892287 | 8004 | | 168417193 | 10 | 961 | 0 | 22721887 | 8004 | +-----------+----------------+--------------+----------+-----------+-------+

表2爲品牌代號 -

select * from catalog_product_entity_int where attribute_id = 953 and entity_id in (39214,281155,1926249,11892287,22721887); 

+----------+----------------+--------------+----------+-----------+-------+ | value_id | entity_type_id | attribute_id | store_id | entity_id | value | +----------+----------------+--------------+----------+-----------+-------+ | 5401700 | 10 | 953 | 0 | 39214 | 1050 | | 5401701 | 10 | 953 | 0 | 281155 | 1109 | | 5401702 | 10 | 953 | 0 | 1926249 | 1082 | | 21106883 | 10 | 953 | 0 | 11892287 | 1109 | | 87135500 | 10 | 953 | 0 | 22721887 | 1109 | +----------+----------------+--------------+----------+-----------+-------+

表3列出了基於品牌代碼的實際品牌名稱。

select * from eav_attribute_option_value where option_id in (1050,1109,1082); 

+----------+-----------+----------+------------------------+ | value_id | option_id | store_id | value | +----------+-----------+----------+------------------------+ | 15222 | 1050 | 0 | Samsung | | 13070 | 1082 | 0 | Whirlpool | | 15317 | 1109 | 0 | GE | +----------+-----------+----------+------------------------+

預期輸出 -

enter image description here

回答

2

我認爲這是你正在尋找

SELECT GROUP_CONCAT(b.entity_id SEPARATOR ',') as entity_ids,p.value as part_no,GROUP_CONCAT(b.value SEPARATOR ',') as brand_code, GROUP_CONCAT(bn.value SEPARATOR ',') as brand_name FROM catalog_product_entity_varchar p RIGHT JOIN catalog_product_entity_int b ON p.entity_id = b.entity_id LEFT JOIN eav_attribute_option_value bn ON b.value = bn.option_id WHERE p.attribute_id = '961' GROUP BY b.entity_type_id 
+0

沒有這種不正常的查詢。 entity_type_id和entity_id是不同的。 –

+0

將attribute_id作爲前兩個表中的外鍵? –

+0

不,entity_id在此處是唯一的 –

相關問題