2016-08-03 48 views
0

所以,我有一个非常规的非常规问题。我希望能够将具有相同ID的行连接成一个大行。为了说明我的问题,让我举个例子。下面是该查询:PostgreSQL:如何连接行

SELECT b.id AS "ID", 
     m.content AS "Conversation" 
FROM bookings b 
INNER JOIN conversations c on b.id = c.conversable_id AND c.conversable_type = 'Booking' 
INNER JOIN messages m on m.conversation_id = c.id 
WHERE b.state IS NOT NULL 
GROUP BY 1,2 
LIMIT 1000; 

这里是输出:

ID  **Conversation 
1223 "blah, blah, blah, blah" 
1223 " ... blaaaah, blah.." 
1223 "more blaaah" 
1223 "last blah" 
5000 "new id, with more blah" 
5000 "and the blah continues" 

有没有一种方法来连接会话行成一个总计行,同时保持ID?

像这样:

ID  Conversation 
1223 "blah, blah, blah, blah, ... blaaaah blah.. more blaaah, last blah" 
5000 "new id, with more blah and the blah continues" 

我相信有一个有效的方式来做到这一点。我无法自己想出来。

+0

'group_concat()'是你的朋友。 – wildplasser

+0

是不是MySQL? – DBE7

+0

可能。也许'string_agg()',那么? – wildplasser

回答

0

我能够通过查看this question的精彩答案来解决我自己的问题。这与使用PostgreSQL string_agg()-功能一样简单。