Print Page | Close Window

Inner Join

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Database Discussion
Forum Description: Discussion and chat on database related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=8230
Printed Date: 30 March 2026 at 7:27am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Inner Join
Posted By: madius
Subject: Inner Join
Date Posted: 19 December 2003 at 4:33pm

Currently using VB6 to retreive data using the 'Data Control'

I have a master table called  "Orders", another table called "Material" and another called "Hours"

Using 'INNER JOIN' I can call all orders from the master table where the invoices are not completed AND that have an entry against it in the Material table by matching the RJKNo field in each table with the following

"SELECT DISTINCT Orders.RJKNo FROM Orders INNER JOIN Material ON Orders.RJKNo = Material.RJKNo WHERE Orders.InvoiceComplete = 'No'"

I can also search for all orders again, this time looking for orders that have hours against them with

"SELECT DISTINCT Orders.RJKNo FROM Orders INNER JOIN Hours ON Orders.RJKNo = Hours.RJKNo WHERE Orders.InvoiceComplete = 'No'"

How do I create 1 query out of these two which will search for records that have either Material OR Hours.

I've tried nesting the 'INNER JOIN' but I can't get the sytax correct.

Any takers

Madius (Scotland UK)




Replies:
Posted By: fernan82
Date Posted: 20 December 2003 at 11:28am
Try something like this:

SELECT DISTINCT Orders.JKNo From (Orders LEFT JOIN Material ON Orders.RJKNo = Material.RJKNo) LEFT JOIN Hours.RJKNo WHERE (Material.RJKNo IS NOT null OR Orders.JKNo IS NOT null) AND Orders.InvoiceComplete = 'No';

The LEFT JOINS should return all the records on the Orders table along with the records that match on the other tables AND the WHERE statement should filter out the records that don't have a match on another table. This might not work at the first try but it'll give you an idea of what to try...


-------------
FeRnAN
http://www.danasoft.com/">


Posted By: madius
Date Posted: 21 December 2003 at 8:16am


Posted By: madius
Date Posted: 21 December 2003 at 8:25am

Thanks for the reply,

It didn't work but I think I might have something to play with there,

Meanwhile I got this to work

 "SELECT DISTINCT Orders.* FROM (Orders INNER JOIN Material ON Orders.RJKNo = Material.RJKNo) LEFT JOIN Hours ON Orders.RJKNo = Hours.RJKNo WHERE Orders.InvoiceComplete = 'No'"
 

This will select all orders which are not complete and have a material entry against it in the Material Table but it doesn't find anything in the Hours Table.

Swap the word INNER and LEFT around and it will find all orders which are not complete and have Hours against it in the Hours Table but it doeesn't find anything in the Material Table

"SELECT DISTINCT Orders.* FROM (Orders LEFT JOIN Material ON Orders.RJKNo = Material.RJKNo) INNER JOIN Hours ON Orders.RJKNo = Hours.RJKNo WHERE Orders.InvoiceComplete = 'No'"

I need something which combines the two. Perhaps another INNER JOIN?

PS EDIT

I find if I change both words to INNER, I only got the orders which have BOTH Materials and Hours but not the orders which have Material only or Hours only



Posted By: psycotik
Date Posted: 21 December 2003 at 7:37pm

SELECT DISTINCT Orders.RJKNo
FROM Orders
LEFT JOIN Material ON Orders.RJKNo = Material.RJKNo
LEFT JOIN Hours ON Orders.RJKNo = Hours.RJKNo
WHERE Orders.InvoiceComplete = 'No'
AND (Material.RJKNo is not Null OR Hours.RJKNo is not Null)

This should solve the problem, if the join is successful then the key field will have a value (ie.. not null).



Posted By: madius
Date Posted: 22 December 2003 at 12:57pm

Whey Hey......

I had to add brackets to the first INNER JOIN but essentially it worked.

Pretty simple when you see how.

Thanks for your help



Posted By: Flamewave
Date Posted: 22 December 2003 at 12:59pm

You could also do a union with the two statements, ie:

statement1
Union All
statement2



-------------
- Flamewave

They say the grass is greener on the other side, but if you really think about it, the grass is greener on both sides.



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net