Jump to content

[SOLVED] group by order by


smerny

Recommended Posts

I have a table with fields ID, ID_user, action, ip, time

 

what I want is to take the rows with the highest ID, one per ID_user...

 

so if the table is like..

 

ID, ID_user, action, ip, time

1, 1, ...

2, 1, ...

3, 1, ...

4, 3, ...

5, 3, ...

6, 1, ...

7, 2, ...

8, 2, ..

 

 

it should take the rows

8, 2, ...

6, 1, ...

5, 3, ...

Link to comment
Share on other sites

no thats not it...

 

what I tried was "SELECT * FROM tracklog GROUP BY ID_user ORDER BY ID DESC"

 

but the results were like...

 

7,2, ...

4,3, ...

1,1, ...

 

(it's grouping them into the lowest ID for each ID_user and then sorting those DESC, how do I get it to group them into the highest ID for each ID_user?)

Link to comment
Share on other sites

If you just want the highest ID for each ID_user and don't care about getting the  other columns from the row with that highest ID, you can do the following -

 

SELECT max(ID), ID_user, action FROM your_table GROUP BY ID_user

The above will give you values for the remaining columns of the first row encountered for each ID_user, not the values from the row with the highest ID for each ID_user.

 

However, if you want to directly retrieve the row that corresponds to the highest ID for each ID_user, you will need to use one of the methods shown at this link - http://mysql.proserve.nl/doc/refman/5.1/en/example-maximum-column-group-row.html

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.