我有两个SQL函数要集成,它们能够用一个SQL查询进行预存储导出:
函数1连接来自不同表的数据
函数2将多行转换为一行。
我无法让这些功能协同工作。。。让我来描述一下这两个功能。
功能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
这些内部联接工作良好:
+------------+---------------+-------------+-----------+--------+-------------------+
| 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 |
+------------+---------------+-------------+-----------+--------+-------------------+
但我想以某种方式转换c。第二个INNER JOIN使用一个在单个键(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 |
+--------+------------+---------+-------------------+
我想先把这些行合并起来。在表"ps_extraproducttab_product_lang"上运行第二个函数正是这样做的:
功能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'
它输出:
+------------+-------------------+------------------+
| id_product | ingred | allerg |
+------------+-------------------+------------------+
| 11 | some ingredients | some allergenes |
| 12 | other ingredients | other allergenes |
+------------+-------------------+------------------+
我使用了这个来源,由Akina提供特权:https://dba.stackexchange.com/questions/236692/combining-multiple-rows-into-a-single-row-with-multiple-columns(我仍然需要找出如何将此代码扩展到第3个和第4个id_Tab,尽管这不是我当前问题的主题(
我无法将以上内容集成到一个单独的查询中,该查询将导致:
+------------+---------------+-------------+-----------+-------------------+-------------------+
| 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 |
+------------+---------------+-------------+-----------+-------------------+-------------------+
您将如何构建一个SQL查询来获得上述结果?
感谢您的帮助!
如果在您的Prestashop平台上使用最新版本的MySQL/MariaDB,请考虑多个CTE。请确保使用显式联接(而不是DBASE链接使用的隐式联接(,并避免a、b、c表别名。将自联接扩展到第3类和第4类的ps_extraproducttab_product_lang
。
WITH ew AS
(SELECT p.id_product, p.ean13, p.weight, pl.name
FROM ps_product AS p
INNER JOIN ps_product_lang AS pl
ON p.id_product = pl.id_product
), ia AS
(SELECT t1.id_product, t1.content AS 'ingred', t2.content AS 'allerg'
, t3.content AS 'thirdcat', t4.content AS 'fourthcat'
FROM ps_extraproducttab_product_lang t1
INNER JOIN ps_extraproducttab_product_lang t2
ON t1.id_product = t2.id_product
AND t1.id_Tab = '1' AND t2.id_Tab = '2'
INNER JOIN ps_extraproducttab_product_lang t3
ON t1.id_product = t3.id_product AND t3.id_Tab = '3'
INNER JOIN ps_extraproducttab_product_lang t4
ON t1.id_product = t4.id_product AND t4.id_Tab = '4'
)
SELECT ew.id_product, ew.ean13, ew.weight, ew.name
, ia.ingred, ia.allerg, ia.thirdcat, ia.fourthcat
FROM ew
INNER JOIN ia
ON ew.id_product = ia.id_product
对于早期版本的MySQL(v8.0之前(或MariaDB(v10.2之前(,请使用子查询:
SELECT ew.id_product, ew.ean13, ew.weight, ew.name
, ia.ingred, ia.allerg, ia.thirdcat, ia.fourthcat
FROM
(SELECT p.id_product, p.ean13, p.weight, pl.name
FROM ps_product AS p
INNER JOIN ps_product_lang AS pl
ON p.id_product = pl.id_product
) ew
INNER JOIN
(SELECT t1.id_product, t1.content AS 'ingred', t2.content AS 'allerg'
, t3.content AS 'thirdcat', t4.content AS 'fourthcat'
FROM ps_extraproducttab_product_lang t1
INNER JOIN ps_extraproducttab_product_lang t2
ON t1.id_product = t2.id_product
AND t1.id_Tab = '1' AND t2.id_Tab = '2'
INNER JOIN ps_extraproducttab_product_lang t3
ON t1.id_product = t3.id_product AND t3.id_Tab = '3'
INNER JOIN ps_extraproducttab_product_lang t4
ON t1.id_product = t4.id_product AND t4.id_Tab = '4'
) ia
ON ew.id_product = ia.id_product