我有一张桌子用来存储对话的消息。这是我的查询:
SELECT
a.id,
a.`from member_id`,
a.`to member_id`,
IF(a.`from member_id`={$targetID}, a.`to member_id`, a.`from member_id`) as other_id,
a.text,
MAX(a.`date sent`) as `date sent`,
a.to_read,
m.`first name`,
m.`last name`,
m.avatar_name,
m.avatar_status
FROM message a
JOIN members m on IF(a.`from member_id`={$targetID}, a.`to member_id`, a.`from member_id`) = m.id
WHERE (a.`from member_id`={$targetID} OR a.`to member_id`={$targetID}) AND a.active=1
GROUP BY IF(a.`from member_id`={$targetID}, a.`to member_id`, a.`from member_id`)
ORDER BY `date sent` DESC
消息表如下:
id int(11) id of the message
from member_id int(11) id of the person the message was sent from
to member_id int(11) id of the person the message was sent to
date sent datetime date of when it was sent
active tinyint(1) if the message is deleted
text longtext the text of the message
from_read tinyint(1) boolean to know if the person who sent it read it
to_read tinyint(1) boolean to know if the person who it got sent to read it
此 select 语句用于显示您当前拥有的对话列表。例如,这正是您在 Android 智能手机上单击短信图标时获得的活动,您会在其中看到对话列表,每个对话都有您和另一个人之间交换的最新消息。
因此,我在 select 语句中所做的是获取您的 id 在to
or中的消息from
,然后将其分组,以便使用的行是您和其他人之间交换的最新消息。
问题是,当一个人向自己发送文本时,最新的文本不会出现。
有谁知道是什么问题?
谢谢。