I have a MYSQL table like this example:
[ID]--[Date]--[UserID]--[Username]--[String]
1 12-10-06 02:35:01 25 Jack string1
2 12-10-06 02:38:01 25 Jack string2
3 12-10-06 02:40:01 25 Jack END
4 12-10-06 02:42:01 36 Jill string1
5 12-10-06 02:45:01 36 Jill string2
6 12-10-06 02:48:01 36 Jill END
7 12-10-06 02:50:01 25 Jack string1
8 12-10-06 02:52:01 25 Jack string2
9 12-10-06 02:55:01 25 Jack END
I’m using this MYSQL query to group them:
SELECT m1.Date, m1.Username, m1.UserID
FROM Username m1 LEFT JOIN Username m2
ON (m1.UserID = m2.UserID AND m1.ID < m2.ID)
WHERE m2.ID IS NULL
ORDER BY Date DESC
It works but the problem with this query is that it only gives the last entry of each user as the output:
6 12-10-06 02:48:01 36 Jill
9 12-10-06 02:55:01 25 Jack
I want to get all sessions as the output, like:
12-10-06 02:35:01 - 12-10-06 02:40:01 25 Jack
12-10-06 02:42:01 - 12-10-06 02:48:01 25 Jill
12-10-06 02:50:01 - 12-10-06 02:55:01 25 Jack
How can I accomplish this with PHP and MYSQL?
Well, I doubt you ever want to query the entire database for sessions, so for the chunk you want, I think I would just fetch the end, and then for each one of them request a different.
Do something like this
And then for each one of those entries you can do
Get when the last session from the user ended:
Get the min date between those 2 dates:
It shouldn’t hammer the database too much, just build appropriate indexes and limit your queries to what it’s really useful rather than just presenting a full report on all sessions for no reason.