select a.id, a.from, a.content, a.addtime from (select * from `table` where to = 'a' order by addtime desc) a group by a.from

解决方案 »

  1.   

    试了一下,结果是个空数组啊。 to = 'a' 这里我不太理解,外部a应该是个集合吧。
      

  2.   

    select *
    from table结构如下 v
    where (`from`='a' or to='a')
    and not exists (select 1 from table结构如下 where (`from`='a' or to='a') and (to=v.to or to=v.from) and id>v.id)
      

  3.   

    谢谢版主支持!
    这个试了一下,具体操作句子是这样select * from __TABLE__ v where (c_from='a' or c_to='a') and not exists (select 1 from __TABLE__ where (c_from='a' or c_to='a') and (c_to=v.c_to or c_to=v.c_from) and c_id >v.c_id)有结果出来了,但是是所有与a聊天的记录,依然有重复。
      

  4.   

    select *
    from tb  v
    where (`from`='a' or to='a')
    and not exists (select 1 from tb where (to=v.to or from=v.from or to=v.from or from=v.to) and id>v.id)
      

  5.   

    用子查询,按来源用户分组并找出该最大的时间对应的序号,按此序号查详细字段。
    select * from  table 
    where id in (
      select id,max(add_time) as c_id from table as T1 where T1.from = 'A' group by to
    )
      

  6.   

    select * from  __TABLE__ where c_id in (select max(c_id) as c_id  from __TABLE__ as T1 where T1.c_from = 'a' or T1.c_to = 'a' group by  (case when T1.c_to <= T1.c_from then CONCAT(T1.c_to , T1.c_from) else CONCAT(T1.c_from , T1.c_to) END))
    最后结果,分享一下!