开发者

SQL Group by - Listing all rows by group?

开发者 https://www.devze.com 2022-12-28 10:35 出处:网络
Given data such as TypeValue AThis Ais BWill A开发者_Go百科a Bthis Atest Bwork I would like to end up with

Given data such as

Type    Value
A       This
A       is
B       Will
A  开发者_Go百科     a
B       this
A       test
B       work

I would like to end up with

Type    Value
A       This is a test
B       Will this work

Is this possible? Thanks!


Use GROUP_CONCAT().

SELECT Type, GROUP_CONCAT(Value SEPARATOR ' ') AS Value
FROM MyTable
GROUP BY Type;

Ensuring the words are concatenated in the order you want can be tricky, however. GROUP_CONCAT() has an ORDER BY clause (see the docs) but your example doesn't include any column that can be used to determine the order. Relying on the physical storage order isn't reliable.

0

精彩评论

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