rockstardev
rockstardev

Reputation: 13527

MySQL Query help with hit statistics?

TABLE: pages
pageId | name
 1     | Home
 2     | About Us
 3     | Contact Us

Then i have stats stored:

TABLE: hits
hid | created    | assignId
01  | 1000000001 | 1
02  | 1000000002 | 1
03  | 1000000003 | 3

So basically, "Home" was opened twice, "About us" never, and "Contact us" once. Also notice that Contact us was most recently accessed.

I need a query that returns this:

pageId | name         | lastAccessDate
 1     | Home         | 1000000002   --> NB: Notice that it takes the higher one
 2     | About Us     | null
 3     | Contact Us   | 1000000003

Anybody know how to do this?

Upvotes: 3

Views: 68

Answers (1)

cichy
cichy

Reputation: 10644

This should work

SELECT p.pageId, p.name, MAX(s.created) FROM pages p LEFT JOIN stats s ON p.pageId=s.assignId GROUP BY p.pageId ORDER BY pageid ASC

Upvotes: 4

Related Questions