I need to do a SQL query where I return the last messages exchanged between users (my account for example) and who I talked to ... regardless of whether the last conversation was sent or received.
The code that I am using sends the sent and received in the case repeating the user, I would like to know how I do not repeat grouping only my last conversation with whom I spoke independently if the message was sent or received.
SELECT distinct * FROM mensagens inner join usuarios on de = id_user where para LIKE '$sessao'
union SELECT distinct * FROM mensagens inner join usuarios on para = id_user where de LIKE '$sessao'
group by id_user order by id asc
Messages table:
id - de - para - mensagem
Users table:
id_user - nome