• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Combining Multiple Inner Joins with a Left Join

 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I'm trying to write a query which is going to combine values from 4 tables. Three of those tables will be combined using Inner Joins. However, the fourth table must be combined used a left join as there may or may not be an index for a record in that table. Here's my query (well, a simplified version of it, anyway) using only inner joins:



What I'd like to do is something similar to the following:



Unfortunately, that doesn't seem to be valid SQL and I'm no SQL expert to figure out the best way to go about this.

The trick here is that a.YetAnotherForeignKey may be null. If it is not null, I want to pull the information from "additionalTable". If it is, that's just fine.

Anyone have a good way to do this in a single query? I can handle inner joins and I can handle left joins, but I can't say I've ever tried to put the two together quite like this.

Thanks.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the trouble, but I think I may have actually worked out a solution. Using a different syntax, I came up with this:



That seems to be giving me the data that I want. Anyone see any issues with that query, or am I in the clear?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks good Corey!
 
Ranch Hand
Posts: 472
Objective C Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it looks right.
 
reply
    Bookmark Topic Watch Topic
  • New Topic