[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 356
  • Last Modified:

How would you join another table to an existing query?

I am developing and Access 2003 application using an MDB type file

How would you join another table named tblBanksRev to the follwing query as follows?

SELECT tblBanks.*, tblDates.dtRec, tblOpenItems.*
FROM tblDates,
tblBanks INNER JOIN tblOpenItems ON tblBanks.[Bank Code]=tblOpenItems.Bank
WHERE tblOpenItems.t In ("A","D","E");

ADD the following table to the Query:

tblBanksRev.[BRS #]  = tblBanks.[Bank Code]
0
zimmer9
Asked:
zimmer9
1 Solution
 
MikkelAStrojekCommented:
SELECT tblBanks.*, tblDates.dtRec, tblOpenItems.*
FROM tblDates, tblBanks
INNER JOIN tblOpenItems ON tblBanks.[Bank Code]=tblOpenItems.Bank
INNER JOIN tblBanksRev ON tblBanksRev.[Bank Code]=tblBanks .BankWHERE tblOpenItems.t In ("A","D","E");

The bold highlighted depends on what the tables have in common

0
 
Alex MatzingerCommented:
Like this
SELECT tblBanks.*, tblDates.dtRec, tblOpenItems.*
FROM tblDates,  
tblBanks 
INNER JOIN tblOpenItems ON tblBanks.[Bank Code]=tblOpenItems.Bank
INNER JOIN tblBanksRev ON tblBanksRev.[BRS #]  = tblBanks.[Bank Code]
WHERE tblOpenItems.t In ("A","D","E");

Open in new window

0
 
AkAlanCommented:
SELECT tblBanks.*, tblDates.dtRec, tblOpenItems.*
FROM tblDates,
tblBanks
INNER JOIN tblOpenItems ON tblBanks.[Bank Code] = tblOpenItems.Bank
INNER JOIN tblBanksRev ON tblBanksRev.[BRS #]  = tblBanks.[Bank Code]
WHERE tblOpenItems.t In ("A","D","E");

This will only return records if there are records in tblBanksRev where the [BRS #] from tblBanksRev  matches [Bank Code] tblBanks

If you want All the records from tblBanks and any that have related records in tblBanksRev then you need to join it with a LEFT OUTER JOIN like this


SELECT tblBanks.*, tblDates.dtRec, tblOpenItems.*
FROM tblDates,
tblBanks
INNER JOIN tblOpenItems ON tblBanks.[Bank Code] = tblOpenItems.Bank
LEFT OUTER JOIN tblBanksRev ON tblBanksRev.[BRS #]  = tblBanks.[Bank Code]
WHERE tblOpenItems.t In ("A","D","E");

0
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 
zimmer9Author Commented:
SELECT tblBanks.*, tblDates.dtRec, tblOpenItems.*
FROM tblDates,
tblBanks
INNER JOIN tblOpenItems ON tblBanks.[Bank Code] = tblOpenItems.Bank
LEFT OUTER JOIN tblBanksRev ON tblBanksRev.[BRS #]  = tblBanks.[Bank Code]
WHERE tblOpenItems.t In ("A","D","E");

The above statement causes the following error:

Syntax error (missing operator) in query expression
'tblBanks.[Bank Code] = tblOpenItems.Bank
Left outer join tblBanksRev.[BRS #] = tblBanks.[Bank Code]'.

However, if I leave out the following LEFT OUTER JOIN statement from the query, then it works fine:

LEFT OUTER JOIN tblBanksRev ON tblBanksRev.[BRS #]  = tblBanks.[Bank Code]
0
 
zimmer9Author Commented:
Let me restate the error:

Syntax error (missing operator) in query expression
'tblBanks.[Bank Code] = tblOpenItems.Bank
LEFT OUTER JOIN tblBanksRev on
tblBanksRev.[BRS #] = tblBanks.[Bank Code]'.

0
 
zimmer9Author Commented:
If I use the following query:

SELECT tblBanks.*, tblDates.dtRec, tblOpenItems.*
FROM tblDates,  
tblBanks
INNER JOIN tblOpenItems ON tblBanks.[Bank Code]=tblOpenItems.Bank
INNER JOIN tblBanksRev ON tblBanksRev.[BRS #]  = tblBanks.[Bank Code]
WHERE tblOpenItems.t In ("A","D","E");

then the error is very similar as follows:

Syntax error (missing operator) in query expression
'tblBanks.[Bank Code] = tblOpenItems.Bank
INNER JOIN tblBanksRev ON
tblBanksRev.[BRS #] = tblBanks.[Bank Code]'.



0
 
AkAlanCommented:
Your statement is definately not correct. You are trying to select all records from tblBanks, one column from tblDates (dtRec) and all from tblOpenItems

you then have FROM tblDates,tblBanks. That is a problem, you don't seperate the tables with a comma, you do it with joins
So you need a join with tblBanks

SELECT tblBanks.*, tblDates.dtRec, tblOpenItems.*

FROM tblDates

INNER JOIN tblBanks ON tblBanks.[SomeKeyGoesHere] = tblDates.[SomeKeyGoesHere]
INNER JOIN tblOpenItems ON tblBanks.[Bank Code]=tblOpenItems.Bank
INNER JOIN tblBanksRev ON tblBanksRev.[BRS #]  = tblBanks.[Bank Code]
WHERE tblOpenItems.t In ("A","D","E");

Without knowing the table schema, there is no way for me to know which keys to join on.

I think you really need to study up on joins to understand the concept. http://www.w3schools.com/sql/default.asp has great free tutorials.




0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now