Doubt with SELECT in MYSQL database

1

I need an sql that, in the table below, always brings the last insert for each user. I tried with distinct but it did not work. So in the case below sql would bring the results of line 2 and 5. Detail that there are more columns (city, country, address, etc ...) and that need to appear in the final result.

DATA----------------------ID---NOME

2017-07-17 16:37:38  || 1   || Lucas    
2017-07-17 16:38:03  || 1   || Lucas    
2017-07-17 16:38:37  || 2   || Juliana  
2017-07-17 16:38:47  || 2   || Juliana  
2017-07-17 16:39:00  || 2   || Juliana 

After so much searching I found nothing. This table will have more insertions with different users (always 1 id for each user) but only exemplified to understand.

    
asked by anonymous 17.07.2017 / 23:00

2 answers

1

The query below solves your problem only replace tabela with your table name

SELECT t1.*
FROM tabela t1
WHERE t1.data = (SELECT MAX(t2.data)
FROM tabela t2
WHERE t2.nome = t1.nome)

Table used for testing

QueryResult

    
18.07.2017 / 01:22
1

I can not get the Motta link from working, but it should be quoting the MAX() function. Just completing the query would look like this:

SELECT max(data), id, nome, cidade, pais, endereco
FROM tabela
GROUP BY id, nome, cidade, pais, endereco
ORDER BY id

For the other fields in the table, you must add each of them in both% w and% w.%.

    
18.07.2017 / 00:02