开发者

sql query: build from table row by Id, that would be followed in order Sym Pos

开发者 https://www.devze.com 2023-01-17 02:37 出处:网络
a table: +-----+-----+----+ Sym | Pos | Id | +-----+-----+----+ a开发者_开发知识库 | 0| 0|

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()

0

精彩评论

暂无评论...
验证码 换一张
取 消