这是一个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'将是一个列出所有名字的数组。可以这样做吗?
目前我只是追加名字,当成绩单日期和房间发生变化时,我会追溯回复它们,但我会对文件和突出显示的信息做同样的事情并且它很混乱。
感谢。
答案 0 :(得分:4)
您的结果中不能将数组作为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