首页 > 解决方案 > SELECT 在 MySQL 中连接 N:N 关系

问题描述

我有这 3 个表,它们将产品及其类别置于 N:N 关系中:

products:
product_id | product_name
1          | Apples
2          | Shrimps
3          | Water

categories:
cat_id   | cat_name
1        | Vegetable
2        | Seafood
3        | Fresh
4        | Drinks
5        | Non-alcohol

products_to_categories:
product_id | cat_id
2          | 2
2          | 3
3          | 3
3          | 4
3          | 5
...

我想SELECT CONCAT...有这样的行:

product_id | product_name | product_categories
2            Shrimps        Seafood, Fresh
3            Water          Fresh, Drinks, Non-alcohol

有没有办法在 MySQL 中做到这一点?

标签: mysqlsqldatabaseselect

解决方案


推荐阅读