【问题标题】:MariaDB & Android - Implementing Message feature, Selecting users and its messages respectivelyMariaDB & Android - 实现消息功能,分别选择用户及其消息
【发布时间】:2018-07-11 17:59:35
【问题描述】:

我有一个 MESSAGE 表。

它的列是

| seq | sender_email | receiver_email | content | date | is_read | sender_profile_icon_url|

在我的 Android 应用中,我想先显示用户列表。

你可以想象一下普通的Messenger应用,比如Kakaotalk、Line、Whatsapp等。

If A ever sent a message to B OR if B ever sent a message to A.

1. A's view

B's information(email, photo) with the most recent message between
B and A must be shown in the list(MessageUserList Activity) of A.
If you click B, you see all the messages between B and A.

2. B's view

A's information(email, photo) with the most recent message between
A and B must be shown in the list(MessageUserList Activity) of A.
If you click B, you see all the messages between B and A.

所以,它看起来像这样(chanjungskim@gmail.com 的观点):

左:显示曾经发送或接收过至少一条消息的用户。

右:点击第一个用户(fman1335@gmail)后显示所有消息。

您应该知道的是,您可以向自己发送消息。然后,所有消息都会以绿色放置在右侧。

图片看起来很完美……但事实并非如此。

我对左图的尝试是......

select  m3.*
    from
        ( SELECT  m1.*
            from  message as m1
            left outer join  message as m2  ON m1.sender_email = m2.sender_email
              and  (m1.date < m2.date
                      or  (m1.date = m2.date
                              and  m1.seq < m2.seq)
                   )
            where  m2.sender_email is null
        ) as m3
    where  m3.sender_email=?
       or  m3.receiver_email=?
    order by  date desc
    limit  30;

我用正确的图片尝试的是......

select  *
    from  message
    where  sender_email=?
       or  receiver_email=?
       or  sender_email=?
       or  receiver_email=?
    order by  date;

它带来了错误的数据。

目前的问题是

如果A和B互相发送消息,那么A、B在双方的MessageUserListActivity上。它需要分别是一个(例如 A 看到 B,B 看到 A。)。而如果 A 点击列表中的 A 用户,那么会带来错误的数据。

我不知道我需要解决什么问题。有什么问题?

【问题讨论】:

    标签: android mysql sql mariadb messenger


    【解决方案1】:

    要查找“最后一个”电子邮件:

    SELECT  *
        FROM  message
        WHERE  sender_email = ...
        ORDER BY  date DESC, seq DESC
        LIMIT  1;
    

    这比使用 LEFT JOIN 更简单、更快捷。

    “正确”查询需要使用这个,我认为:

    SELECT  * FROM message
        WHERE  ( sender_email= A  AND  receiver_email = B )
           OR  ( sender_email= B  AND  receiver_email = A )
        ORDER BY date
    

    【讨论】:

    • 谢谢,我觉得第二个查询没问题。但第一个总是显示一个记录。有很多用户,但它只显示一个最近发送邮件的用户。而且,当A 第一次向B 发送消息时,A 必须在列表中看到自己。 LIMIT 仅用于最多显示 30 个不同的人。并且每一行都必须显示每个人最多的信息。如果有50个人。它只显示最近发送消息的 30 个不同的人,并且每行显示他们最近的消息。
    • 我猜你需要“groupwise max”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-02
    • 2011-02-20
    • 2021-08-30
    • 2019-12-21
    • 2014-01-18
    • 2012-06-15
    相关资源
    最近更新 更多