Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Inner Join
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Inner Join

 Post Reply Post Reply
Author
madius View Drop Down
Newbie
Newbie


Joined: 19 December 2003
Status: Offline
Points: 4
Post Options Post Options   Thanks (0) Thanks(0)   Quote madius Quote  Post ReplyReply Direct Link To This Post Topic: Inner Join
    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)

Back to Top
fernan82 View Drop Down
Mod Builder Group
Mod Builder Group
Avatar

Joined: 17 November 2002
Location: United States
Status: Offline
Points: 362
Post Options Post Options   Thanks (0) Thanks(0)   Quote fernan82 Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
madius View Drop Down
Newbie
Newbie


Joined: 19 December 2003
Status: Offline
Points: 4
Post Options Post Options   Thanks (0) Thanks(0)   Quote madius Quote  Post ReplyReply Direct Link To This Post Posted: 21 December 2003 at 8:16am
Back to Top
madius View Drop Down
Newbie
Newbie


Joined: 19 December 2003
Status: Offline
Points: 4
Post Options Post Options   Thanks (0) Thanks(0)   Quote madius Quote  Post ReplyReply Direct Link To This Post 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



Edited by madius
Back to Top
psycotik View Drop Down
Groupie
Groupie


Joined: 27 November 2003
Status: Offline
Points: 73
Post Options Post Options   Thanks (0) Thanks(0)   Quote psycotik Quote  Post ReplyReply Direct Link To This Post 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).

Back to Top
madius View Drop Down
Newbie
Newbie


Joined: 19 December 2003
Status: Offline
Points: 4
Post Options Post Options   Thanks (0) Thanks(0)   Quote madius Quote  Post ReplyReply Direct Link To This Post 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

Back to Top
Flamewave View Drop Down
Senior Member
Senior Member
Avatar

Joined: 19 June 2002
Location: United States
Status: Offline
Points: 376
Post Options Post Options   Thanks (0) Thanks(0)   Quote Flamewave Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.