简体   繁体   English

将列转置为行

[英]transpose columns to rows

The following query is working as expected. 以下查询按预期方式工作。

But how do I get the results in rows those are displayed in columns? 但是,如何获得显示在列中的行中的结果?

select curdate() AS one, 
date_sub(curdate(), interval 15 day) AS two
, date_sub(curdate(), interval 30 day) AS three
, date_sub(curdate(), interval 45 day) AS four
, date_sub(curdate(), interval 60 day) AS five
, date_sub(curdate(), interval 75 day) AS six
, date_sub(curdate(), interval 90 day) AS seven
;

| one        | two        | three      | four       | five       | six        | seven      |
+------------+------------+------------+------------+------------+------------+------------+
| 2010-09-27 | 2010-09-12 | 2010-08-28 | 2010-08-13 | 2010-07-29 | 2010-07-14 | 2010-06-29 | 

Expected results in a row: 预期结果:

one 2010-09-27
two 2010-09-12
three 2010-08-28
four 2010-08-13
five 2010-07-29
six 2010-07-14
seven 2010-06-29

You can use UNION ALL for this 您可以为此使用UNION ALL

select 'one' as label,  curdate() as val
UNION ALL
select 'two' as label,  date_sub(curdate(), interval 15 day) as val
UNION ALL
select 'three' as label,  date_sub(curdate(), interval 30 day)  as val
UNION ALL
select 'four' as label,  date_sub(curdate(), interval 45 day) as val
UNION ALL
select 'five' as label,  date_sub(curdate(), interval 60 day) as val
UNION ALL
select 'six' as label,  date_sub(curdate(), interval 75 day)  as val
UNION ALL
select 'seven' as label,  date_sub(curdate(), interval 90 day) as val

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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