Mysql - Order before grouping

0

I need to make a selection that will bring the last users who logged in without repeating the names. The problem is that the command below first groups and then sorts:

SELECT usuario FROM tab_logins GROUP BY usuario ORDER BY data_login  desc

Table example:

usuario, data_login
joão,    2018-01-01
maria,   2018-01-02
jose,    2018-01-03
joão,    2018-01-04
antonio, 2018-01-05
joão,    2018-01-06

Using the command I mentioned. First it will group the names, and then it will sort, bringing the wrong result:

antonio
jose
maria
joao

Since the correct result would be this:

joão
antonio
jose
maria

Does anyone know how to solve it?

    
asked by anonymous 11.03.2018 / 17:47

1 answer

0

You can solve this as follows:

SELECT usuario
FROM tab_logins 
GROUP BY usuario 
ORDER BY MAX(data_login) desc;

The only difference is the application of the MAX ()

    
11.03.2018 / 18:20