sql - retrieve the last record when grouping mysql table -


i have table t_message, collect conversation beetween user , user.

here's table strcture.

 id_message sender_user_id receiver_user_id message  1          2              1                test 1 2          1              2                test 2 3          2              1                test 3 4          1              2                test 4

with table, have objective collect group of conversation user, , example want colletct groups of conversation beetween user 1 , user 2. using query

select * ( select * t_message receiver_user_id = 1 order id_message desc ) group sender 

and result is

 id_message sender_user_id receiver_user_id message  3          2              1                test 3 

it's showing group of conversation beetwen user 1 , user 2, not showing conversation beetwen user 1 , user 2. showing last conversation user 2 or sender.

what query need collect group of conversation beetwen user, , retrieve last record of conversation. suggestion?

oh way, sory bad english, hope got idea.

you can concat sender , receiver form conversation

select a.id, a.conversation, b.sender_user_id, b.receiver_user_id, b.message (   select max(id_message) id,          if(sender_user_id>receiver_user_id,             concat(receiver_user_id,',',sender_user_id),             concat(sender_user_id,',',receiver_user_id)) conversation   t_message   group conversation) join t_message b on a.id = b.id_message; 

fiddle


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -