dphnn333971 2017-03-04 03:36
浏览 28

找不到每个会话的列表消息的查询

I have the following table. I want to get the last message of each conversation. Please help with a proper query to get it.

  ID   ID2   User1   User2           Message                 TimeStamp         
 ---- ----- ------- ------- ------------------------- ------------------------ 
   1     1       1       2   hii                       March, 04 2017 8:30:00  
   1     2       2       1   hey bro                   March, 04 2017 8:31:00  
   1     3       1       2   hw are u?                 March, 04 2017 8:32:00  
   1     4       2       1   good,thnks4asking         March, 04 2017 8:33:00  
   5     1       3       1   r u there?                March, 04 2017 8:34:00  
   5     2       1       3   yup,say                   March, 04 2017 8:35:00  
   5     3       3       1   hw are u?                 March, 04 2017 8:36:00  
   5     4       1       3   m fine                    March, 04 2017 8:37:00  
   5     5       3       1   where are u these days?   March, 04 2017 8:38:00  
   1     5       1       2   your most welcom :D       March, 04 2017 8:39:00

Here ID is the discussion id (chat id say for example 1 is the discussion id between users having id 1 and 2 and 5 is the for discussion between users having id 1 and 3 ) and ID2 is the number in the discussion. The resultant should show 2 messages because "your most welcome" is the last message between users having ID 1 and 2 and the 2nd message "where are u these days?" is the last message between users having ID 1 and 3

I tried many queries like

 SELECT message ,MAX(id2) 
 FROM pm 
 WHERE user1=1 OR user2=1 
 GROUP BY id

 SELECT message 
 FROM pm 
 WHERE user1=1 OR user2=1 
 GROUP BY id 
 HAVING id2=MAX(id2)

NOTE: For now I just want the last from the user (having ID=1) point of view.

  • 写回答

2条回答 默认 最新

  • doudou1438 2017-03-04 03:57
    关注

    This might be help you:

    
        select * from pm where user1 = 1 order by id desc limit 1;
    
    

    Here 'id' (you can use according to your need i.e timestamp) is your column name on basis which your will get descending order with limit 1 so that you will get your last message.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据