简体   繁体   中英

Can I concatenate multiple MySQL Column into One Column?

Good day i would like to ask if this is possible in MySQL

 SELECT id,label,name,age,sex FROM table LIMIT 3

Output

[row1] id,label,name,age,sex
[row2] id,label,name,age,sex
[row3] id,label,name,age,sex

My Output Needed

[row1] id
[row2] label
[row3] name
[row4] age
[row5] sex
[row6] id
[row7] label
[row8] name
[row9] age
[row10] sex
[row11] id
[row12] label
[row13] name
[row14] age
[row15] sex

You can do something like this:

SELECT * FROM
((SELECT id AS id1, 1 AS rownum, 'id' AS colname, id AS Data_value FROM mytable LIMIT 3)
UNION ALL
(SELECT id, 2, 'label', label FROM mytable LIMIT 3)
UNION ALL
(SELECT id, 3, 'name', name FROM mytable LIMIT 3)
UNION ALL
(SELECT id, 4, 'age', age FROM mytable LIMIT 3)
UNION ALL
(SELECT id, 5, 'sex', sex FROM mytable LIMIT 3)) A 
 ORDER BY id1, rownum

Here's a fiddle: https://www.db-fiddle.com/f/dvg6x1vBg6H5bDNp9VZxQa/4

I've added 3 additional column id AS id1 , rownum and colname . The first two additional column is used for ORDER BY at the outer query. If you don't want to see the additional column, you can just type SELECT Data_value FROM ... at the outer query.

You can use group_concat() to aggregate rows by string concatenation. For the LIMIT to work you then need to use a derived table. But you should be careful with a LIMIT without an ORDER BY . As the order of a query result can be random unless an explicit ORDER BY is issued, you may get different results each time you run the query.

SELECT group_concat(id,
                    '\n',
                    label,
                    '\n',
                    name,
                    '\n',
                    age,
                    '\n',
                    sex
                    SEPARATOR '\n')
       FROM (SELECT id,
                    label,
                    name,
                    age,
                    sex
                    FROM elbat
                    LIMIT 3) x;

If you just want to concatenate the columns but keep the rows just use concat() .

SELECT concat(id,
              '\n',
              label,
              '\n',
              name,
              '\n',
              age,
              '\n',
              sex)
       FROM elbat
       LIMIT 3;

yes,you can use union all like below :

SELECT id FROM table LIMIT 3
union all
SELECT label FROM table LIMIT 3
union all
SELECT name FROM table LIMIT 3
union all
SELECT age FROM table LIMIT 3
union all
SELECT sex FROM table LIMIT 3

That what you looking is to Unpivot data. For more info about pivot and unpivot you can check here.

http://archive.oreilly.com/oreillyschool/courses/dba1/dba110.html

Unfortunately there is no easy way to unpivot in mysql.

The below script will work for MySQL 8.0

set @rowNum :=0;
set @string :=( 
       select group_concat(id,',',label,',',name,',',age,',',sex separator ',')
       from (
         select id, label, name, age, sex from mytable limit 3
       ) x
);


 with recursive
    R1 as ( select @string as items),
    R2 as ( select 1 as n 
             union 
           select n + 1 from R2, R1
           where n <= length(items) - length(replace(items, ',', '')))
    select distinct @rowNum := @rowNum+1 as rowNum, substring_index(substring_index(items, ',', n), ',', -1) output from R2, R1;

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