Solved

SQL Inner Join Question

Posted on 2012-03-27
6
323 Views
Last Modified: 2012-03-28
I have an SQL Statement that joins Table A - "Products" with Table B - "Brands" based on a BrandID.  

Brand information isn't required.  so in many cases, there is no BrandID in the Products Table.  The value for BrandID in the Products Table is 0 when no Brand is assigned.

For those products that do have a BrandID, I need to grab the related BrandName .  For those that don't have a BrandID, I still need them included in the results.

My statment looks like this:

"Select products.id, products.desc, products.details, brands.BrandName, products.IDBrand  from products inner join Brands on brands.BrandID = products.BrandID"

I'm hoping to return all the products in the database, but this will only return those that can be joined with the Brand table.  I'm not sure how to work around this.

Any ideas?

Thanks for your help!!!
0
Comment
Question by:slightlyoff
6 Comments
 
LVL 39

Expert Comment

by:Kyle Abrahams
ID: 37773423
Select products.id, products.desc, products.details, brands.BrandName, products.IDBrand  from products left join Brands on products.BrandID = brands.BrandID
0
 
LVL 19

Accepted Solution

by:
Daniel Van Der Werken earned 250 total points
ID: 37773443
You need to do an OUTER JOIN like LEFT OUTER JOIN instead of INNER JOIN for this to work. Inner joins only get the information where the ID's exist in both tables. If you want to have it where the ID doesn't exist in the other table, then you need the OUTER JOIN.

Replace INNER JOIN with LEFT OUTER JOIN and I think you'll get the results you want.
0
 
LVL 8

Assisted Solution

by:gpizzuto
gpizzuto earned 250 total points
ID: 37773476
you should use the LEFT OUTER JOIN command:

Select products.id, products.desc, products.details, brands.BrandName, products.IDBrand  from products LEFT OUTER JOIN Brands on brands.BrandID = products.BrandID

Is not a good idea to put the 0 value where there is no Brand. Use instead the NULL value:
better to set up a RELATION between the 2 tables (much safer)
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 18

Expert Comment

by:deighton
ID: 37775354
to give the 0 when there is no brand record

Select products.id, products.desc, products.details, brands.BrandName, COALESCE(products.IDBrand, 0)  from products LEFT join Brands on brands.BrandID = products.BrandID
0
 
LVL 8

Expert Comment

by:gpizzuto
ID: 37776074
If you use 0 value you cannot create a relation between the tables (also in future) ...
0
 
LVL 1

Author Closing Comment

by:slightlyoff
ID: 37777621
Thank you!  I appreciate the help.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

746 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

12 Experts available now in Live!

Get 1:1 Help Now