Union table with a query (1 Viewer)

Joy83

Member
Local time
Today, 06:26
Joined
Jan 9, 2020
Messages
116
Hi
I find this forum very useful specially when I get stack with something that takes me long time to figure it out

I have a query that gets me certain records.
I want to merge the results of this query with another table
Example: the query gives me a lis of department name from a table
Now I want the department details from another table
how to do this?
 

GPGeorge

Grover Park George
Local time
Today, 06:26
Joined
Nov 25, 2004
Messages
1,873
Include the primary and foreign keys for the related tables, as required. Then join the two on those keys as appropriate (primary to foreign keys).
 

Joy83

Member
Local time
Today, 06:26
Joined
Jan 9, 2020
Messages
116
How to refer back to the query?
deal with it as a table
Can you explain more
 

mike60smart

Registered User.
Local time
Today, 14:26
Joined
Aug 6, 2017
Messages
1,911
Can you upload your database so we can see the structure?
 

GPGeorge

Grover Park George
Local time
Today, 06:26
Joined
Nov 25, 2004
Messages
1,873
How to refer back to the query?
deal with it as a table
Can you explain more
Maybe we both should explain more....

I'll take a guess as to the problem and offer a screenshot which I hope addresses the question. When you want to create a query, you have three options from which to select tables and queries to include.

1650558205508.png
 

Joy83

Member
Local time
Today, 06:26
Joined
Jan 9, 2020
Messages
116
Thanks a lot for your reply
Unfortunately I can’t share access I can’t do attachments from my laptop

I tried to create it this way but I didn’t get what I want
I want to put the table data beside the IDs that I got from the query
But I am now getting it the other way around

if I have three IDs
I am expecting to see the data beside it and not more than three records
 

bastanu

AWF VIP
Local time
Today, 06:26
Joined
Apr 13, 2010
Messages
1,402
Click the Create\Query Design and add the table and the query. Click on the related field in the table (like DepartmentID) and drag it to the corresponding one in the query. Now select the fields you want to see from each. What you have now is called an equi-join (showing you the results where the fields match in both). You can double-click that line (=join) and select one of the other options to create a left join (so in your example seeing the entire table even if the query does not include certain departments).


Cheers,
 

jdraw

Super Moderator
Staff member
Local time
Today, 09:26
Joined
Jan 23, 2006
Messages
15,379
Joy83,
How about showing readers the SQL of your query?
 

June7

AWF VIP
Local time
Today, 05:26
Joined
Mar 9, 2014
Messages
5,474
Why don't you include the 'other' table in the original query? If you can't provide db, build tables in post to show sample data and structure. Also provide the SQL statement.
 

GPGeorge

Grover Park George
Local time
Today, 06:26
Joined
Nov 25, 2004
Messages
1,873
The more context you provide (I.e. the actual SQL of your query and the tables involved) the better the chance someone can interpret what that properly and give more focused guidance.

For example, "....I tried to create it this way but I didn’t get what I want"

Tell us what you DID get instead. Tell us what you want, specifically. This statement, while relevant, lacks the needed degree of specificity.
"I am expecting to see the data beside it and not more than three records"
 

Joy83

Member
Local time
Today, 06:26
Joined
Jan 9, 2020
Messages
116
Click the Create\Query Design and add the table and the query. Click on the related field in the table (like DepartmentID) and drag it to the corresponding one in the query. Now select the fields you want to see from each. What you have now is called an equi-join (showing you the results where the fields match in both). You can double-click that line (=join) and select one of the other options to create a left join (so in your example seeing the entire table even if the query does not include certain departments).


Cheers,
Thanks a lot
It worked
 

Joy83

Member
Local time
Today, 06:26
Joined
Jan 9, 2020
Messages
116
The more context you provide (I.e. the actual SQL of your query and the tables involved) the better the chance someone can interpret what that properly and give more focused guidance.

For example, "....I tried to create it this way but I didn’t get what I want"

Tell us what you DID get instead. Tell us what you want, specifically. This statement, while relevant, lacks the needed degree of specificity.
"I am expecting to see the data beside it and not more than three records"
Thank you for your time and help
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:26
Joined
Feb 19, 2002
Messages
43,293
Let me give you a visual for union vs join. Take a piece of paper with a list on it. Take a second list and put the second paper below the first. If the first list had 20 items and the second had 30, you now have 50 items. For a join, put the two pieces of paper side by side and draw lines from the left page to the right page where the IDs match. That is an inner join. The result is a list that is either equal in length to the left sheet if the right sheet has 100% of the same IDs as in the first list or if fewer match, the result will be the rows in the left list that match any rows in the right list. If the right list has multiple matches for the IDs in the left list, the resulting row count will be determined by the number of matches in the right list.
 

Users who are viewing this thread

Top Bottom