Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Problem with INNER JOIN

Posted on 2008-06-26
Medium Priority
Last Modified: 2008-06-26
Hi all,
I have Two tables : "Users" & "UserLogs"
The table Users has all the common fields (ID, Name, Email, etc....)
The table UserLogs has: UserID, UserLogTypeID, CreatedDate.

The point is that if a user for example LogsOn to the website, the system will insert to the UserLogs Table this row:
UserID: 29 (just for the example)
UserLogTypeID: 1 (1 means "LogOn", 2 means "LogOff", etc......)
CreatedDate = now() / GatDate() / etc... :)

If I want to see what is the last time the user was logged on, I make this query:

SELECT     TOP (1) CreatedDate
FROM          UserLogs
WHERE      (UserLogTypeID = 1) AND (UserID=29)

Until here it's all good.

But, I want to pull all the users table and insert an extra field called LastLoggedOn.
I try to do this query but I get the results for only the last user and all the others are NULL:

SELECT     Users.ID, Users.Email,  Users.FirstName, Users.LastName, tblLastLogon.CreatedDate AS LastLogon
                          (SELECT     TOP (1) CreatedDate, UserID
                            FROM          UserLogs
                            WHERE      (UserLogTypeID = 1)
                            ORDER BY CreatedDate DESC) AS tblLastLogon ON Users.ID = tblLastLogon.UserID

Please tell me how to fix it.
Question by:giligatech
1 Comment
LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 2000 total points
ID: 21873258
here we go:
SELECT     u.ID, u.Email,  u.FirstName, u.LastName, ul.CreatedDate AS LastLogon
FROM         Users u
  ON ul.userid = u.id
 AND ul.createddate = ( SELECT MAX(i.creteddate) FROM UserLogs i WHERE i.UserID = u.ID )

Open in new window


Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Sometimes MS breaks things just for fun... In Access 2003, only the maximum allowable SQL string length could cause problems as you built a recordset. Now, when using string data in a WHERE clause, the 'identifier' maximum is 128 characters. So, …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question