我需要从组织为EAV-feed存储库的一个表中提取结果。 我需要的是返回结果,如关系表。我正在使用这样的架构:
表架构
meta table
-----------------------
idmeta | entity_identity(fk) | products_idproduct(fk) | products_feeds_idfeed(fk) | value |
1 | 1 | 1 | 1 | First product first val |
2 | 2 | 1 | 1 | First product second val|
3 | 1 | 2 | 1 | 2nd product first val |
4 | 2 | 2 | 1 | 2nd product second val |
5 | 1 | 3 | 1 | 3rd product first val |
6 | 2 | 3 | 1 | 3rd product second val |
所以我试图从每个idfeed的所有产品中提取所有值。在这种情况下,我试图得到这样的东西:
通缉结果..
+----------------------+---------------------------+---------------------------+
| products_idproduct | field1 | field2 |
+--------------------+--------------------------+------------------------------+
| 1 | First product first val | First product second val |
+--------------------+--------------------------+------------------------------+
| 2 | 2nd product first val | 2nd product second val |
+--------------------+--------------------------+------------------------------+
| 3 | 3rd product first val | 3rd product second val |
+--------------------+--------------------------+------------------------------+
我一直尝试使用一些合并句子但是我只得到只有一行因为MAX函数,或者是一些NULL值而不是......:
我现在正在尝试...
SELECT DISTINCT products_idproduct
, MAX( IF(entity_identity = 1, value, NULL) ) as 'field1'
, MAX( IF(entity_identity = 2, value, NULL) ) as 'field2'
FROM meta
WHERE products_feeds_idfeed = 1;
然而,这只会返回最后一行(最后一个产品)..
+----------------------+---------------------------+---------------------------+
| products_idproduct | field1 | field2 |
+--------------------+--------------------------+------------------------------+
| 3 | 3rd product first val | 3rd product second val |
+--------------------+--------------------------+------------------------------+
关于如何获得所有产品结果的任何想法,如关系表?
答案 0 :(得分:0)
有些人建议我在这里需要一些联接...这是我现在正在使用的句子(需要获得一些性能统计数据:
SELECT PROD1.products_idproduct
, PROD1.value as 'field1'
, PROD2.value as 'field2'
, PROD3.value as 'field3'
, PROD4.value as 'field4'
FROM meta PROD1 LEFT JOIN meta PROD2
ON ( PROD1.products_feeds_idfeed = PROD2.products_feeds_idfeed
AND PROD1.products_idproduct = PROD2.products_idproduct)
LEFT JOIN meta PROD3
ON ( PROD1.products_feeds_idfeed = PROD3.products_feeds_idfeed
AND PROD1.products_idproduct = PROD3.products_idproduct)
LEFT JOIN meta PROD4
ON ( PROD1.products_feeds_idfeed = PROD4.products_feeds_idfeed
AND PROD1.products_idproduct = PROD4.products_idproduct)
WHERE
PROD1.products_feeds_idfeed = 1
AND PROD1.entity_identity = 1
AND PROD2.entity_identity = 2
AND PROD3.entity_identity = 3
AND PROD3.entity_identity = 4;