Develop

mysql rows to columns

작은이야기 2021. 10. 21. 23:40

https://stackoverflow.com/questions/1241178/mysql-rows-to-columns

 

MySQL - Rows to Columns

I tried to search posts, but I only found solutions for SQL Server/Access. I need a solution in MySQL (5.X). I have a table (called history) with 3 columns: hostid, itemname, itemvalue. If I do a ...

stackoverflow.com

 

select
  d2.host_id,
  ifnull(sum(d2.A),0) as A,
  ifnull(sum(d2.B),0) as B,
  ifnull(sum(d2.C),0) as C
from
  (select
    demo2.*,
    case when item_name='A' then item_value end as A,
    case when item_name='B' then item_value end as B,
    case when item_name='C' then item_value end AS C
 	 from demo2) AS d2
GROUP by d2.host_id;