如何在mysql查询中的2列中拆分10行

时间:2019-01-09 05:25:02

标签: php mysql mysqli

enter image description here

在mysql表中有10行,我需要将10行拆分为一列中的5行,再将5列拆分为另一列。

enter image description here

要获得这种结果的查询是什么?

2 个答案:

答案 0 :(得分:1)

在MySQL中做这件事太可怕了,最好还是用一种应用程序语言来做。

您可以执行以下操作,但是它缺乏任何可扩展性:

0

答案 1 :(得分:0)

请尝试下面的查询,它将动态地将行分解为没有任何参数的列,只需将“ 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