+-------+-----------+------------+
| id | sequence | text |
+-------+-----------+------------+
| 12345 | 01 | Hello, my |
| 12345 | 02 | name is CJ |
| 54321 | 01 | Good morn |
| 54321 | 02 | ing! Sup! |
+-------+-----------+------------+
I am totally lost. There are 2 messages in these rows 1) "Hello, my name is CJ" and 2) "Good morning! Sup!"
How could I display both of these messages by selecting them by IDs and with correct order(sequence)?
So the results would be
"Hello, my name is CJ"
"Good morning! Sup!"
SELECT id,GROUP_CONCAT(text ORDER BY sequence SEPARATOR ' ') AS messages
FROM table1
GROUP BY id;
Output
id messages
12345 Hello, my name is CJ
54321 Good morning! Sup!
Demo
GROUP_CONCAT() function
MySQL GROUP_CONCAT() function returns a string with concatenated non-NULL value from a group.
Syntax :
GROUP_CONCAT([DISTINCT] expr [,expr ...]
[ORDER BY {unsigned_integer | col_name | expr}
[ASC | DESC] [,col_name ...]]
[SEPARATOR str_val])
TRY GROUP_CONCAT
SELECT id,GROUP_CONCAT(text SEPARATOR ' ')
FROM tableA
GROUP BY id;
select GROUP_CONCAT(text SEPARATOR ' ')
from table_name
group by id