List the last users logged in (GROUP BY AND ORDER BY)

3

I have two tables ( user and log ), and would like to list the users by sorting by the last logged in. I'm using GROUP BY to join the logs (since a user can have more than one).

The problem occurs when I add the ORDER BY , returning error in the query:

  

Expression # 2 of SELECT list is not in GROUP BY clause and contains   nonaggregated column 'insidetv.log.data' which is not functionally   dependent on columns in GROUP BY clause; this is incompatible with   sql_mode = only_full_group_by

SQL:

SELECT nome, log.data FROM usuario JOIN log ON usuario.codigo = log.codigo_usuario GROUP BY usuario.codigo ORDER BY log.data

    
asked by anonymous 23.04.2017 / 15:52

1 answer

2

To list the last ones (in descending order), simply add the ORDER BY DESC clause, where:

DESC: Descending ASC: Ascending

By default, the language SQL uses ASC (ascending / ascending form).

SQL:

SELECT nome, max(log.data) as last_data FROM usuario JOIN log ON usuario.codigo = log.codigo_usuario GROUP BY usuario.codigo ORDER BY last_data DESC
    
28.04.2017 / 17:09