MYSQL 透视/将行转换为值匹配的列

MYSQL pivot / turning rows into columns where a value matches

我真的需要一些帮助来了解如何根据相似的值来调整 table。

   day  |  startDate
-----------------------
Monday  |  09:00
Monday  |  13:00
Tuesday |  08:30
Tuesday |  12:30

理想情况下,我希望我的结果类似于...

   day  |  firstStartDate | secondStartDate
-------------------------------------------
Monday  |       09:00     |     13:00
Tuesday |       08:30     |     12:30

一般来说,我知道我每天只会有两个实例,所以将它们转换成列是一件固定的事情。

如有任何建议,我们将不胜感激!

我相信这可以使用 GROUP BY 来完成,就像这样

SELECT 
   day, 
   MIN(startDate) as firstStartDate, 
   MAX(startDate) as secondStartDate 
FROM TableName 
GROUP BY day

您可以使用自联接

select a.day , a.startDate as firstStartDate, b.startDate as secondStartDate
from my_table as a 
inner join my_table as b  on a.day = b.day and a.startDate < b.startDate