Does anyone know how I could list the last message of each user?
I was able to do this but list the last message of only one user, and I wanted it to list everyone who sent the message to the user in question (code 3).
SELECT m1.codigo,
m1.codigo_remetente,
usuario.url,
m1.descricao,
m1.data,
m1.visualizado,
usuario.nome,
usuario.sobrenome
FROM mensagens m1
JOIN usuario ON m1.codigo_remetente = usuario.codigo
AND m1.codigo_destinatario = 3
AND data = (SELECT MAX(data)
FROM mensagens m2
WHERE m2.codigo_remetente = m1.codigo_remetente)
ORDER BY data DESC
User table: code, name, surname, url.
Message table: code, repository_code, diskette_code, description, date