SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Joining 3 tables with the 3rd as optional
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Eagle_f90
Constraint Violating Yak Guru

USA
377 Posts

Posted - 05/24/2012 :  07:03:11  Show Profile  Visit Eagle_f90's Homepage  Reply with Quote
I have 3 tables I want to join together. The first 2 will always have data to show but the 3rd may or may not. I am new to joins and am not sure how to make a 3 table join where it will always show the data from the first 2 tables and only show the data in the third if there is any. Here is the code I has so far, the optional table in this case is the FilePaths table

	SELECT si.SectionID, si.ShortName, si.LinkName, r.RaceID, r.RaceName, r.RaceDescription, r.RaceImagePathID, p.FilePath + p.Name AS FullPath
	FROM dbo.SectionInfo AS si WITH(NOLOCK)
	INNER JOIN dbo.RaceInfo AS r WITH(NOLOCK) 
    ON r.SectionID = si.SectionID
    INNER JOIN dbo.FilePaths as p WITH (NOLOCK)
    ON p.PathID = r.RaceImagePathID


--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia

Lumbago
Norsk Yak Master

Norway
3241 Posts

Posted - 05/24/2012 :  07:28:30  Show Profile  Reply with Quote
Change the last join to a LEFT OUTER JOIN instead:

SELECT si.SectionID, si.ShortName, si.LinkName, r.RaceID, r.RaceName, r.RaceDescription, r.RaceImagePathID, p.FilePath + p.Name AS FullPath
FROM dbo.SectionInfo AS si WITH(NOLOCK)
  INNER JOIN dbo.RaceInfo AS r WITH(NOLOCK) 
    ON r.SectionID = si.SectionID
  LEFT OUTER JOIN dbo.FilePaths as p WITH (NOLOCK)
    ON p.PathID = r.RaceImagePathID


- Lumbago
My blog-> http://thefirstsql.com
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.12 seconds. Powered By: Snitz Forums 2000