Can I concatenate multiple MySQL rows into one field?
You can use GROUP_CONCAT
:
SELECT person_id,
GROUP_CONCAT(hobbies SEPARATOR ', ')
FROM peoples_hobbies
GROUP BY person_id;
As Ludwig stated in his comment, you can add the DISTINCT
operator to avoid duplicates:
SELECT person_id,
GROUP_CONCAT(DISTINCT hobbies SEPARATOR ', ')
FROM peoples_hobbies
GROUP BY person_id;
As Jan stated in their comment, you can also sort the values before imploding it using ORDER BY
:
SELECT person_id,
GROUP_CONCAT(hobbies ORDER BY hobbies ASC SEPARATOR ', ')
FROM peoples_hobbies
GROUP BY person_id;
As Dag stated in his comment, there is a 1024 byte limit on the result. To solve this, run this query before your query:
SET group_concat_max_len = 2048;
Of course, you can change 2048
according to your needs. To calculate and assign the value:
SET group_concat_max_len = CAST(
(SELECT SUM(LENGTH(hobbies)) + COUNT(*) * LENGTH(', ')
FROM peoples_hobbies
GROUP BY person_id) AS UNSIGNED);
MySQL solution to concatenate multiple rows of text based on 2 matching IDs
You're after something like that no ?
SQL Fiddle
Query 1:
SELECT
`substance_id` ,
`display_id`,
GROUP_CONCAT( `generated_text` SEPARATOR '<br>' ) as concact_text
from generated_text_tbl
group by
`substance_id` ,
`display_id`
Results:
| substance_id | display_id | concact_text |
|--------------|------------|--------------|
| 38 | 27 | xyz |
| 54 | 139 | foo<br>bar |
| 1933 | 139 | baz |
Query 2:
SELECT
`substance_id` ,
`display_id`,
GROUP_CONCAT( `generated_text` ORDER BY id DESC SEPARATOR '<br>') as concact_reverse_text
from generated_text_tbl
group by
`substance_id` ,
`display_id`
Results:
| substance_id | display_id | concact_reverse_text |
|--------------|------------|----------------------|
| 38 | 27 | xyz |
| 54 | 139 | bar<br>foo |
| 1933 | 139 | baz |
Official Documentation : https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_group-concat
How can i concatenate multiple MySQL rows into one field?
Test this:
SELECT category.id_category, category.id_category_type,
CONCAT(category.label, '-', parents.label), category.id_parent FROM category INNER JOIN
(SELECT id_category AS id, label FROM category WHERE id_parent = -1) AS parents
ON parents.id = category.id_parent WHERE category.id_category_type < 3
ORDER BY category.id_category_type;
Your data mixes "parents rows" and "child rows": when a register has id_parent = -1, then i consider is a parent row, and when id_parent <> -1 is child row.
You are requesting a result column joining "label" column from "childs rows" and "parents rows", then its necesary build a "parent table", that is achieved with this:
(SELECT id_category AS id, label FROM category WHERE id_parent = -1) AS parent
then you can use "parent" table like any other table and build your join query
How to merge a column from multiple rows into a single string
- Coalesce simply allows to replace NULL with another value. It cannot solve your task.
+
does not concatenate strings, it is arithmetic addition operator only.- If you need to concatenate values from a lot of rows into single value then you should use
GROUP BY
(maybe implicit) andGROUP_CONCAT()
:
SELECT GROUP_CONCAT([DISTINCT] name [ORDER BY name])
FROM cricketers;
If you do not need to remove duplicates then remove DISTINCT
.
If you need to limit the amount of values concatenated then you may concatenate then remove excess values by:
SELECT SUBSTRING_INDEX(GROUP_CONCAT([DISTINCT] name [ORDER BY name]), ',', 20)
FROM cricketers;
or select needed rows amount in subquery:
SELECT GROUP_CONCAT(name [ORDER BY name])
FROM ( SELECT [DISTINCT] name
FROM cricketers
[ORDER BY name]
LIMIT 20 ) subquery;
ORDER BY
expression presence is strongly recommended - without them you will receive indefinite values ordering (and in the case of amount limitation - their selection).
Related Topics
Error: Tcp Provider: Error Code 0X2746. During the SQL Setup in Linux Through Terminal
Insert into a MySQL Table or Update If Exists
SQL Update from One Table to Another Based on a Id Match
How to Update If Exists, Insert If Not (Aka "Upsert" or "Merge") in MySQL
Dynamic Alternative to Pivot With Case and Group By
Insert Statement Conflicted With the Foreign Key Constraint - SQL Server
Null in MySQL (Performance & Storage)
How to Select a Column Name With a Space in MySQL
MySQL Select Only Not Null Values
Bash Script to Insert Values in MySQL
How to Reset Auto_Increment in MySQL
How to Query Between Two Dates Using MySQL
Is Select or Insert in a Function Prone to Race Conditions
Error 1452: Cannot Add or Update a Child Row: a Foreign Key Constraint Fails
SQL Server 2012 Column Identity Increment Jumping from 6 to 1000+ on 7Th Entry
How to Combine Date from One Field With Time from Another Field - Ms SQL Server