Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 =
 AND ul.createddate = ( SELECT MAX(i.creteddate) FROM UserLogs i WHERE i.UserID = u.ID )

Open in new window


Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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…

722 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