【问题标题】:How to combine 2 MySQL-functions如何组合 2 个 MySQL 函数
【发布时间】: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


【解决方案1】:

如果在您的 Prestashop 平台上使用最新版本的 MySQL/MariaDB,请考虑多个 CTEs。请务必使用explicit joins(不像DBA SE 链接使用的那样隐含)并避免使用a, b, c table aliasing。将第 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(pre v8.0)或 MariaDB(pre 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

【讨论】:

  • 感谢您的加入!我在这两种解决方案上都遇到了错误:MySQL 说:#1060 - 列名“id_product”重复。这些 phpmyadmin 详细信息有帮助吗?服务器类型:MariaDB,服务器版本:10.3.27-MariaDB - MariaDB Server,协议版本:10,数据库客户端版本:libmysql - 5.6.43,PHP扩展:mysqli curl mbstring,PHP版本:7.3.6
  • 请再试一次。我在ew 结果集中多余地包含了id_product 的两个引用。
  • Parfait,像你这样的人让互联网变得有价值!谢谢,这行得通。我会研究你的答案,并加深对 MySQL 的理解。
  • 哈哈...很高兴听到并乐于提供帮助和快乐的编码!顺便说一句 - 从技术上讲,您使用的是 MySQL 的表亲 MariaDB(最初都是由同一个创建者,其女儿名为 My 和 Maria),因此 MariaDB 使用许多 MySQL 编码 API,例如 PHP 的 prestashop。所以一定要在你的 SQL 学习中使用 MariaDB 文档。
  • 可选:如果要输出 t# 为空的行,请将 '... ew INNER JOIN (SELECT ...' 更改为 '... ew LEFT JOIN (SELECT ...'。 id_Tab 为 NULL
猜你喜欢
  • 2012-07-09
  • 1970-01-01
  • 2014-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多