MYSQL - 旋转数据

MYSQL - Pivoting Data

我的应用程序中的数据目前以一系列列的形式出现,但我真的希望只有两列,RESULTS 和 TASK。请参阅随附的 "Spreadsheet" 屏幕截图,了解我当前的数据是如何进入的,以及 "Database" 屏幕截图,了解我想要实现的目标。

我目前能够使用大约 100 个 UNION ALL 语句执行此操作,但我的查询变得非常慢。没有那么多 UNION ALL,有没有更好的方法来实现这一点?

数据库:

谢谢!

UNION ALL 是一种很好的方法,但它确实需要为每个子查询扫描一次 table。

一个替代方案使用 cross join。这需要更多编码,但它可能会加快速度:

select tt.task,
       (case when tt.task =  'Use Proper PPE' then use_proper_ppe
             when tt.task = 'Damage Prevention' then damage_prevention
             . . .
        end) as result
from t cross join
     (select 'Use Proper PPE' as task union all
      select 'Damage Prevention' union all
      . . .
     ) tt;