我有一个SQL数据库表,其中有四列具有不同的时间。我希望能够对它们进行排序,以便可以将a,b,c和d时间戳中的每一个合并到一个列中,同时保留id和元数据。
id | a_time | b_time | c_time | d_time | metadata1 | metadata2 1 | 8 | 7 | 2 | 4 | a | b 2 | 6 | 1 | 12 | 10 | c | d 3 | 5 | 9 | 3 | 11 | e | f
您可以union在不同时间进行几个查询:
union
(SELECT id, a_time AS time, metadata1, metadata2 FROM mytable) UNION (SELECT id, b_time AS time, metadata1, metadata2 FROM mytable) UNION (SELECT id, c_time AS time, metadata1, metadata2 FROM mytable) UNION (SELECT id, d_time AS time, metadata1, metadata2 FROM mytable) ORDER BY time