简体   繁体   中英

Pivot column to row in mysql result output

I have the following result

A   B   C
1   2   3

How do I get the result like

title1  title2
A   1
B   2
C   3

My query is something like this

select
sum(if(d =4,1,0)) AS 'A',
sum(if(d = 4 and (datediff(curdate(), r_date)=1),1,0)) AS ' B',
sum(if(d = 1 and (datediff(curdate(), r_date)=2),1,0)) AS ' C'
from delay ;

It appears that you are trying to unpivot a table in MySQL. One way to do this would be to use a series of unions:

SELECT 'A' AS title1, A AS title2
FROM delay
UNION ALL
SELECT 'B' AS title1, B AS title2
FROM delay
UNION ALL
SELECT 'C' AS title1, C AS title2
FROM delay

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