Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Left Outer Join and Non Existent Records

Author  Topic 

robertc
Starting Member

7 Posts

Posted - 2011-09-27 : 15:43:12
Hi,

I have two tables that I'm working with.

tblUsers
userid

tblSheets
userid
sheetid


I'm trying to grab all records from tblUsers regardless if there are entries for that user in tblSheets.

SELECT TOP 10240 d.userID,s.userID as inSheet FROM tblUsers u LEFT OUTER JOIN tblSheets s ON u.userID = s.userID WHERE s.sheetid = 676



The code above is only bringing back results where users are actually in the sheetid of 676. But i'd like to bring them all back, and if they are not in sheet 676 then inSheet would contain null. If they were then inList would just have their userID so I could run test in my web app.



TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2011-09-27 : 15:45:54
[code]
SELECT TOP 10240 d.userID,s.userID as inSheet
FROM tblUsers u
LEFT OUTER JOIN tblSheets s
ON u.userID = s.userID
and s.sheetid = 676[/code]

Be One with the Optimizer
TG
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-27 : 23:17:40
and the reason is this if you're curious to know

http://weblogs.sqlteam.com/jeffs/archive/2007/05/14/criteria-on-outer-joined-tables.aspx



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -