Page 1 of 1

Grouping with date sorting

Posted: Sat Oct 10, 2015 7:28 am
by WorldCom
Hi all,
I have a table of user ID's and date/time of sites visited.
I was trying the DISTINCT function to just get one user_id but I have a problem sorting the last visited date.
I opted for the max(date) and Group By function which does what I want except for the sorting of the date.

Here's the code I'm now using.

Code: Select all

$query = "SELECT user_id, max(date) FROM `timeTracker` Group By user_id ORDER BY date DESC"
user_id max(date)
1 2015-10-10 07:14:57
531 2015-10-01 08:48:11
535 2015-10-08 11:46:34

It just seems to sort the user_id from low to hi.

If I take out the DESC I get:

user_id max(date)
535 2015-10-08 11:46:34
531 2015-10-01 08:48:11
1 2015-10-10 07:14:57

This is just the opposite.

I really want to sort the dates from the latest date.
Thanks for any help.

Re: Grouping with date sorting

Posted: Sat Oct 10, 2015 7:34 am
by WorldCom
Dah on me ... just fixed it.

Code: Select all

SELECT user_id, max(date) FROM `timeTracker` Group By user_id ORDER BY max(date) DESC
user_id max(date) Descending
1 2015-10-10 07:14:57
535 2015-10-08 11:46:34
531 2015-10-01 08:48:11

I guess making this post gave me a minute to think. :roll: