mysql使用varchar值将行转换为列

flseospp  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(248)

我有这样的table:

+-------+------------+---------+-------------------------+
| Id    | Mission    | Status  | Time                    |
+-------+------------+---------+-------------------------+
| 1     | Mission 1  | Start   | 2020-06-01 09:50:00     |
| 2     | Mission 1  | Running | 2020-06-01 10:50:00     |
| 3     | Mission 1  | Stop    | 2020-06-01 11:50:00     |
+-------+------------+---------+-------------------------+

我的期望输出是

+------------+-------------------------+---------------------+---------------------+
| Mission    | Start                   | Running             | Stop                |
+------------+-------------------------+---------------------+---------------------+
| Mission 1  | 2020-06-01 09:50:00     | 2020-06-01 10:50:00 | 2020-06-01 11:50:00 |
+------------+-------------------------+-------------------------------------------+

我发现了一些与我的问题有关的问题,但这些问题与我的不完全一致。

cs7cruho

cs7cruho1#

select mission, 
       max(case when status = 'Start' then time end) as `Start`,
       max(case when status = 'Running' then time end) as Running,
       max(case when status = 'Stop' then time end) as Stop
from your_table
group by mission

相关问题