本文介绍了将列转置为行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下查询正在按预期方式工作.

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 |

连续的预期结果:

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

推荐答案

您可以使用全部联盟为此

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

这篇关于将列转置为行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-12 02:45