MySQL连接两个表以生成带有逗号分隔值的列

2022-03-13 00:00:00 mysql inner-join

我有一个MySQL问题,标题可能有点混乱,但这里是:

我有两个表

    TABLE A           TABLE B
+-----+--------+   +-----+--------+
| id  | NumCol |   | id  | NumCol |
+-----+--------+   +-----+--------+
| 101 |  123   |   | 101 |        |
| 101 |  124   |   +-----+--------+  
+-----+--------+   

现在我想通过id将这两个人结合起来,因此我会得到如下输出

+-----+--------+
| id  | NumCol |
+-----+--------+
| 101 | 123,124|
+-----+--------+

有人知道做这件事的便捷方法吗?


解决方案

尝试此查询

SELECT tb.id,GROUP_CONCAT(ta.NumCol) AS NumCol FROM tableB AS tb
JOIN tableA AS ta ON ta.id=tb.id
GROUP BY tb.id

相关文章