MySQL arranges order data in two ways
I am tracking pageviews in modules using the following table:
id | page_id | user_id | session_id | created
Each page is unique to a specific module; therefore page 14 exists, for example, only in module 2.
I'm trying to write a query that tells me when a browsing session (GROUP BY session_id) has started for a specific module, but I'm having problems with my order. Basically, for this grouped session, I want to return the earliest "created" (the time the browsing session started) and the last page_page (how far they got into the module).
What's the best way to do this?
+1
a source to share
2 answers
I think you will need to use subqueries in select:
SELECT a.session_id, MIN(a.created) as start_time,
b.page_id AS last_page, b.end_time
FROM table a
INNER JOIN
(SELECT b.session_id, MAX(b.created) as end_time, MAX(b.page_id)
FROM table b GROUP BY b.session_id)
ON a.session_id = b.session_id GROUP BY a.session_id
+3
a source to share