在mysql表中有10行,我需要将10行拆分为一列中的5行,另一列中拆分为5。
查询将得到什么结果?
最佳答案
请在下面的查询中尝试,它将动态地将行分为没有任何参数的列,只需将“ Time”替换为您的列名即可。
SELECT b.*, c.* from
(SELECT CEILING(COUNT(id) / 2) as totalId from test) as a JOIN
(SELECT id as id1, Time as column1 FROM test) as b LEFT JOIN
(SELECT id as id2, Time as column2 FROM test) as c
ON b.id1 = c.id2-a.totalId
WHERE b.id1 < a.totalId+1
您也可以将*替换为列名,如下所示:
SELECT b.column1, c.column2 from
(SELECT CEILING(COUNT(id) / 2) as totalId from test) as a JOIN
(SELECT id as id1, Time as column1 FROM test) as b LEFT JOIN
(SELECT id as id2, Time as column2 FROM test) as c
ON b.id1 = c.id2-a.totalId
WHERE b.id1 < a.totalId+1
关于php - 如何在mysql查询中的2列中拆分10行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54103697/