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

Asked

Viewed 142 times

3

I have two tables (user and log), and would like to list users ordering by last logged in. I am using the GROUP BY to join the logs (since a user may own more than one).

The problem occurs when I add the ORDER BY, returning error in 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
  • 4

    It would be more interesting, even to suit the format of this site, you post the solution as a response.

1 answer

2


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

DESC: Descending
ASC: Ascendant

By default, the language SQL uses the 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
  • Diego, it would be interesting if, in addition to the answer itself, you pointed out where the problem was and also, if possible, explained how you found the solution. ;)

  • I took the liberty of editing your reply. If you think that was exaggerated or that is not correct, I ask you to edit again and it is my apology.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.