Grouping with date sorting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
WorldCom
Forum Commoner
Posts: 45
Joined: Sat Jun 24, 2006 8:14 am
Location: Ontario, Canada

Grouping with date sorting

Post 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.
WorldCom
Forum Commoner
Posts: 45
Joined: Sat Jun 24, 2006 8:14 am
Location: Ontario, Canada

Re: Grouping with date sorting

Post 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:
Post Reply