Can two queries be re-written into a single query? (1 Viewer)

lithium1976

Registered User.
Local time
Yesterday, 17:28
Joined
May 28, 2010
Messages
12
Hello,

I was just wondering if the following two queries can be re-written into one single query or is this not do-able?

They are based off a many-to-many relationship between tblPeople and tblJobs with a link table called tblLinkPeoplesJobs in the middle.

I am pretty much an SQL novice, so am asking purely out of interest.

Thank you :)

SQL:
SELECT tblPeople.peopleID, tblPeople.peopleFirstName, tblPeople.peopleSurname, tblLinkPeoplesJobs.jobID

FROM tblPeopleINNER JOIN tblLinkPeoplesJobsON tblPeople.peopleID= tblLinkPeoplesJobs.peopleID

WHERE (((tblLinkPeoplesJobs.jobID)=[forms]![frmSearch]![listPeople]));
and
SQL:
SELECT tblPeople.peopleID, Query1.jobID, tblPeople.peopleFirstName, tblPeople.peopleSurname

FROM Query1 RIGHT JOIN tblPeopleON Query1.peopleID= tblPeople.peopleID

WHERE (((Query1.jobID) Is Null));
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 20:28
Joined
Feb 19, 2002
Messages
43,233
No. The tables are different and the criteria is different.

Are you asking if the recordsets can be combined? That is a different question. In that case, you could use a Union query. HOWEVER, you need to clean up the two queries you posted so that the field orders are IDENTICAL.

Also, you have typos in both queries. It is always better to copy and paste than to retype.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 19:28
Joined
Feb 28, 2001
Messages
27,147
In general when you are exploiting a many-many JOIN through a junction table, you write the SELECT clause for all the fields you want to see together - i.e. SELECT A.something, B.anothersomething, C.somethingelse... FROM ...

The FROM clause is written in parts: FROM ( ( A INNER JOIN B ON A.AID = B.BID ) INNER JOIN C ON A.XID = C.CID ) and note that the parentheses, while not strictly necessary, help you to see the method of the JOIN.

Also NOTE CAREFULLY that while you CAN do this, sometimes Access makes you physically split the query because of the "direction" of the relationship arrows. What I mean by this is that sometimes, the one/many arrows leading to the item point into the center, which can confuse the query builder. In that case you have to break it up into two queries.
 

isladogs

MVP / VIP
Local time
Today, 01:28
Joined
Jan 14, 2017
Messages
18,209
This article may be helpful:
 

CJ_London

Super Moderator
Staff member
Local time
Today, 01:28
Joined
Feb 19, 2013
Messages
16,610
I assume the first sql is the query1 referred to in the second sql. In which case you can combine them as

Code:
SELECT tblPeople.peopleID, Query1.jobID, tblPeople.peopleFirstName, tblPeople.peopleSurname

FROM (SELECT tblPeople.peopleID, tblPeople.peopleFirstName, tblPeople.peopleSurname, tblLinkPeoplesJobs.jobID
            FROM tblPeople INNER JOIN tblLinkPeoplesJobsON tblPeople.peopleID= tblLinkPeoplesJobs.peopleID
            WHERE (((tblLinkPeoplesJobs.jobID)=[forms]![frmSearch]![listPeople]))) as Query1

RIGHT JOIN tblPeople ON Query1.peopleID= tblPeople.peopleID

WHERE (((Query1.jobID) Is Null));

But not clear why you are returning so many fields from query1 when all you want is peopleID and jobID
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 20:28
Joined
Feb 19, 2002
Messages
43,233
Clearly none of us has any idea what your objective might be. Perhaps three recordsets will solve the problem so we can tell how you are trying to combine the data.

query1
query2
results
 

Isaac

Lifelong Learner
Local time
Yesterday, 17:28
Joined
Mar 14, 2017
Messages
8,777
@lithium1976

Did you know that you can simply edit one of the queries to refer to the second query as if it were a Table - as long as there is a column that's appropriate for Joining?
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 20:28
Joined
Feb 19, 2002
Messages
43,233
OK, now we have four people assuming a join is what the OP requested and me assuming he needed a union. Maybe someday, we'll know:)
 

Isaac

Lifelong Learner
Local time
Yesterday, 17:28
Joined
Mar 14, 2017
Messages
8,777
OK, now we have four people assuming a join is what the OP requested and me assuming he needed a union. Maybe someday, we'll know:)
good point ... who knows.
honestly if someone is on Day 1 of writing queries and I see a Right Join I highly doubt that even what they already wrote is correct or they have any idea what they are doing or why.

I think they were just playing with the options on the GUI.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 19:28
Joined
Feb 28, 2001
Messages
27,147
I based my answer on this statement from the OP's post #1:

"They are based off a many-to-many relationship between tblPeople and tblJobs with a link table called tblLinkPeoplesJobs in the middle."
 

KitaYama

Well-known member
Local time
Today, 09:28
Joined
Jan 6, 2022
Messages
1,540
That's why I love this forum. The OP asks something and never show back again, but the members are still debating and insisting to help.
 

ebs17

Well-known member
Local time
Today, 02:28
Joined
Feb 7, 2020
Messages
1,935
It has been a long time ...

two queries can be re-written into one single query or is this not do-able?
They are based off a many-to-many relationship between tblPeople and tblJobs with a link table called tblLinkPeoplesJobs in the middle.

I assume with some confidence that the first query is the one used as Query1 in the second query. Then putting them together is extremely easy, just copy and paste.

SQL:
SELECT tblPeople.peopleID, Query1.jobID, tblPeople.peopleFirstName, tblPeople.peopleSurname
FROM

(
SELECT tblPeople.peopleID, tblPeople.peopleFirstName, tblPeople.peopleSurname, tblLinkPeoplesJobs.jobID
FROM tblPeopleINNER JOIN tblLinkPeoplesJobsON tblPeople.peopleID= tblLinkPeoplesJobs.peopleID
WHERE (((tblLinkPeoplesJobs.jobID)=[forms]![frmSearch]![listPeople]))
) AS Query1

RIGHT JOIN tblPeopleON Query1.peopleID= tblPeople.peopleID
WHERE (((Query1.jobID) Is Null))

Whether the overall design makes sense is another question.

You would certainly have to clean up a lot, but you can see the problems at a glance (thanks to the composition):

- The many brackets only confuse and should be removed as much as possible.

- The subquery returns many fields, the main query uses only one of them. swipe.

- The form reference in the subquery should cause problems. But there are parameter queries, and parameters can also be set at lower query levels.

- The criteria in the subquery and main query contradict each other (jobID=37 versus jobID IS NULL).

- The double use of tblPeople is certainly reworkable.

Before a final revision, I would also ask again what the actual goal of the overall query is: A question of meaning in addition to the question of technology.
 
Last edited:

Users who are viewing this thread

Top Bottom