473,420 Members | 3,645 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,420 software developers and data experts.

SQL Join Multiple Tables

I need some help trying to figure out how to retrieve data from 3 different tables, when there are records in 1 table that don't exists in the other tables.
Here's the situation
Table1
Fields: RefNum & PartNum
In this table there may be multiple PartNum associated with a RefNum
Ex:
RefNum | PartNum
4-1 | ABC
4-1 | DEF
4-2 | AAA
4-3 | BBB

Table2
Fields PartNum, NewPartNum
The PartNum will match up to Table1.PartNum although there may not be a match at all
Ex:
PartNum | NewPartNum
ABC | 123
AAA | 999
BBB | 888

Table3
Fields PartNum, Price
This PartNum will also match up to Table2.NewPartNum
Ex:
PartNum | Price
123 | 100.00
999 | 200.00
888 | 300.00

What I am trying to do is find all records that belong to Table1.RefNum. Then using those Table1.PartNum to find the NewPartNum in Table2 and the Price from Table3

The problem I'm having is that if there is not match from Table1.PartNum = Table2.PartNum, then it returns no data. For example, if I restrict the query to Table1.RefNum="4-1", what I would like to get returned is:

Table1.RefNum | Table1.PartNum | Table2.NewPartNum | Table3.Price
4-1 | ABC | 123 | 100.00
4-1 | DEF | Null | Null


Unfortunately I can't figure out the correct way to join these 3 tables so that I see ALL the results from Table1 and only the matching results from Table2 & Table3 (with "nulls" listed for those that don't have a match)
Apr 30 '10 #1

✓ answered by ck9663

Use a LEFT JOIN to table2 and table3.

Caution: Check for duplicate.

Happy Coding!!!

~~ CK

4 2065
ck9663
2,878 Expert 2GB
Use a LEFT JOIN to table2 and table3.

Caution: Check for duplicate.

Happy Coding!!!

~~ CK
Apr 30 '10 #2
Unfortunately I'm still getting just the one result. The stmt I'm using is:

Select Table1.RefNum, Table1.PartNum, Table2.NewPartNum, Table3.Price from (Table1 LEFT OUTER JOIN Table2 on Table1.PartNum=Table2.PartNum) LEFT JOIN Table3 on Table3.PartNum = Table2.NewPartNum
Where Table1.RefNum='4-1'

The result I get is
4-1 | ABC | 123 | 100.00

The result I want to get is
4-1 | ABC | 123 | 100.00
4-1 | DEF | Null | Null <----to show there is no partnum "DEF" in either Table2 or Table3


Any ideas what's wrong w/ the select stmt?

If I remove the 3rd table from the mix and just join Table1 & Table2, I get the result I'm after (listing both results from Table1 and "null" for the Table2 item that doesn't have a match). But once I throw the 3rd table back into the mix, I only get the 1 result from Table1 instead of both items (w/ nulls for non-matching one)
Apr 30 '10 #3
ck9663
2,878 Expert 2GB
Try removing the parenthesis on your join, the one after "from".


Happy Coding!!!

~~ CK
May 3 '10 #4
well found the problem. In my example, this was just a short version of a much longer query and the problem was in the longer query part of the "where" clause was to restrict the results of only US$ prices from the price table. But by doing that, if the result included a "null", then of course those records weren't returned either. So by simply adding a "where Table3.currency='USD' or Table3.currency is null" then the query returned all records, including ones without a match.

I realize if I had provided ALL the info your response would have probably saved me a lot of time. But by confirming the whole "left join" stmt I was at least able to narrow down my problem & solution to something other than the way I was joining the table.

Thank you!!
May 3 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Petre Agenbag | last post by:
Hi List Me again. I'm trying to return from multiple tables, the records that have field "information_sent" between two dates. The tables are all related by means of the id of the entry in the...
1
by: Robin Dindayal | last post by:
Does anyone know how I can print a fully rendered .aspx to the server's printer? I know that, if I wanted to print to the client's printer it would be easy (ie. use javascript's window.print()). ...
5
by: Earl Teigrob | last post by:
I am creating an application where I would like to give web designers the ablity to create a static html page and dyanamically load it into my application(exactly like loading a user control into a...
0
by: Jerwin | last post by:
I have successfully embedded a Word document in my ASPX. I can view an write onto the Word document, as if the word document was pasted on the page It's like a 4"x3" area where u can view a word...
0
by: tshad | last post by:
I am finally splitting my code into 2 (code-behind). This is what I have in the first few lines: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <%@ Page Language="VB"...
5
by: jim | last post by:
Hi, I've browsed several posts, but still haven't found the answer I'm looking for. I have one table (A) that contains a list of values I want to return. I have two other tables (B) and (C)...
6
by: DAnDA | last post by:
Hi how can i access value of a TextBox inside ASPX Page ,,,from ASCX(User Control page) ,,,thats all.!!! Thanks
3
by: buterfly0707 | last post by:
hi.. i have 3 tables. and the field discription is in Accounts table Account. and GL table fields are Debit, Credit and Period. And in Period Table Period and Date. this are nt only fields in this...
0
by: umeed4u | last post by:
i am student presently doing one academic project,but i came across with a problem which is as.. 1)my project contains coding in java swing,servlets where i wana to access it through mobile ... ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.