使用 Pivot 连接多对多表

Many To Many Table Join With Pivot(使用 Pivot 连接多对多表)
本文介绍了使用 Pivot 连接多对多表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我假设有一个 GROUP_CONCAT() 被抛出到某处的命令中,但我似乎无法阻止结果看起来像这样:

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?

推荐答案

需要指定一个DISTINCT,即

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

查看 MySQL 文档 这里.

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

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

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.

这篇关于使用 Pivot 连接多对多表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)