简体   繁体   中英

How to convert multiple rows(but one column) into a single row in MySQL

I want to create a single row from multiple rows (has only one column). Please help me how to do this. The number of rows is variable.

select column_name from information_schema
where table_name='temp'

Here temp has 5 columns and the output will be:

col1
col2
col3
col4
col5

But I want the output as:

col1,col2,col3,col4,col5

You can use GROUP_CONCAT .

As in:

select person_id, group_concat(hobbies separator ', ')
from peoples_hobbies group by person_id;

There is a 1024 byte limit on result. to solve this run this query before your query:

set group_concat_max_len=2048

Off course, you can change 2048 accourding to your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM