简体   繁体   中英

Many To Many Table Join With Pivot

I currently have two tables similar to users and programs that are linked through a many-to-many relationships by way of a link table.

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)

I understand how to join the tables and return a result of this sort:

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       |
+----------+----------------------+

But what I am really looking for is a little more complicated:

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

I assume there is a GROUP_CONCAT() thrown into the command somewhere, but I cannot seem to keep the results from looking like this:

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 |
+----------+------------------------------------------------------------------------------------+

Can anybody point me in the right direction?

You need to specify a DISTINCT , ie

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

See the MySQL docs here .

Try changing your query to:

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

This will give you a null for any user with no programs associated with them. To filter them out, just add a WHERE programs.id IS NOT NULL .

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM