在数据库中获取对话

时间:2013-11-05 18:16:02

标签: java mysql sql

我有这张桌子:

ofmessagearchive (fromID, toID, message, sentDate)

我需要获取确定用户的每个会话的最后10条消息。

实施例: UserA有两个朋友(UserB和UserC)

我需要获取与UserB的最后10条对话消息,以及与UserC对话的最后10条消息。

1 个答案:

答案 0 :(得分:1)

我担心如果没有函数你就无法在SQL中获得它。

您可以使用此计划使用函数或java函数获取所有内容

1)获取用户A的所有用户

  select distinct id from 
   (Select distinct toid as id from ofmessagearchive where fromID=UserA union
   ( select distinct fromid as id from ofmessagearchive where toID=UserA) ) as ids;

2)浏览该列表中的每个ID,然后选择最后10条消息/打印它。

相关问题