MySQL combines multiple rows of results into one row with commas

Time:2021-9-14

explain:

1.GROUP_ The value in concat () is the field name of the data you want to merge;

 The separator function is used to separate the data to be merged. It is separated by commas by default;

 'is the symbol you want to use to separate;

2. Group by statement must be used for group management, otherwise all data will be merged into one record

Normal query:

SELECT a.order_id,goods_name
FROM A AS a
LEFT JOIN B AS b ON a.order_id = b.order_id

Query results:

MySQL combines multiple rows of results into one row with commas

Desired results:

MySQL combines multiple rows of results into one row with commas

SQL implementation:

SELECT a.order_id,GROUP_CONCAT( goods_name SEPARATOR ',' ) AS goods_name 
FROM A AS a
LEFT JOIN B AS c ON a.order_id = c.order_id
GROUP BY a.order_id