I need to extract results from one table organized as EAV-feed repository. What I need is return results like a relational table. I'm using an schema like that:
Table Schema
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 |
So I'm trying to extract all values from all products for each idfeed. In this case I'm trying to get somethig like this:
Wanted results..
+----------------------+---------------------------+---------------------------+
| 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 |
+--------------------+--------------------------+------------------------------+
I've been trying with some coalescing sentences but all I'm getting only one row because the MAX function, or some NULL values instead..:
What I'm trying now..
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;
And obsiously this only returns the last line (last product)..
+----------------------+---------------------------+---------------------------+
| products_idproduct | field1 | field2 |
+--------------------+--------------------------+------------------------------+
| 3 | 3rd product first val | 3rd product second val |
+--------------------+--------------------------+------------------------------+
Any ideas about how can I get all product results like a relational table?
Some people advise me some joins needed here... This is the sentence I'm using now (need to get some performance stats:
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;