0
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 have selected below on mysql
and is working, but in sqlite
returns error next to Union, (I don’t know much about syntax 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
Select result
What is the error message?
– Jefferson Quesado
In mysql works perfectly, in Android sqlite returns error next to Union
– Julio Antonini
Just that? Tries to run every part of the
union
separately. I’m not sure whether to put alias in column is needed byAS alias
on sqlite, but it jumped out at me– Jefferson Quesado
Anyway, it is possible to make an equivalent consultation without
union
, using aor
in the clausewhere
and acase
in theselect
. My experience says that removing this union can make the query lighter if it is being run repeatedly– Jefferson Quesado