我正在尝试创建与Facebook或whatsapp相同的消息对话列表或消息列表,我的记录保存在这样的消息表中。
这里保存ToprofileId(用户ID)和FromProfileId(用户ID) 用户206向用户214发送消息(消息id515),用户214只回复用户206(消息id516)。 消息由用户200发送到206(messageid 517) 用户206向184发送另一个消息(消息代码517)。 现在我想显示为对话标题。 (用户:214-206或206-214)作为单行 如果他们也进行对话,他们也一样。
假设我的用户ID是206,那么如果我收到任何消息或发送任何消息,它应该作为单个对话。
在此对话中显示最后一条消息。
剩余看起来像
消息列表页面看起来像Facebook页面
MsgID |toId|fromID |isR|isF|isAl|Message|DateTime |TargetUser
515 |214 |206 |1 |0 |0 |hiiii |2013-12-26 12:19:51 |214
516 |206 |214 |0 |0 |0 |hello, |2013-12-26 12:21:44 |214
517 |206 |200 |1 |0 |0 |message|2013-12-26 12:22:59 |200
518 |184 |206 |0 |0 |0 |message|2013-12-26 14:52:30 |184
519 |200 |206 |0 |0 |0 |1a |2013-12-26 16:11:58 |200
520 |200 |206 |0 |0 |0 |2b |2013-12-26 16:12:02 |200
521 |200 |206 |0 |0 |0 |3a |2013-12-26 16:12:04 |200
531 |200 |206 |1 |0 |0 |13 |2013-12-26 16:12:24 |200
532 |206 |200 |0 |0 |0 |14 |2013-12-26 16:12:34 |200
我需要基于唯一TargetUsers的所有行,结果应该遵循
MsgID |toId|fromID |isR|isF|isAl|Message|DateTime |TargetUser
516 |206 |214 |0 |0 |0 |hello, |2013-12-26 12:21:44 |214
517 |206 |200 |1 |0 |0 |message|2013-12-26 12:22:59 |200
518 |184 |206 |0 |0 |0 |message|2013-12-26 14:52:30 |184
答案 0 :(得分:0)
如果我理解你,那么你想要一个SQL查询来获取一个用户的所有转换及其日期,以便将其显示为你给出的图片:
执行此查询:
Select FromProfileId, Message, DateTime
From MessagingTable
Where ToProfileId = 206
And MessageId in ( Select Max(MessageId) From MessagingTable Group By FromProfileId)
它会像这样显示:
FromProfileId. | Message | DateTime
214 | "hiii" | 2013-12-26 10:22
201 | "brb" | 2013-12-26 10:15
这是你想要的吗?
答案 1 :(得分:0)
搜索用户206时,您可以执行以下操作:
Select Max(MessageId), PartnerId
From
(
Select MessageId,
PartnerId = FromProfileId
From MessagingTable
Where ToProfileId = 206
Union All
Select MessageId,
PartnerId = ToProfileId
From MessagingTable
Where FromProfileId = 206
) Messages
Group By PartnerId
答案 2 :(得分:0)
感谢您的回复,我找到了解决此问题的方法,
select *, ROW_NUMBER() over(order by outerRow)as Row from
(select * ,Row_Number() over (partition by a.TargetUser order by a.datetime desc)as
outerRow from
(select *, case when ToProfileID=206 then FromProfileID when
FromProfileID=206 then ToProfileID end as TargetUser from Message
where ToProfileID=206 or FromProfileID=206)a)b where b.outerRow=1