Cant figure out the query

This is a discussion on "Cant figure out the query" within the Databases section. This forum, and the thread "Cant figure out the query are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > Databases

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Mar 1st, 2006, 18:53
New Member
Join Date: Mar 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Cant figure out the query

Here's the deal. I've got 2 tables:

user
==========
int uid
varchar firstname
varchar lastname

log
==========
int uid
datetime date
varchar page

Every time someone accesses a page on my system, if they're logged in, I create an entry in log with their uid and the url of the page they're accessing.

Now, I'd like to see a list of the last 5 peoples names to access the site. I can't figure out how to write a query to get that.

Sample Data:

user
===============
233 Joe Blow
1150 Tom Sawer
429 Mike Christy

log
===============
233 3/1/2006 7:54:39 AM /trips/payments.asp
233 3/1/2006 7:54:44 AM /trips/whosgoing.asp
233 3/1/2006 7:54:48 AM /login.asp
1150 3/1/2006 8:34:25 AM /trips/payments.asp
1150 3/1/2006 8:34:28 AM /trips/whosgoing.asp
1150 3/1/2006 8:34:36 AM /trips/payments.asp
429 3/1/2006 9:27:22 AM /profile.asp
429 3/1/2006 9:27:35 AM /trips/Default.asp
429 3/1/2006 9:27:38 AM /default.asp


So, what I'd like is:

result
========================
3/1/2006 9:27:38 AM Mike Christy /default.asp
3/1/2006 8:34:36 AM Tom Sawyer /trips/payment.asp
3/1/2006 7:54:48 AM Joe Blow /login.asp


Hope that makes sense.

Thanks in advance for the help.

- O
Reply With Quote

  #2 (permalink)  
Old Apr 26th, 2006, 20:18
spinal007's Avatar
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 22
Posts: 1,620
Blog Entries: 1
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via ICQ to spinal007 Send a message via MSN to spinal007 Send a message via Yahoo to spinal007 Send a message via Skype™ to spinal007
Re: Cant figure out the query

I assume you're using MSSQL...

Join the tables and use the DISTINCT keyword:

SELECT TOP 5 DISTINCT
U.uid, U.firstname, U.lastname,
L.date, L.page
FROM [log] L LEFT JOIN [user] U ON L.uid = U.uid
ORDER BY L.date DESC
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
Reply With Quote
  #3 (permalink)  
Old Apr 26th, 2006, 20:57
New Member
Join Date: Mar 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Cant figure out the query

thanks for the reply, but its not that simple (if it was, i wouldn't have posted

the problem with the query that you provided is that, since i'm using a datetime field, every single entry is unique. so if i have 1 user browse the system and go through 5 pages, i'll just see that user name 5 times (since each datetime is unique).

i actually got this problem solved over at the devshed forums. here was the final solution:

SELECT TOP 5
L1.yourdate,
CONCAT(firstname lastname) AS name
L1.page
FROM
user
INNER JOIN log AS L1
ON user.uid=L1.uid
INNER JOIN log AS L2
ON L1.uid=L2.uid
GROUP BY
L1.yourdate,
name,
L1.page
HAVING L1.yourdate=MAX(L2.yourdate)
ORDER BY L1.yourdate DESC
Reply With Quote
  #4 (permalink)  
Old Apr 26th, 2006, 21:02
spinal007's Avatar
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 22
Posts: 1,620
Blog Entries: 1
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via ICQ to spinal007 Send a message via MSN to spinal007 Send a message via Yahoo to spinal007 Send a message via Skype™ to spinal007
Re: Cant figure out the query

yep, that works!
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
Reply With Quote
Reply

Tags
figure, query

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to figure out about keeping chat in... mcdanielnc89 Web Page Design 4 Sep 13th, 2007 17:31
Cant figure out senton and response Foobster ASP.NET Forum 0 Aug 15th, 2007 10:01
Rendering Problems in IE (go figure) Zonglars JavaScript Forum 2 Jul 22nd, 2007 10:46
Simple problem that i cant figure out!! robukni Flash & Multimedia Forum 2 Apr 26th, 2007 23:25


All times are GMT. The time now is 05:06.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43