P粉5118967162023-07-25 00:05:48
您正在嘗試對資料進行反轉。 MySQL沒有反轉函數,因此您需要使用UNION ALL查詢將列轉換為行:
select id, 'a' col, a value from yourtable union all select id, 'b' col, b value from yourtable union all select id, 'c' col, c value from yourtable
See SQL Fiddle with Demo.
這也可以使用CROSS JOIN來實現:
select t.id, c.col, case c.col when 'a' then a when 'b' then b when 'c' then c end as data from yourtable t cross join ( select 'a' as col union all select 'b' union all select 'c' ) c