2

我正在开发一个 mysql/php 私人消息系统,但我在使用 MYSQL 代码时遇到了问题。我想在用户之间的对话中发送/接收最后一条消息。喜欢这个 Facebook 页面https://www.facebook.com/messages/。(用语言来解释这件事并不容易。)

我的消息的 mysql 表是这样的:

`id`            -> unique ID, autoincremented
`message_from`  -> sender user ID 
`message_to`    -> receiver user ID 
`message`       -> message
`date_created`  
`ip`            
`status`        -> read or not read

我脑子里不清楚该怎么做,所以我没有要显示的初始脚本。

4

6 回答 6

2

这将为您提供每次聊天的最新消息。每个聊天都由 message_from 和 message_to ID 标识。

SELECT
*
FROM messages m1
WHERE date_created = (SELECT MAX(m2.date_created) FROM messages m2 WHERE m1.message_from = m2.message_from AND m1.message_to = m2.message_to)

如果您想进行特定的聊天,只需添加一个

AND message_from = $yourUserId
AND message_to = $yourOtherUserId

抱歉,如果这不是您要找的内容,我不会点击 Facebook 链接。

于 2012-06-04T12:44:50.633 回答
1
SELECT f . * FROM ( SELECT * FROM message a WHERE ( LEAST( a.message_from, a.message_to ) , GREATEST( a.message_from, a.message_to ) , a.sent) IN (SELECT LEAST( b.message_from, b.message_to ) AS x, GREATEST( b.message_from, b.message_to ) AS y, MAX( b.date_created ) AS msg_time FROM message b GROUP BY x, y))f WHERE ".$mainUser." IN (f.message_from, f.message_to )ORDER BY f.date_created DESC limit $pageLimit,".$per_page

只需复制此查询并在 phpmyadmin 中查看

于 2014-03-19T06:18:10.100 回答
1

首先,您构建查询以获取所有对话和最后发送消息的日期;对话被定义为发送者和接收者的唯一组合。

SELECT message_from, message_to, MAX(date_created) AS max_date
FROM messages
WHERE message_from = :user OR message_to = :user
GROUP BY message_from, message_to

:user只是当前登录用户的占位符。

要从对话中的最后一条消息中获取实际数据,您必须将结果与原始表连接起来:

SELECT message, date_created, status
FROM messages
JOIN (
    SELECT message_from, message_to, MAX(date_created) AS max_date
    FROM messages m
    WHERE message_from = :user OR message_to = :user
    GROUP BY message_from, message_to
) AS lm ON m.date_created=lm.max_date AND m.message_from=lm.message_from AND m.message_to=lm.message_to
于 2012-06-04T12:46:58.370 回答
0

也许是这样的:

SELECT
    *
FROM
    tblMessage
    JOIN(
            SELECT
                MAX(date_created) AS maxCreated,
                message_from
            FROM
                tblMessage
            GROUP BY
                message_from
        ) AS Latest
    ON tblMessage.date_created=Latest.maxCreated
    AND tblMessage.message_from=Latest.message_from
于 2012-06-04T12:38:03.857 回答
0

你可以用这个

$recup_messages = mysqli_query($connection, SELECT * FROM messages WHERE 

(message_from = $yourUserId  AND message_to = $yourOtherUserId) OR (message_from = $yourUserId  AND message_to = $yourOtherUserId ) ") or die(mysqli_error($connection));

while($affichage = mysqli_fetch_assoc($recup_messages))
{
$message_from = $affichage['message_from'];

$message = $affichage['message'];

echo "From : $message_from <br> $message <br><br>";

}

这将像 facebook 一样一个接一个地显示您的消息。现在您必须按照您想要的方式对其进行样式设置,并添加有关日期和其他内容的信息

于 2015-01-24T05:15:13.477 回答
0

这对我有用

    SELECT t1.*
        FROM messages AS t1
        INNER JOIN
        (
            SELECT
                LEAST(sender_id, receiver_id) AS sender_id,
                GREATEST(sender_id, receiver_id) AS receiver_id,
                MAX(id) AS max_id
            FROM messages
            GROUP BY
                LEAST(sender_id, receiver_id),
                GREATEST(sender_id, receiver_id)
        ) AS t2
            ON LEAST(t1.sender_id, t1.receiver_id) = t2.sender_id AND
               GREATEST(t1.sender_id, t1.receiver_id) = t2.receiver_id AND
               t1.id = t2.max_id
            WHERE t1.sender_id = :id OR t1.receiver_id = :id

其中 :id 已绑定。

从这里引用 https://laracasts.com/discuss/channels/laravel/get-the-latest-message-of-chat-model-with-mysql-just-cannot-get-the-idea-how-to-do -这个

于 2020-06-27T13:58:22.590 回答