我想將列轉換爲行,有沒有什麼辦法可以實現這一點。這裏是我的截圖enter image description here將列轉置爲行使用MYSQL
我的輸出這樣的
+----------------------+
| 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
可能的副本吃了https://stackoverflow.com/questions/7674786/mysql-pivot-table和https://stackoverflow.com/questions/7674786/mysql-pivot-table –
沒有它不同的東西 – user3734149
請參閱https:// stackoverflow.com/questions/16913717/mysql-select-column-name-and-value-as-a-field – KMS