当使用提供的 id 时,mysql 选择带有外键的连接

发布于 2024-10-25 05:41:41 字数 959 浏览 1 评论 0原文

另外,如果这不是正确的位置,那么我会将其移动到堆栈溢出。我把它贴在这里,因为当我在那里有一个关于mysql的问题时,他们说这是应该问这样的问题的地方。

我遇到的问题是我的聊天系统。我有一个普通的聊天桌,然后我还有一个用于私人消息的聊天桌。因此,私人消息聊天表仅包含私人消息 ID,以及应该接收该消息的人。

表架构是这样的。

chat(
id int unsigned not null,
sayer_id int unsigned not null,
message_type tinyint unsigned not null,
message varchar(150) not null,
timesent timestamp on update current_timestamp,
);

索引位于 id 和 timesent 上。 Primary是id,也是timesent上的普通索引。 第二张桌子只是。

chat_private(
message_id int unsigned not null,
target_id int unsigned not null
)

主键位于消息 ID 上,也是外键。目标 ID 目前还没有,但我可能会在上面放置一个普通索引。

现在,我尝试执行的查询类似于 SELECT message, timesent FROM chat WHERE timesent>=last_update AND target_id=$userid; last_update 是一个会话变量,说明上次执行更新的时间。 $userid 是服务器端会话变量的 ID。 另外,我不知道如何轻松地执行类似的操作,因为我想从 chat_private 表中加入其中,但我也不想处理其中的所有 id,因为它只包含target_id/message_id 因此毫无意义。

处理数据的最简单方法是什么?如果无法使用,那么我只需将私人聊天和其他后续聊天移至自己的表中,并相应地调整数据大小。

Also, if this isn't the right place then i'll move it to stack overflow. I posted it here, since when i had a question about mysql over there, they said that this is where such questions should be asked.

The problem that i'm having is with my chat system. I have a normal chat table and then i also have on that is used for private messages. As such the private message chat table only contains the message id that was private, and also the person who was supposed to receive it.

the table schema is something like this.

chat(
id int unsigned not null,
sayer_id int unsigned not null,
message_type tinyint unsigned not null,
message varchar(150) not null,
timesent timestamp on update current_timestamp,
);

the indexes are on id, and timesent. Primary is id, and also a normal index on timesent.
The second table is just.

chat_private(
message_id int unsigned not null,
target_id int unsigned not null
)

the primary is on message id and is also a foreign key. target id currently doesn't have one at the moment but i may put a normal index on it.

Now then, the query that i'm attempting to do is something like SELECT message, timesent FROM chat WHERE timesent>=last_update AND target_id=$userid;
last_update is a session variable stating when the last time that updates were performed.
$userid is the id from the session variable on the serverside.
Also I don't know how to easily, do something similar to that since i want to have the join in it from the chat_private table but i also don't want to have to deal with all of the ids from it since it only holds the target_id/message_id and thus would be rather pointless.

What's the easiest way to work with the data as it is? If it's impossible to work with, then i'll just move the private chat and other subsequent ones into their own table and adjust the data sizes accordingly.

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

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

发布评论

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

评论(1

百变从容 2024-11-01 05:41:41

你想要类似的东西

SELECT chat.message, chat.timesent FROM chat LEFT JOIN chat_private ON chat.id=chat_private.message_id WHERE chat.timesent>='$timestamp' AND chat_private.target_id=$target_id

You want something like

SELECT chat.message, chat.timesent FROM chat LEFT JOIN chat_private ON chat.id=chat_private.message_id WHERE chat.timesent>='$timestamp' AND chat_private.target_id=$target_id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文