连接3个引用表1的表,并在MYSQL中使用逗号分隔符组合值[重复]

k5ifujac  于 2023-02-18  发布在  Mysql
关注(0)|答案(2)|浏览(127)
    • 此问题在此处已有答案**:

SQL left join and group_concat returns duplicate data(1个答案)
MySQL - GROUP_CONCAT returns duplicate data, can't use DISTINCT(2个答案)
Eliminate duplicate rows inside GROUP_CONCAT(2个答案)
昨天关门了。
我在连接三个表时遇到问题。第一次尝试连接两个表是成功的,但在第三个表上结果不正确...
我有三个表计算机列表是 * 主表 *,然后适用的rpm适用的产品是 * 计算机列表 * 的一些详细信息
表:计算机列表

| id | machine_number | machine_brand |
---------------------------------------
| 1  |     MN-1       |     TOYO      |
| 2  |     MN-2       |    AMITA      |

表:适用_rpm

| id | mc_recordID | rpm |
--------------------------
| 1  |      1      | 20  |
| 2  |      2      | 20  |
| 3  |      2      | 25  |

表格:适用_产品

| id | mc_recordID | productline|
---------------------------------
| 1  |      1      |    mono    |
| 2  |      2      |    mono    |
| 3  |      2      |    poly    |

我想这样回去:

| machine_number |   rpm  |   twine    |
----------------------------------------
|      MN-1      | 20     | mono       |
|      MN-2      | 20, 25 | mono, poly |

我首先尝试使用以下查询连接这两个表:

SELECT t1.machine_number, GROUP_CONCAT(' ', t2.speed) machine_speed
FROM machine_list t1
INNER JOIN applicable_rpm t2 ON t1.id = t2.mc_recordID
GROUP BY t1.id;

其结果是:

| machine_number |  rpm   |
---------------------------
|      MN-1      |  20    |
|      MN-2      | 20, 25 |

这是正确的,但是当我尝试第三个表时,它重复了它的值。
这是我的疑问:

SELECT t1.machine_id,
       GROUP_CONCAT(' ', t2.speed) machine_speed,
       GROUP_CONCAT(' ', t3.twine) production_line
FROM machine_list t1
INNER JOIN applicable_rpm t2 ON t1.id = t2.mc_recordID
INNER JOIN applicable_product t3 ON t1.id = t3.mc_recordID
GROUP BY t1.id;

其结果是:

| machine_number |  rpm   |   twine    |
----------------------------------------
|      MN-1      | 20, 20 | mono, poly |
|      MN-2      | 20, 25 | mono, poly |

我该怎么办?

s4chpxco

s4chpxco1#

如果不进行分组,您将看到有两行与MN-2相关联。因此,如果使用group_concat,它将显示两行中所选列的值。

SELECT *
FROM machine_list t1
INNER JOIN applicable_rpm t2 ON t1.id = t2.mc_recordID
INNER JOIN applicable_product t3 ON t1.id = t3.mc_recordID;

你需要在这里使用嵌套选择,类似于下面的代码:

SELECT machine_number, 
    (SELECT GROUP_CONCAT(rpm) FROM applicable_rpm WHERE mc_recordID = t1.ID) as rpm,
    (SELECT GROUP_CONCAT(productline) FROM applicable_product WHERE mc_recordID = t1.ID) as twin,
    FROM machine_list t1;

作为一种补充,您还可以尝试使用GROUP_CONCAT和DISTINCT

SELECT t1.machine_id,
       GROUP_CONCAT(DISTINCT t2.speed) machine_speed,
       GROUP_CONCAT(DISTINCT t3.twine) production_line
FROM machine_list t1
INNER JOIN applicable_rpm t2 ON t1.id = t2.mc_recordID
INNER JOIN applicable_product t3 ON t1.id = t3.mc_recordID
GROUP BY t1.id;
moiiocjp

moiiocjp2#

您的联接似乎产生了重复行。
我们可以通过使用子查询来获得所需的输出。

SELECT t1.machine_number, t2.machine_speed, t3.production_line
FROM machine_list t1
LEFT JOIN (
  SELECT mc_recordID, GROUP_CONCAT(' ', speed) AS machine_speed
  FROM applicable_rpm
  GROUP BY mc_recordID
) t2 ON t1.id = t2.mc_recordID
LEFT JOIN (
  SELECT mc_recordID, GROUP_CONCAT(' ', twine) AS production_line
  FROM applicable_product
  GROUP BY mc_recordID
) t3 ON t1.id = t3.mc_recordID;

这将返回预期的输出:
| 机器_编号|机器_速度|生产线|
| - ------|- ------|- ------|
| MN-1|二十个|单声道|
| MN-2|二十、二十五|单、多|

相关问题