SQL 将结果分组为列数组

发布于 2024-08-29 09:34:49 字数 1443 浏览 3 评论 0原文

这是一个 SQL 问题,不知道要使用哪种类型的 JOIN、GROUP BY 等,它用于聊天程序,其中消息与房间相关,房间中的每一天都链接到记录等。

基本上,当输出我的文字记录时,我需要显示哪些用户在该文字记录上聊天。目前,我通过消息链接它们,如下所示:

SELECT rooms.id, rooms.name, niceDate, room_transcripts.date, long 
 FROM room_transcripts 
  JOIN rooms ON room_transcripts.room=rooms.id 
  JOIN transcript_users ON transcript_users.room=rooms.id AND transcript_users.date=room_transcripts.date 
  JOIN users ON transcript_users.user=users.id 
 WHERE room_transcripts.deleted=0 AND rooms.id IN (1,2) 
 ORDER BY room_transcripts.id DESC, long ASC

结果集如下所示:

Array
(
    [0] => Array
        (
            [id] => 2
            [name] => Room 2
            [niceDate] => Wednesday, April 14
            [date] => 2010-04-14
            [long] => Jerry Seinfeld
        )

    [1] => Array
        (
            [id] => 1
            [name] => Room 1
            [niceDate] => Wednesday, April 14
            [date] => 2010-04-14
            [long] => Jerry Seinfeld
        )

    [2] => Array
        (
            [id] => 1
            [name] => Room 1
            [niceDate] => Wednesday, April 14
            [date] => 2010-04-14
            [long] => Test Users
        )

)

我希望数组中的每个元素代表一个转录条目,并将用户分组在数组中作为条目的元素。所以“long”将是一个列出所有名称的数组。这可以做到吗?

目前,我只是附加名称,当成绩单日期和房间发生变化时,我会回顾性地回显它们,但我会对文件和突出显示的消息执行相同的操作,这很混乱。

谢谢。

this is an SQL question and don't know which type of JOIN, GROUP BY etc. to use, it is for a chat program where messages are related to rooms and each day in a room is linked to a transcript etc.

Basically, when outputting my transcripts, I need to show which users have chatted on that transcript. At the moment I link them through the messages like so:

SELECT rooms.id, rooms.name, niceDate, room_transcripts.date, long 
 FROM room_transcripts 
  JOIN rooms ON room_transcripts.room=rooms.id 
  JOIN transcript_users ON transcript_users.room=rooms.id AND transcript_users.date=room_transcripts.date 
  JOIN users ON transcript_users.user=users.id 
 WHERE room_transcripts.deleted=0 AND rooms.id IN (1,2) 
 ORDER BY room_transcripts.id DESC, long ASC

The result set looks like this:

Array
(
    [0] => Array
        (
            [id] => 2
            [name] => Room 2
            [niceDate] => Wednesday, April 14
            [date] => 2010-04-14
            [long] => Jerry Seinfeld
        )

    [1] => Array
        (
            [id] => 1
            [name] => Room 1
            [niceDate] => Wednesday, April 14
            [date] => 2010-04-14
            [long] => Jerry Seinfeld
        )

    [2] => Array
        (
            [id] => 1
            [name] => Room 1
            [niceDate] => Wednesday, April 14
            [date] => 2010-04-14
            [long] => Test Users
        )

)

I would like though for each element in the array to represent one transcript entry and for the users to be grouped in an array as the entry's element. So 'long' will be an array listing all the names. Can this be done?

At the moment I just append the names and when the transcript date and room changes I echo them retrospectively, but I will do the same for files and highlighted messages and it's messy.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彩虹直至黑白 2024-09-05 09:34:50

结果中不能将数组作为 long 字段,因为每个数组元素代表表中的一行。但是您可以使用连接的字符串,并在需要时拆分该字符串。该查询将如下所示:

SELECT rooms.id, rooms.name, niceDate, room_transcripts.date, group_concat(long) as long
 FROM room_transcripts
  JOIN rooms ON room_transcripts.room=rooms.id 
  JOIN transcript_users ON transcript_users.room=rooms.id AND transcript_users.date=room_transcripts.date 
  JOIN users ON transcript_users.user=users.id 
 WHERE room_transcripts.deleted=0 AND rooms.id IN (1,2)
 GROUP BY room_transcripts.id
 ORDER BY room_transcripts.id DESC

You can not have an array as long field in your results, since each array element represents a row from a table. But you can have a concatened string instead, and split the string if it is required. The query will look like the following:

SELECT rooms.id, rooms.name, niceDate, room_transcripts.date, group_concat(long) as long
 FROM room_transcripts
  JOIN rooms ON room_transcripts.room=rooms.id 
  JOIN transcript_users ON transcript_users.room=rooms.id AND transcript_users.date=room_transcripts.date 
  JOIN users ON transcript_users.user=users.id 
 WHERE room_transcripts.deleted=0 AND rooms.id IN (1,2)
 GROUP BY room_transcripts.id
 ORDER BY room_transcripts.id DESC
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文