mysql - How to get last record for group? -


i have table called tbl_chat , tbl_post. tbl_chat given follows.

|--------------------------------------------------------------------------------| |  chat_id  |  message  |  from_user  |  to_user  |  post_id  |send_date         | |--------------------------------------------------------------------------------| |    1      |   hi      |     23      |         |    35     |  2016-04-01 17:35| |    2      |   test    |     24      |         |    35     |  2016-04-02 01:35| |    3      |   thut    |           |   23      |    35     |  2016-04-02 03:35| |    4      |   test    |           |   24      |    35     |  2016-04-02 12:35| |    5      |   hi      |     23      |         |    35     |  2016-04-03 17:35| |--------------------------------------------------------------------------------| 

now, in chat table can see 3 users interacting each other. admin (a), user id = 23 , user = 24.

so there 2 chat thread.

  1. one between , 23
  2. another between , 24.

i want query show 2 chat threads, last chat message. in case of facebook chat list showing chat-threads mentioning last chat.

i writing query this.

select * tbl_chat, tbl_post tbl_post.post_id = tbl_chat.post_id , tbl_post.post_id = '39' group tbl_chat.chat_from order date desc  

the query has problem. first retrieving chats , grouping w.r.t. chat_from , ordering descending-wise.

so first it's creating group, , ordering group.

also, first query produces 3 group, taking reply message admin separate group. since group chat_from.

how can solve issue?

edit:- grateful if can build query in active-records of codeigniter.

drop table if exists my_table;  create table my_table (chat_id int not null auto_increment primary key ,message varchar(20) not null ,from_user varchar(12) ,to_user varchar(12) ,post_id int not null ,send_date datetime not null );  insert my_table values (1,'hi'  ,'23','a' ,35,'2016-04-01 17:35:00'), (2,'test','24','a' ,35,'2016-04-02 01:35:00'), (3,'thut','a' ,'23',35,'2016-04-02 03:35:00'), (4,'test','a' ,'24',35,'2016-04-02 12:35:00'), (5,'hi'  ,'23','a' ,35,'2016-04-03 17:35:00');   select a.*    my_table   join       ( select least(from_user,to_user) user1             , greatest(from_user,to_user) user2             , max(send_date) send_date           my_table          group             user1             , user2      ) b     on b.user1 = least(a.from_user,a.to_user)    , b.user2 = greatest(a.from_user,a.to_user)    , b.send_date = a.send_date;  +---------+---------+-----------+---------+---------+---------------------+ | chat_id | message | from_user | to_user | post_id | send_date           | +---------+---------+-----------+---------+---------+---------------------+ |       4 | test    |         | 24      |      35 | 2016-04-02 12:35:00 | |       5 | hi      | 23        |       |      35 | 2016-04-03 17:35:00 | +---------+---------+-----------+---------+---------+---------------------+ 

Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -