I have two SQL functions that I want to integrate in able to make a prestashop export with a single SQL query:
Function 1 joins data from different tables.
Function 2 converts multiple rows into a single row.
I am unable to have these functions work together... Let me describe the two functions.
FUNCTION 1
SELECT a.id_product, a.ean13, a.weight, b.id_product, b.name, c.id_product, c.id_tab, c.content
FROM ps_product AS a
INNER JOIN ps_product_lang AS b ON b.id_product = a.id_product
INNER JOIN ps_extraproducttab_product_lang AS c ON c.id_product = a.id_product
These INNER JOINS work fine:
+------------+---------------+-------------+-----------+--------+-------------------+
| id_product | ean13 | weight | name | id_tab | content |
+------------+---------------+-------------+-----------+--------+-------------------+
| 11 | 0000000000001 | 1000.000000 | product_A | 1 | some ingredients |
| 11 | 0000000000001 | 1000.000000 | product_A | 2 | some allergenes |
| 12 | 0000000000002 | 1500.000000 | product_B | 1 | other ingredients |
| 12 | 0000000000002 | 1500.000000 | product_B | 2 | other allergenes |
+------------+---------------+-------------+-----------+--------+-------------------+
But I want to convert c somehow. The second INNER JOIN uses a table that has multiple rows on a single key (id_product):
+--------+------------+---------+-------------------+
| id_Tab | id_product | id_lang | content |
+--------+------------+---------+-------------------+
| 1 | 11 | 1 | some ingredients |
| 2 | 11 | 1 | some allergenes |
| 1 | 12 | 1 | other ingredients |
| 2 | 12 | 1 | other allergenes |
+--------+------------+---------+-------------------+
I want to combine these rows first. Running this second function on table 'ps_extraproducttab_product_lang' does exactly that:
FUNCTION 2
SELECT t1.id_product, t1.content AS 'ingred', t2.content AS 'allerg'
FROM ps_extraproducttab_product_lang t1, ps_extraproducttab_product_lang t2
WHERE t1.id_product = t2.id_product
AND t1.id_Tab = '1'
AND t2.id_Tab = '2'
It outputs:
+------------+-------------------+------------------+
| id_product | ingred | allerg |
+------------+-------------------+------------------+
| 11 | some ingredients | some allergenes |
| 12 | other ingredients | other allergenes |
+------------+-------------------+------------------+
I used this source, privided by Akina: https://dba.stackexchange.com/questions/236692/combining-multiple-rows-into-a-single-row-with-multiple-columns ( I still need to find out how to extend this code to a 3th and 4th id_Tab, although that is not the topic of my current question )
I am unable to integrate the above in a single query that would result into:
+------------+---------------+-------------+-----------+-------------------+-------------------+
| id_product | ean13 | weight | name | ingred | allerg | |
+------------+---------------+-------------+-----------+-------------------+-------------------+
| 11 | 0000000000001 | 1000.000000 | product_A | some ingredients | some allergenes |
| 12 | 0000000000002 | 1500.000000 | product_B | other ingredients | other allergenes |
+------------+---------------+-------------+-----------+-------------------+-------------------+
How would you build a single SQL-query to get the above result?
Any help is appreciated!