【问题标题】:Need a solution for mysql select data where until需要一个mysql选择数据的解决方案,直到
【发布时间】:2013-09-09 08:14:24
【问题描述】:

我有两个人用户 222 和用户 5555 之间的消息对话,显示如下:

Conversation-ID 1, Message-ID 5, User-ID 222: "Oh, and I'm happy about that!"
Conversation-ID 1, Message-ID 4, User-ID 222: "And bought me a new car."
Conversation-ID 1, Message-ID 3, User-ID 222: "I just won some money!"
Conversation-ID 1, Message-ID 2, User-ID 5555: "Fine, how are you?"
Conversation-ID 1, Message-ID 1, User-ID 222: "Hi there, how are you?"

现在我正在寻找 PHP / MySQL 中的解决方案,以仅显示来自 User-ID 222 的消息,直到来自 User-ID 5555 的消息。

结果应该是:

Conversation-ID 1, Message-ID 5, User-ID 222: "Oh, and I'm happy about that!"
Conversation-ID 1, Message-ID 4, User-ID 222: "And bought me a new car."
Conversation-ID 1, Message-ID 3, User-ID 222: "I just won some money!"

其实我有

"SELECT * FROM table_conversations WHERE conversation_id='1' and user_id !='5555' ORDER BY message_id DESC"

但这当然会显示来自 User-ID 222 的所有消息。我想要的是只有来自 User-ID 222 的最后未答复的消息

PHP 和 MySQL 中是否有任何命令或解决方案可以得到这个结果,也适用于所有其他对话?

【问题讨论】:

    标签: php mysql sql select


    【解决方案1】:

    假设消息 id 指定消息的顺序:

    select *
    from table_conversation c
    where user_id = 222 and
          message_id > (select max(message_id) from table_conversation c2 where user_d = 555);
    

    【讨论】:

    • 修复了代码中的输入错误,删除了 cc1 并将对话 ID 添加到 select max 部分使您的代码正常工作。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 2014-03-21
    • 2016-03-23
    • 2019-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多