这次我有一些问题要解决我的问题,所以我在这里。
我在数据库中有一个表,名称是test,包含3个属性(id,id_position,name)。详情请看这里
这里我们有来自group_concat的新结果,我给出了新的字段名称(结果)。我只想将(result)字段设置为新的动态列,跟随(id_position)。示例结果:
+-------------+-----------+----------+-----------+----------+
+ id_position + result1 + result2 + result3 + result4 +
+-------------+-----------+----------+-----------+----------+
+ 1 + name_11 + name_6 + name_5 + name_3 +
+ 2 + name_2 + name_1 + - + - +
+ 3 + name_4 + name_7 + - + - +
+ 4 + name_8 + name_9 + name_10 + - +
+-------------+-----------+----------+-----------+----------+
如果结果为空,请给出( - )之类的符号。请提出你的建议,以解决这个问题,谢谢你的时间
答案 0 :(得分:0)
也许这不是最灵活和最美丽的解决方案,但它适用于4个职位的情况:http://sqlfiddle.com/#!2/a3b77/120
SELECT
t1.id_position,
(select name from test where id_position=t1.id_position limit 0,1) AS result1,
(select name from test where id_position=t1.id_position limit 1,1) AS result2,
(select name from test where id_position=t1.id_position limit 2,1) AS result3,
(select name from test where id_position=t1.id_position limit 3,1) AS result4
FROM test t1
GROUP BY t1.id_position
ORDER BY t1.id_position;