如何通过查询从MySql表中获取矩阵

本文关键字:获取 MySql 何通过 查询 | 更新日期: 2023-09-27 18:26:15

我使用的mySql表如下所示:

ID   mondend  |MondStart |TuesEnd   |TuesStrt  |wedsEnd  |wednStrt  
120| 06:00:00 | 22:00:00 |06:00:00  |22:00:00  |06:00:00    |22:00:00       
122| 06:00:00 | 22:00:00 |06:00:00  |22:00:00  |06:00:00    |22:00:00 

有没有可能通过查询得到这样的表?:

120| monday     |   22:00:00 |06:00:00  |   
   | tuesday    |   22:00:00 |06:00:00  |
   | tuesday    |   22:00:00 |06:00:00  |
122|monday      |   22:00:00 |06:00:00  |   
   | tuesday    |   22:00:00 |06:00:00  |
   | tuesday    |   22:00:00 |06:00:00  |

如何通过查询从MySql表中获取矩阵

一个解决方案是使用UNION ALL查询:

SELECT ID, 'monday' AS week_day, MondStart AS day_start, TuesEnd day_end
FROM tablename
UNION ALL
SELECT ID, 'tuesday' AS week_day, TuesStrt AS day_start, WedEnd day_end
FROM tablename
UNION ALL
...etc...

如果你想要一个表而不是查询,你可以使用:

CREATE TABLE final_table AS
...the select query above...