帮助sql查询

时间:2010-09-23 11:58:16

标签: mysql

表:

+-----+-----+----+ 
| Sym | Pos | Id | 
+-----+-----+----+ 
| a   | 0   | 0  | 
| b   | 1   | 0  | 
| c   | 2   | 0  | 
| a   | 0   | 1  | 
| d   | 1   | 1  | 
| b   | 0   | 2  | 
+-----+-----+----+ 

需要通过Id从此表行构建,然后按顺序Sym Pos。 在这种情况下,那将有:

+-----+-----+----+ 
| str | Pos | Id | 
+-----+-----+----+ 
| abc | 0   | 0  | 
| ad  | 0   | 1  | 
| b   | 0   | 2  | 
+-----+-----+----+

谢谢!

2 个答案:

答案 0 :(得分:2)

使用GROUP BY和MySQL特定的聚合函数GROUP_CONCAT

SELECT GROUP_CONCAT(Sym ORDER BY Pos SEPARATOR '') AS str, Id
FROM yourtable
GROUP BY id

答案 1 :(得分:1)

查看使用GROUP_CONCAT()