0

我有两张桌子 testatestb.

mysql> select * from testa;
+------+-------+
| id   | dcac  |
+------+-------+
|    1 | hello |
|    2 | world |
+------+-------+
2 rows in set (0.00 sec)

mysql> select * from testb;
+------+------+
| a_id | x    |
+------+------+
|    1 | a    |
|    1 | b    |
|    1 | b    |
|    1 | c    |
|    2 | x    |
+------+------+
5 rows in set (0.00 sec)

如何添加一列 ( x_list) 使其成为stesta的逗号分隔列表xtestbtesta.idtestb.a_id

所以我期待的输出有点像这样 -

+------+-------+--------+
| id   | dcac  | x-list |
+------+-------+--------+
|    1 | hello | a,b,b,c|
|    2 | world | x      |
+------+-------+--------+

我尝试使用一些复杂join的语句,并查看了这个http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat(不太了解)

但我无法继续。我该怎么办?谢谢。

4

1 回答 1

2

尝试:

SELECT testa.id, testa.dcac, GROUP_CONCAT(testb.x)
FROM testb
INNER JOIN testa ON testb.a_id = testa.id
GROUP BY testb.a_id

你可以在这里看到结果:http ://sqlfiddle.com/#!2/28887/2/0

于 2013-10-15T16:15:27.460 回答