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 | Conta开发者_开发技巧ct Us | 1000000003
Anybody know how to do this?
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
精彩评论