0
I have the following table sir_ter_conversa which is a conversation history.
id|idterreno|idincorporadora|idusuario|msg|data
1 |    1    |      771      |   771   | a |2018-05-27 10:20:00
2 |    1    |      771      |   773   | b |2018-05-27 10:30:00
3 |    1    |      771      |   771   | c |2018-05-27 11:20:00
4 |    1    |      771      |   773   | d |2018-05-27 11:35:00
5 |    2    |      772      |   775   | e |2018-05-27 13:25:00
idterreno is what groups conversations, id embed is the client, id user is who wrote the message that can be the user or admin (id 773).
I need to make a query that brings the grouped conversations and who made the last interaction, based on the last id or date. In this case it would bring:
id|idterreno|idincorporadora|idusuario|msg|data
4 |    1    |      771      |   773   | d |2018-05-27 10:20:00
5 |    2    |      772      |   775   | e |2018-05-27 10:30:00
I tried the option below, but the problem is that it did not bring the last id you entered, it brought the first id.
SELECT idterreno, idusuario, max(data)
FROM sir_ter_conversa
GROUP BY idterreno
In this consultation he brings Idusuario 771 and not 773:
idterreno|idusuario|max(data)
    1    |   771   |2018-05-27 10:20:00
    2    |   775   |2018-05-27 10:30:00
is coincidence or the higher the
idgreater thedataALWAYS?– rLinhares
yes, the biggest id will always be the biggest date
– Leandro Marzullo
adds the
ORDER BY id descto see if it solves :P– rLinhares
the greatest id will always be the greatest date, not the greatest of all, will be the greatest within each "idterreno"
– Leandro Marzullo
I did it, but the problem is that it brings idusuario from the first line added, not from the last
– Leandro Marzullo