473,544 Members | 2,340 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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 2235
benwizzle
72 New Member
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.(T his 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 Recognized Expert New Member
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,563 Recognized Expert Moderator MVP
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,563 Recognized Expert Moderator MVP
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
6405
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, Employees.LastName, TerritoryID, Employees.EmployeeID, RegionID, ProductID from Employees
8
6308
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 is seem below). This seemed to work but i am unsure why and would like to know in case it falls over again. Why did the two INNER JOINS not work,...
4
2031
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 ? thanks
1
1531
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 overlap in some areas and none of the fields are unique except the AddressKey which I created after pulling data from the Main-table. Why some...
2
1610
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 = 'enclosure' and link_detail.x_id = 3 and link.idx = link_detail.linkidx and A.strandid = link.x_id_a and B.strandid = link.x_id_z would someone please...
0
1275
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 previous day versus monthly targets and sales. Most of the parts were figured out and eveything was done in Visual Studio. The gist of the code was...
5
4061
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 framework to auto-generate a table adapter specifying both stored procs as Get/Fill and Update. The problem is that columns from the JOINed table...
9
11910
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: http://www.codinghorror.com/blog/archives/000976.html
6
1776
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 * # questions posted) + (15 * # members recruited). The relevant parts of the tables are listed below: table members ( member_id int(10), ...
4
3338
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 with the hardcoded criteria. The VBA version with my criteria being set by a variable returns Error 3021. I am using a DAO Recordset and all I want...
0
7449
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7385
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7642
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7405
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5950
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3440
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3432
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1861
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.