[英]query for join two column and get one row per left with column that have array of right table refrenced to left table row
hellow.I have a tablt product like this你好,我有一个这样的平板电脑产品
+-------+-------+
| id | name |
+-------+-------+
+-------+-------+
| 1 | shirt |
+-------+-------+
color table like below:
+-------+--------+
| id | color |
+-------+--------+
+-------+--------+
| 1 | red |
+-------+--------+
+-------+--------+
| 2 | yellow |
+-------+--------+
+-------+--------+
| 3 | black |
+-------+--------+
and shirt_color table:和shirt_color 表:
+------------+----------+
| product_id | color_id |
+------------+----------+
+------------+----------+
| 1 | 1 |
+------------+----------+
+------------+----------+
| 1 | 2 |
+------------+----------+
+------------+----------+
| 1 | 3 |
+------------+----------+
I want to write a query to get all products and its colors (but all colors in one array column) like this below :我想编写一个查询来获取所有产品及其颜色(但所有颜色都在一个数组列中),如下所示:
+-------+-------+---------------+
| pro_id| name | colors |
+-------+-------+---------------+
+------+-------+----------------+
| 1 | shirt |red,yellow,black|
+------+-------+----------------+
use GROUP_CONCAT:使用 GROUP_CONCAT:
select a.id as pro_id, a.name,
GROUP_CONCAT ( color ) as colors
from product a inner join shirt_color b on a.id = b.product_id
inner join color c on c.id = b.color_id
group by a.id, a.name
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.