I'm creating a chat in android studio, so I need to list the last message between the chat user and his contacts, regardless of whether he sent or received the last message.
I made the select below in mysql
and it works, but in sqlite
it returns error next to union, (I do not understand much of the syntax of sqlite
)
Code below using variable 7
SELECT mensagens.* FROM mensagens JOIN (
SELECT user, MAX(id) m FROM (
(SELECT id, id_user user, data FROM mensagens WHERE contact_user=7)
UNION
(SELECT id, contact_user user, data FROM mensagens WHERE id_user=7)
) t1 GROUP BY user
) t2 ON ((contact_user=7 AND id_user=user) OR (contact_user=user AND id_user=7))
AND (id = m) ORDER BY id DESC
My table
Selectresult