Solved

query displaying 2 tables without losing any records

Posted on 2000-04-13
26
135 Views
Last Modified: 2010-05-02
I want to combine two tables into one query, displaying all the records from both tables.
All I get is a query displaying double records or a query filling the wrong fields.
Who can help me?
Please try to answer my question as simple as possible because I’m Dutch.....
0
Comment
Question by:km3
  • 7
  • 7
  • 4
  • +2
26 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 2714970
How are the tables related??
0
 

Author Comment

by:km3
ID: 2715045
The tables are relates by "zoeknaam" ("searchname")
One table is a table with sponsors sponsoring money and the other a table with sponsors sponsoring materials. It’s also possible a sponsor sponsors money AND materials.
The query has to display all the records from both tables.
0
 
LVL 1

Expert Comment

by:detiege
ID: 2715061
SELECT * FROM table1
JOIN table2 ON table1.common = table2.common

OR

SELECT * FROM table1,table2
WHERE table1.common1 = table2.common1
AND table1.common2 = table2.common2
      .                .
      .                .
      .                .

This display all records
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2715062
SELECT * FROM Table1 FULL JOIN Table2 ON Table1.Sponsor = Table2.Sponsor

try that and see if it works
0
 
LVL 1

Expert Comment

by:detiege
ID: 2715065
OOOPS ! ! ! !

Common1 and common2 are the common fields between the tables.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2715070
detiege,
correct me if I'm wrong but a regular join will not include null values. it only includes those records where a match is found.  km3 specified that all records are returned.
0
 
LVL 1

Expert Comment

by:detiege
ID: 2715094
Yes, excuse me !
Replace JOIN by FULL JOIN.

If a row from either table does not match the selection criteria, specifies the row be included in the results set and its output columns that correspond to the other table be set to NULL.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2715098
can you withdraw your answer since mine is the correct solution please
0
 
LVL 1

Expert Comment

by:detiege
ID: 2715148
detiege changed the proposed answer to a comment
0
 

Author Comment

by:km3
ID: 2715167
AzraSound,

When I try a FULL JOIN, I get a Sytaxis-error with the component FROM
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2715175
SELECT * FROM (Table1 FULL JOIN Table2 ON Table1.Sponsor = Table2.Sponsor)

try it with parenthesis

0
 

Author Comment

by:km3
ID: 2715231
It still doesn't work. When I replace FULL for INNER or LEFTor RIGHT etc., I don't get an error but I don't get the right records either.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2715246
yes you need full...dont know why its not supported..try one of these then:

SELECT * FROM (Table1 LEFT OUTER JOIN Table2 ON Table1.Sponsor = Table2.Sponsor)


SELECT * FROM (Table1 RIGHT OUTER JOIN Table2 ON Table1.Sponsor = Table2.Sponsor)


0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 28

Expert Comment

by:AzraSound
ID: 2715248
however i dont think it will return desired results if it works.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2715367
Your statement may read:
SELECT * FROM Table1 FULL OUTER JOIN Table2 ON Table1.Sponsor = Table2.Sponsor
 
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 2715786
Why don't you use a UNION query like this:

SELECT F1, F2, F3 FROM Table1
UNION
SELECT F1, F2, F3 FROM Table2
ORDER BY F1
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2715825
comment on your comment, emoreau:
1) use rather UNION ALL that simple UNION, because of performance and to avoid duplicate records are eliminated

2) the tables are related by a field, so i don't think this is what he is looking for




0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 2715864
angelIII,

The question is: «I want to combine two tables into one query, displaying all the records from both tables.»

This sounds to me a UNION query not a JOIN query!
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2715884
For me, he looks for a list of sponsors (every sponsor listed once, eventually including some details), with the columns [SumOfSponsoredMaterial], [CountOfSponsoredMaterial], [SumOfSponsoredMoney], [CountOfSponsoredMoney]

I think there is no need to "shout" at each other, lets tell km3 what he really wants
0
 

Author Comment

by:km3
ID: 2716076
I tried to use UNION but that doesn’t work either.

There's Table1:

Tabel1ID         SponsorID    Amount
1          1              fl 500,00
2          1              fl 600,00
3          2              fl 100,00

And Table2:

Tabel2ID         SponsorID   Material
1         1             car
2         3             flowers

And my query has to be:

SponsorID    Amount     Material
1           fl 500,00      
1           fl 600,00      
1                  car
2           fl 100,00      
3                  flowers
0
 

Author Comment

by:km3
ID: 2716113
comment on my comment:

In the query, there are three colomns: SponsorID, Amount and Material. In the colomn Amount you should not see the materials and vice versa (It doesn't show that well in my previous comment)
0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 50 total points
ID: 2716135
You can use this Union Query:

SELECT Table1.SponsorID, Table1.Amount, '' as Material
FROM Table1

UNION

SELECT Table2.SponsorID, 0 as Amount, Table2.Material
FROM Table2

ORDER BY SponsorID;
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2716411
Please, emoreau, for performance's sake, use UNION ALL !
0
 

Author Comment

by:km3
ID: 2716420
Thanks emoreau, you're great, it works!!!!
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 2716427
Do you accept this now it is working?

You can use this Union Query:

SELECT Table1.SponsorID, Table1.Amount, '' as Material
FROM Table1

UNION ALL

SELECT Table2.SponsorID, 0 as Amount, Table2.Material
FROM Table2

ORDER BY SponsorID;
0
 

Author Comment

by:km3
ID: 2716429
It took me so long to find an answer for this problem, thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now