0

我需要从一个组织为 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       |
+--------------------+--------------------------+------------------------------+

关于如何获得所有产品结果(如关系表)的任何想法?

4

1 回答 1

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;
于 2013-08-12T15:29:17.510 回答