需要一些编写 MySQL 查询的支持

发布于 2024-11-03 05:34:46 字数 526 浏览 7 评论 0原文

我正在用 PHP/MySQL 编写一个基本的消息脚本,但现在我陷入了数据库查询。我将不胜感激任何提示或帮助(:

我正在使用两个表,因为一条消息可以发送给多个用户:

messages:
id | sender_id | subject | ...

message_receivers:
message_id | receiver_id | ...

我现在想做的是向他选择的用户显示一条消息。但我想显示用户在该对话中的整个消息历史记录(在浏览器中跳转到他选择的消息历史记录)通过连接执行此操作非常简单:

SELECT * FROM messages
    JOIN message_receivers 
    ON messages.id = message_receivers.message_id
    WHERE sender_id = x
    AND receiver_id = y

但现在我丢失了消息的其他接收者的信息,而且我不知道如何做到这一点!有什么想法吗? (:

I'm writing a basic message-script in PHP/MySQL but I'm stuck at a database query right now. I'll appreciate any hints or assistance (:

I'm using two tables, since a message can be sent to several users:

messages:
id | sender_id | subject | ...

message_receivers:
message_id | receiver_id | ...

What I want to do now is display a message to the user that he selects. But I want to show the whole message history the user had in that conversation (jumping in browser to the one he selected). Doing this with a join is quite simple:

SELECT * FROM messages
    JOIN message_receivers 
    ON messages.id = message_receivers.message_id
    WHERE sender_id = x
    AND receiver_id = y

But now I'm missing the information of other receivers of a message! And I have no clue how to get this information. Any ideas for that? (:

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

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

发布评论

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

评论(4

如歌彻婉言 2024-11-10 05:34:46

再次加入 message_receivers 表以检索消息的其他收件人:

SELECT
    m.id, m.sender_id, m.subject,
    r.receiver_id AS recipient,
    c.receiver_id AS carboncopy
FROM messages AS m
INNER JOIN message_receivers AS r
    ON m.id = r.message_id
LEFT OUTER JOIN message_receivers AS c
    ON r.message_id = c.message_id AND r.receiver_id != c.receiver_id
WHERE m.sender_id = x AND r.receiver_id = y

您感兴趣的收件人将位于 recipient 列中(在每个结果记录中)。其他收件人将位于carboncopy 列中(每个结果记录一个)。如果 Carboncopy 为 NULL,则该消息只有一个接收者。

Join the message_receivers table one more time to retrieve the other recipients of the message:

SELECT
    m.id, m.sender_id, m.subject,
    r.receiver_id AS recipient,
    c.receiver_id AS carboncopy
FROM messages AS m
INNER JOIN message_receivers AS r
    ON m.id = r.message_id
LEFT OUTER JOIN message_receivers AS c
    ON r.message_id = c.message_id AND r.receiver_id != c.receiver_id
WHERE m.sender_id = x AND r.receiver_id = y

The recipient that your are interested in will be in column recipient (in every result record). Other recipients will be in column carboncopy (one per result record). If carboncopy is NULL, then the message had only a single receiver.

初相遇 2024-11-10 05:34:46

如果您想查看消息的所有接收者,请删除 were 子句的第二部分:

AND receiver_id = y

同时您需要指定 message_id,因为这会让前端用户感到困惑

AND message_id = z

If you want to see all the receivers of a message then remove the second part of the were clause:

AND receiver_id = y

at the same time you will want to specify the message_id because this will be to confusing to the user on the front end

AND message_id = z
晨光如昨 2024-11-10 05:34:46

由于以下子句,您缺少有关消息的其他接收者的信息:

AND
receiver_id = y

这将结果集限制为仅接收者y。删除该条款,您将获得所有这些。不过,您可能也会收到 sender_id = x 处发送的每条消息,因此您需要通过指定 message_id 来限制查询。

所以你的最终查询应该是这样的:

SELECT
*
FROM
messages
JOIN message_receivers ON messages.id = message_receivers.message_id
WHERE
sender_id = x
AND
message_id = y

You're missing information about other receivers of the message because of the clasue:

AND
receiver_id = y

This restricts the result set to just receiver y. Remove the clause, and you'll get them all. However you'll probably get every message sent where sender_id = x as well, so you'll need to limit the query by specifying a message_id.

So your final query should look something like this:

SELECT
*
FROM
messages
JOIN message_receivers ON messages.id = message_receivers.message_id
WHERE
sender_id = x
AND
message_id = y
忆离笙 2024-11-10 05:34:46

您不需要将结果限制为receiver_id = y,是吗?
您也可以用不同的方式编写语句并轻松返回接收者 ID:

SELECT m.*, r.receiver_id
FROM messages m, message_receivers r
WHERE m.id = r.message_id
AND m.sender_id = x

you don't need to restrict your result to receiver_id = y, do you?
Also you might write the statement in a different way and easily return receivers ids:

SELECT m.*, r.receiver_id
FROM messages m, message_receivers r
WHERE m.id = r.message_id
AND m.sender_id = x
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文