MySql 连接聚合对象
我正在编写一个在线聊天,我有两个对象,一个聊天室和一个聊天用户。 1个聊天室可以容纳多个聊天用户。
SELECT Chatrooms.created_dt,
Chatrooms.description,
ChatUser.first_name,
ChatUser.last_name,
ChatUser.email
FROM Chatrooms
LEFT JOIN ChatUser
ON ChatUser.room_id = Chatrooms.id
WHERE Chatroom.status = 1
这将为我提供类似的行,
2011-09-08 , I need serious help , daffy, duck, [email protected]
2011-09-08 , I need serious help , donald, duck, [email protected]
2011-09-08 , I need serious help , darkwing, duck, [email protected]
显然我想避免每行重复聊天室信息。但是,我也想避免运行多个查询。我只有基本的数据库经验,并且由于 mysql 没有外键(不知道这对我有什么帮助),我不确定有什么更好的选择。
在这种情况下我可以做更好的事情,或者只是不用担心重复的列?
I am writing an online chat and I have two objects, a chat room and a chat user. 1 chat room can contain many chat users.
SELECT Chatrooms.created_dt,
Chatrooms.description,
ChatUser.first_name,
ChatUser.last_name,
ChatUser.email
FROM Chatrooms
LEFT JOIN ChatUser
ON ChatUser.room_id = Chatrooms.id
WHERE Chatroom.status = 1
this will provide me with rows like
2011-09-08 , I need serious help , daffy, duck, [email protected]
2011-09-08 , I need serious help , donald, duck, [email protected]
2011-09-08 , I need serious help , darkwing, duck, [email protected]
Obviously I would like to avoid duplicating the chat room information every row. However, I would also like to avoid running multiple queries. I only have basic experience with dbs and as mysql doesn't have foriegn keys (not sure how that would even help me), I am not sure what better options are.
Is there something better I can do in this situation, or just not worry about the duplicate columns?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,我不认为有一种方法可以在不运行两个查询或不重复某些信息的情况下选择您想要获取的信息。
我的意思是,您所做的本质上是要求数据库在同一查询中获取有关聊天室和聊天用户的信息......除非您能找到一种方法使用相同的列将其写在一张纸上以及每一行的数据类型,这可能是不可能的。
话虽如此,您可能可以忽略每一行中的重复数据来实现您的目标。
As far as I know, I don't believe there is a way to select the information that you're trying to achieve without either running two queries or having some of the information duplicated.
I mean essentially what you're doing is asking the database to fetch you information about the chat room AND chat users in the same query... and unless you could find a way to write that out on a piece of paper using the same columns and data types for every row, it probably isn't possible.
So having that said, you probably could just ignore the duplicate data in every row to accomplish your goal.