sql - MySQL chat system to display last message of sender/receiver AND name of other person -
i'm developing chat system , conversation list must display 2 things:
- last message of whoever sent message (me a.k.a. current user, or other user)
- name of other user
the part i'm having issues second point. current query displays last message of each conversation, in cases me (current user) sent last message, instead of name should displaying other user's name.
select sql_calc_found_rows u.id_user id, i.id_user_from, i.id_user_to, u.name name, unix_timestamp(i.date_msg) date_msg, i.message msg inbox inner join user u on (u.id_user = i.id_user_from or u.id_user = i.id_user_to) id_msg in (select max(id_msg) id ( select id_msg, id_user_from id_with inbox id_user_to = 1 union select id_msg, id_user_to id_with inbox id_user_from = 1) t group id_with ) order i.id_msg desc
in example, i'm andufo (id_user = 1). here's sqlfiddle link if helps. thanks!
may helps
select sql_calc_found_rows u.id_user id, i.id_user_from, i.id_user_to, u.name name, unix_timestamp(i.date_msg) date_msg, i.message msg inbox inner join user u on u.id_user = if(i.id_user_from = 1 /*me*/, i.id_user_to, i.id_user_from) id_msg in (select max(id_msg) id ( select id_msg, id_user_from id_with inbox id_user_to = 1 union select id_msg, id_user_to id_with inbox id_user_from = 1) t group id_with ) order i.id_msg desc
Comments
Post a Comment