Полагаю, вы хотите заменить только заголовки, поэтому с помощью information_schema.columns и dynamici c sql
set @sql = (select concat('select date,',
aa.gc1,
' from (',
replace(substring(bb.gc2,1,length(bb.gc2) - length('union all')),'union all,','union all '),
') s group by date'
)
from
(
select
group_concat(
'max(case when itemname = ',char(39),itemname,char(39),' then value end) as ',itemname
) gc1
from
(
select itemname , column_name from information_schema.columns
join t1 on t1.itemcode = column_name
where table_name = 't'
and column_name in (select distinct itemcode from t1)
) a
) aa
cross join
(
select
group_concat(
'select date,',char(39),itemname,char(39),' as itemname,', column_name,' as value from t union all'
) gc2
from
(
select itemname , column_name from information_schema.columns
join t1 on t1.itemcode = column_name
where table_name = 't'
and column_name in (select distinct itemcode from t1)
) b
) bb
) ;
prepare sqlstmt from @sql;
execute sqlstmt;
deallocate prepare sqlstmt;
+------+-------+-------+
| date | candy | Apple |
+------+-------+-------+
| 2020 | 2 | 3 |
+------+-------+-------+
1 row in set (0.001 sec)