Solved

Help with SQL Syntax (Join Issue)

Posted on 2011-03-22
2
272 Views
Last Modified: 2012-05-11
I need some help wtih the following SQL query. It works find if there is at least one row/result in each of the joined tables. I discovered that on the first two joins dbo.objects_geography and dbo.object_geography can have nulls on a given o.id in the WHERE clause.

How do I fix this so that even if dbo.objects_geography and dbo.object_geography have nulls in them, it will still return the rest of the data?

I'm thinking I need to modify my joins, but realize I need some help in this area.

Thanks.
SELECT o.id, o.title, o.display_date, o.medium, o.dimensions, o.credit_line, o.invno, 
og2.country, og2.city, m.filename, m.primary_display
FROM dbo.objects o
INNER JOIN dbo.objects_geography og1 ON o.id = og1.object_id 
INNER JOIN dbo.object_geography og2 ON og2.id = og1.geography_id
INNER JOIN dbo.objects_media om ON o.id = om.object_id
INNER JOIN dbo.media m ON om.media_id = m.id
WHERE o.id = 4250893

Open in new window

0
Comment
Question by:greddin
2 Comments
 
LVL 5

Accepted Solution

by:
simonpaul64 earned 500 total points
ID: 35194028
SELECT o.id, o.title, o.display_date, o.medium, o.dimensions, o.credit_line, o.invno,
og2.country, og2.city, m.filename, m.primary_display
FROM dbo.objects o
LEFT OUTER JOIN dbo.objects_geography og1 ON o.id = og1.object_id
LEFT OUTER JOIN  dbo.object_geography og2 ON og2.id = og1.geography_id
INNER JOIN dbo.objects_media om ON o.id = om.object_id
INNER JOIN dbo.media m ON om.media_id = m.id
WHERE o.id = 4250893
0
 

Author Closing Comment

by:greddin
ID: 35194134
Thanks for the help! Works great now.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now