473,434 Members | 1,491 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,434 software developers and data experts.

How to modify an inner join search query to return all fields even if a table is null

Hello, my question has to do with inner joins and nulls.

The question is, how would i need to modify an inner joins search query to return all fields even if a table is null. So basically, if I have two tables that have matching values and a third table that is completely null or doesn't have those values, I still want it to return those matching values.

I also don't have any experience with this so please describe it as thoroughly as possible.

Thanks in advance for the help!!!
Jul 26 '10 #1
4 2227
I think I had a similar need before with what your asking. I needed the results of an inner join on two tables but wanted to get matches with a third table and still get results even if there was no matches from that third table.
If I am understanding you correctly then you would inner join the first two tables and then LEFT JOIN the third table. For example. Let say Table1 is General, Table 2 is Services, and Table3 is ServiceGoals.(This is the example i had to use that i am referring to.)
Then your query would look like this.
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM General INNER JOIN Service ON General.ID = Service.ID LEFT JOIN ServiceGoals ON General.ID = ServiceGoals.ID
  2.  
This will return the matches from the first two tables and then join the third even if there are no matches. So therefore you will have all the rows of the first two tables but if there was no match with the third table you will have columns with no values and ones with matches will have values.

W3Schools has a nice example and explanation of joins. Here is a link to that as well.

http://www.w3schools.com/sql/sql_join.asp

I hope this is what you were looking for.
Jul 26 '10 #2
Jerry Winston
145 Expert 100+
After reading and rereading your post I think I have a solution for you.

You are performing two operations on four datasets, TableA, TableB, TableAB(INNER JOIN),TableC.

We start with TableA, TableB, and TableC available to us. We need to create dataset TableAB
by INNER JOINing tables TableA and TableB as our first operation.
Expand|Select|Wrap|Line Numbers
  1. SELECT A.[column1],B.[column2] FROM
  2. TableA A
  3. INNER JOIN
  4. TableB B
  5. ON
  6. A.joinField = B.joinField
  7.  
This produces dataset TableAB. Now we can proceed to our second operation, retrieving all rows
from TableC and appending them to the dataset TableAB.

Expand|Select|Wrap|Line Numbers
  1. SELECT column1,column2 as [ccolumn12] FROM TableAB
  2. UNION ALL
  3. SELECT [ccolumn7] as [column1],[ccolumn12] FROM TableC
  4.  
To successfully use the UNION ALL, you will need to make sure the column names in TableC match with the column names in the dataset TableAB. In addition to matching column names you will have to match data types as well. You can accomplish this easily by assigning incongruous
fields an homogenizing alias. In the code above I give [ccolumn7] the alias [column1] which matches the field from the TableAB dataset. The second alias in the code sample assigns column2 from the TableAB dataset the alias [ccolumn12] which matches the field name from the TableC dataset.

Now that we have all the pieces we can put this together in one big script.
Expand|Select|Wrap|Line Numbers
  1. SELECT A.[Column1],B.[column2] as [ccolumn12] FROM TableA A
  2. INNER JOIN TableB B ON A.joinField = B.joinField
  3.  
  4. UNION ALL
  5. SELECT [ccolumn7] as [column1], [ccolumn12] FROM TableC
  6.  
This script combines rows from INNER JOINed datasets TableA and TableB with any and all rows from dataset
TableC.

Let me know how this works out for you!
Jul 28 '10 #3
NeoPa
32,556 Expert Mod 16PB
This is a perfect example of why we don't allow threads to be hijacked. Robert has asked a perfectly reasonable question, but Akhos seems to think this is his thread all of a sudden. I will do what I can to sort out the mess. In the mean-time, please ignore any further comments that are not on (original) topic. When the new thread is up and running Akhos will have his own thread in which he can continue his conversation.

It may take a while so please be patient.

The other thread can now be found at Full Outer Joins.
Aug 2 '10 #4
NeoPa
32,556 Expert Mod 16PB
Robert,

If you mean the third table has a matching field, but will not always have matching data, then you are probably looking for a LEFT OUTER JOIN between the first set of tables (TableAB in b0010100's scenario). If not, and you're looking for something else then perhaps a clarification from you would be in order at this point.
Aug 2 '10 #5

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

Similar topics

3
by: Prem | last post by:
Hi, I am having many problems with inner join. my first problem is : 1) I want to know the precedance while evaluating query with multiple joins. eg. select Employees.FirstName,...
8
by: kieran | last post by:
Hi, I have the following sql statement. I originally had the statement with two INNER JOINS but in some situations was getting an error so changed the last INNER JOIN to a LEFT OUTER JOIN (as...
4
by: michaelnewport | last post by:
Greetings, I like to write my inner joins as below, but someone at work tells me its not as 'performant' as using the 'inner join' statement. Is this true ? Is there a better way to write it...
1
by: Steve | last post by:
Hi, I am real new to databases and hoping someone can help. Main-table is a huge spreadsheet that I imported into Access Site-table has a bunch of addresses pulled from Main-table, quite a few...
2
by: gkellymail | last post by:
the following query works fine: select link.idx, link.x_table, link.x_id_a, link.x_id_z, a.strandid, b.strandid from link_detail, link, strand A, strand B where link_detail.x_table =...
0
by: stanlew | last post by:
Happy New Year everyone! I'm new to both T-SQL and this forum. I'm currently doing an internship and my first task was to create a small program which will send an email detailing the sales of the...
5
by: Bogdan | last post by:
Hi, I have a stored procedure that uses JOINs to return columns from multiple tables. I also have another stored proc that that takes a series of params and updates multiple tables. I used the...
9
by: shapper | last post by:
Hello, I am used to SQL but I am starting to use LINQ. How can I create Left, Right and Inner joins in LINQ? How to distinguish the different joins? Here is a great SQL example:...
6
by: loffing4 | last post by:
I'm trying to write a query that selects the top five members of a forum based on how many total points they have. A member's total points are calculated as follows: (3 * # answers posted) + (10 * #...
4
by: AXESMI59 | last post by:
I created a query in the Access Query Builder that I modified to use with VBA Code so I could reuse it for any record I choose. The one that I created with the Access Query builder works beautifully...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.