我有一个 MESSAGE 表。
它的列是
|序号 |发件人邮箱 | receiver_email |内容 |日期 | is_read | sender_profile_icon_url|
在我的 Android 应用中,我想首先显示用户列表。
你可以想象一下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 的 View ):
左:显示至少发送或接收过一条消息的用户。
右图:显示点击第一个用户(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在两边的MessageUserList Activity上。它需要分别是一个(例如 A 看到 B,B 看到 A.)。而如果A点击了列表的A用户,那么就会带来错误的数据。
我不知道我需要修复什么。有什么问题?
最佳答案
要找到“最后”的电子邮件:
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
关于android - MariaDB & Android - 实现消息功能,分别选择用户及其消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51291701/