从一个Mysql中创建3个配对列

时间:2016-06-22 18:00:09

标签: mysql

我尝试对数据库中的表进行查询时遇到问题。

所以我的表有一个名称如下的列:

enter image description here

我想要一个在以下模式中选择名称和ID的查询:

enter image description here

enter image description here

提前致谢...

1 个答案:

答案 0 :(得分:1)

您可以使用内部联接并计算ID

@my_len = 3

select a.id as id, a.name as name, b.id as id1, b.name as name1, c.id as id2, c.name as name2
from my_table as a
inner join my_table as b where a.id = b.id+@my_len
inner join my_table as c where a.id = c.id+@my_len*2