3

我有history以下列的表格:

row_id, msgid, sender, receiver, chatcontent, transdate, transtime

每个聊天记录都作为单独的行存储在表中。

场景:如果内容超过 2048 个字符,我的框架将聊天内容拆分为多个脚本,数据存储在多行中,具有相同的详细信息,例如 msgid、sender、receiver、transdate 和 transtime,只有 chatcontent 与 row_id 作为序列不同.

例如,我在表中的内容是

001, msgid1, mark@test.int, james@test.int, this is a long tes, 2013-03-13, 13:55:34
002, msgid1, mark@test.int, james@test.int, t message which is, 2013-03-13, 13:55:34
003, msgid1, mark@test.int, james@test.int,  splitted in multi, 2013-03-13, 13:55:34
004, msgid1, mark@test.int, james@test.int, ple rows, 2013-03-13, 13:55:34
005, msgid2, james@test.int, mark@test.int, yup i got you, 2013-03-13, 13:56:12

现在我想在单个查询中获取数据,该查询应该输出为

msgid1, mark@test.int, james@test.int, this is a long test message which is splitted in multiple rows, 2013-03-13, 13:55:34
msgid2, james@test.int, mark@test.int, yup i got you, 2013-03-13, 13:56:12

怎么做。我无法在单个查询中获取所有详细信息。我可以使用命令将聊天内容合并在一列中,但我不想硬编码 msgid 的值

SELECT array_to_string(array(SELECT chatcontent FROM history where msgid='msgid1'),'');
4

1 回答 1

9
SELECT
    msgid,
    sender,
    receiver,
    transdate,
    transtime,
    array_to_string(array(
        select chatcontent
        from history
        where msgid = h.msgid
        order by row_id
        ), ' '
    ) chatcontent
from history h
group by 1, 2, 3, 4, 5, 6
order by 1
于 2013-03-14T12:45:03.390 回答