【发布时间】:2020-12-18 21:53:16
【问题描述】:
我有两个想要集成的 SQL 函数,能够通过单个 SQL 查询进行 prestashop 导出:
函数 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
这些 INNER JOINS 工作正常:
+------------+---------------+-------------+-----------+--------+-------------------+
| 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 查询来获得上述结果?
感谢任何帮助!
【问题讨论】:
-
谢谢@Parfait ...我已将标签和标题更新为 MySQL。
标签: mysql group-by inner-join prestashop