使用MYSQL将列转换为行
我想把列转换成行,有什么方法可以实现吗。这是我的截图 我的输出是这样的使用MYSQL将列转换为行,mysql,sql,Mysql,Sql,我想把列转换成行,有什么方法可以实现吗。这是我的截图 我的输出是这样的 +----------------------+ | Month | Data | +----------------------+ | data_july | 130.11 | | data_august | 257.28 | +----------------------+ 。。。。。等等 我在查询中实现这一点时遇到了一个问题,以下是我的查询: select a.id as milestone_
+----------------------+
| Month | Data |
+----------------------+
| data_july | 130.11 |
| data_august | 257.28 |
+----------------------+
。。。。。等等
我在查询中实现这一点时遇到了一个问题,以下是我的查询:
select
a.id as milestone_id,
a.name_of_work,
case when(a.sch_jul>100) then 100 when (a.sch_jul<0) then 0 else a.sch_jul end as data_july,
case when(a.sch_aug>100) then 100 when (a.sch_aug<0) then 0 else a.sch_aug end as data_august,
case when(a.sch_sep>100) then 100 when (a.sch_sep<0) then 0 else a.sch_sep end as data_september,
case when(a.sch_oct>100) then 100 when (a.sch_oct<0) then 0 else a.sch_oct end as data_october,
case when(a.sch_nov>100) then 100 when (a.sch_nov<0) then 0 else a.sch_nov end as data_november,
case when(a.sch_dec>100) then 100 when (a.sch_dec<0) then 0 else a.sch_dec end as data_december,
case when(a.sch_jan>100) then 100 when (a.sch_jan<0) then 0 else a.sch_jan end as data_january,
case when(a.sch_feb>100) then 100 when (a.sch_feb<0) then 0 else a.sch_feb end as data_february,
case when(a.sch_mar>100) then 100 when (a.sch_mar<0) then 0 else a.sch_mar end as data_march,
case when(a.sch_apr>100) then 100 when (a.sch_apr<0) then 0 else a.sch_apr end as data_april
from
(
SELECT
distinct w.id,
w.name_of_work,
s.milestone_id,
round(((DATEDIFF(date_format('2017-07-30', '%Y-%m-%d'), s.start_date) / DATEDIFF(s.end_date, s.start_date)) * 100),2) as sch_jul,
round(((DATEDIFF(date_format('2017-08-30', '%Y-%m-%d'), s.start_date) / DATEDIFF(s.end_date, s.start_date)) * 100),2) as sch_aug,
round(((DATEDIFF(date_format('2017-09-30', '%Y-%m-%d'), s.start_date) / DATEDIFF(s.end_date, s.start_date)) * 100),2) as sch_sep,
round(((DATEDIFF(date_format('2017-10-30', '%Y-%m-%d'), s.start_date) / DATEDIFF(s.end_date, s.start_date)) * 100),2) as sch_oct,
round(((DATEDIFF(date_format('2017-11-30', '%Y-%m-%d'), s.start_date) / DATEDIFF(s.end_date, s.start_date)) * 100),2) as sch_nov,
round(((DATEDIFF(date_format('2017-12-30', '%Y-%m-%d'), s.start_date) / DATEDIFF(s.end_date, s.start_date)) * 100),2) as sch_dec,
round(((DATEDIFF(date_format('2018-01-30', '%Y-%m-%d'), s.start_date) / DATEDIFF(s.end_date, s.start_date)) * 100),2) as sch_jan,
round(((DATEDIFF(date_format('2018-02-28', '%Y-%m-%d'), s.start_date) / DATEDIFF(s.end_date, s.start_date)) * 100),2) as sch_feb,
round(((DATEDIFF(date_format('2018-03-30', '%Y-%m-%d'), s.start_date) / DATEDIFF(s.end_date, s.start_date)) * 100),2) as sch_mar,
round(((DATEDIFF(date_format('2018-04-30', '%Y-%m-%d'), s.start_date) / DATEDIFF(s.end_date, s.start_date)) * 100),2) as sch_apr
FROM tbl_scope_of_work w
left join tbl_schedule_pre_site_survey s on s.milestone_id = w.id
where w.property = 2
group by w.id
order by w.id asc
) a
选择
a、 id作为里程碑\u id,
a、 工作名称,
当(a.sch_jul>100)然后100当(a.schu jul100)然后100当(a.schu aug100)然后100当(a.schu sep100)然后100当(a.schu oct100)然后100当(a.schu nov100)然后100当(a.schu dec100)然后100当(a.schu jan100)然后100当(a.schu feb100)然后100当(a.schu mar100)然后100当(a.sch_apr您可以逐列查询,然后合并结果:
select name, value
from
(
select 'data_july' as name, data_july as value, 1 as sortkey from mytable
union all
select 'data_august' as name, data_august as value, 2 as sortkey from mytable
union all
...
) data
order by sortkey;
如果不需要保持排序顺序,则只需:
select 'data_july' as name, data_july as value from mytable
union all
select 'data_august' as name, data_august as value from mytable
union all
...
这是一个非常奇怪的查询。您将调查表加入到工作表中,然后根据工作表的ID进行分组。这是无效的SQL,因为每个工作记录都有多个调查数据。MySQL让我们看看这张纸条,并为每个工作记录选取任意调查记录。这给您留下了比较随机的结果。然后对数据使用DISTINCT
,但它已经是独立的,因为您的选择列表包含按列分组。然后对结果集排序,但当您再次从中选择时,order by
子句将无效(派生表中的数据被认为是无序的,MySQL可以完全忽略您的orderby
子句).它很好,但是是静态的..你能换成检索数据吗dynamically@AnkitAgrawal:当我写我的答案时,我希望从一个表中读取数据。在这种情况下,我们不能编写一个不那么静态的查询,因为我们从中读取的是某些列。由于OP现在添加了一个查询,并且我们看到生成了列,因此可以考虑重新写入的方法整个查询。但正如您从我对请求的评论中看到的,该查询似乎存在缺陷,因此必须首先修复。