多对多表加入Pivot

时间:2011-05-24 16:43:56

标签: mysql many-to-many pivot

我目前有两个类似于usersprograms的表格,它们通过link表格通过多对多关系链接。

mysql> select * from users;
+----+----------+
| id | name     |
+----+----------+
|  1 | Jonathan |
|  2 | Little   |
|  3 | Annie    |
|  4 | Bob      |
+----+----------+
4 rows in set (0.00 sec)

mysql> select * from programs;
+----+----------------------+
| id | name                 |
+----+----------------------+
|  1 | Microsoft Word       |
|  2 | Microsoft Excel      |
|  3 | Microsoft PowerPoint |
+----+----------------------+
3 rows in set (0.00 sec)

mysql> select * from link;
+---------+------------+
| user_id | program_id |
+---------+------------+
|       1 |          1 |
|       1 |          2 |
|       1 |          3 |
|       2 |          2 |
|       3 |          1 |
|       3 |          4 |
+---------+------------+
6 rows in set (0.00 sec)

我理解如何连接表并返回这种结果:

mysql> select users.name, programs.name from linker
    -> join users on users.id = linker.user_id
    -> join programs on programs.id = linker.program_id;
+----------+----------------------+
| name     | name                 |
+----------+----------------------+
| Jonathan | Microsoft Word       |
| Jonathan | Microsoft Excel      |
| Jonathan | Microsoft PowerPoint |
| Little   | Microsoft Excel      |
| Annie    | Microsoft Word       |
+----------+----------------------+

但我真正想要的是更复杂一点:

+----------+-----------------------------------------------------+
| name     | name                                                |
+----------+-----------------------------------------------------+
| Jonathan | Microsoft Word,Microsoft Excel,Microsoft PowerPoint |
| Little   | Microsoft Excel                                     |
| Annie    | Microsoft Word                                      |
+----------+-----------------------------------------------------+

我假设某个地方有一个GROUP_CONCAT()投入命令,但我似乎无法保持结果看起来像这样:

mysql> select users.name, group_concat(programs.name) from linker
    -> join users on users.id = linker.user_id
    -> join programs on programs.id = linker.program_id;
+----------+------------------------------------------------------------------------------------+
| name     | group_concat(programs.name)                                                        |
+----------+------------------------------------------------------------------------------------+
| Jonathan | Microsoft Word,Microsoft Excel,Microsoft PowerPoint,Microsoft Excel,Microsoft Word |
+----------+------------------------------------------------------------------------------------+

有人能指出我正确的方向吗?

2 个答案:

答案 0 :(得分:10)

您需要指定DISTINCT,即

select users.name, group_concat( DISTINCT programs.name)

请参阅MySQL文档here

尝试将您的查询更改为:

SELECT users.name, group_concat(programs.name) 
from users
LEFT JOIN linker on linker.user_id = users.id
LEFT JOIN programs on linker.program_id = programs.id
GROUP BY users.id

对于没有与之关联的程序的用户,这将为您提供null。要过滤掉它们,只需添加WHERE programs.id IS NOT NULL

答案 1 :(得分:0)

SELECT users.name, group_concat(programs.name) from linker
INNER JOIN users on linker.user_id = users.id
INNER JOIN programs on linker.program_id = programs.id
GROUP BY users.id;