sql query: build from table row by Id, that would be followed in order Sym Pos
a table:
+-----+-----+----+
| Sym | Pos | Id |
+-----+-----+----+
| a 开发者_开发知识库 | 0 | 0 |
| b | 1 | 0 |
| c | 2 | 0 |
| a | 0 | 1 |
| d | 1 | 1 |
| b | 0 | 2 |
+-----+-----+----+
need to build from this table row by Id, that would be followed in order Sym Pos. In this case, that would have:
+-----+----+
| str | Id |
+-----+----+
| abc | 0 |
| ad | 1 |
| b | 2 |
+-----+----+
Use GROUP BY and the MySQL specific aggregate function GROUP_CONCAT:
SELECT GROUP_CONCAT(Sym ORDER BY Pos SEPARATOR '') AS str, Id
FROM yourtable
GROUP BY id
Have a look at using GROUP_CONCAT()
精彩评论