简体   繁体   English

查询连接两列并在每列左侧获得一行,其中列具有与左表行相关的右表数组

[英]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.

相关问题 MySQL JOIN查询-右表中的每一行,左表中的每一行,对包含的数据进行优先级排序 - MySQL JOIN Query - one row from right table for each row left table with prioritizing contained data 在MySQL中,如何获得带有左连接和两个右表行匹配两个单独值的行? - How to get a row with left join with two right table row matching two separate value in MySQL? LEFT JOIN仅返回右表的一行 - LEFT JOIN only returns one row from right table 在两个表的左联接中,选择左表中的所有记录,并从右表中仅选择与左表匹配的一行记录 - In Left Join of two table select all records from left table and select only one row record from right table that matches to left table 将表1中的一列左连接到表2中的两列 - Left Join One Column in Table 1 to Two Columns in Table 2 在单列表上进行LEFT JOIN(或类似操作)以获取每一行是否在其他表中 - LEFT JOIN (or similar) on a single-column table to get whether each row is in other table or not LEFT JOIN导致右表上一列的错误空记录 - LEFT JOIN resulting in erroneous null records for one column on right table 将AS table.column添加到LEFT JOIN查询 - Adding AS table.column to LEFT JOIN query 基于同一张表获取 3 列左连接 - Get 3 column left join based on same one table 加入MySQL表:在左表的一行中显示右表的所有结果 - Join MySQL Tables: Display All Results From Right Table In One Row Of Left Table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM