简体   繁体   中英

MS Access Inner Join Or

I'm having a problem with the query builder in MS Access 2010. I'm not able to join two tables where one has two references to the other. I have one table, Workers, with two columns, HumanFactor1 and HumanFactor2. Both of these point to table HumanFactors which consists of only two fields, ID and HumanFactor. This table is used to populate two combobox selectors for the user, and those selections are then stored as FKs in Worker.

Using the query builder, it automatically creates the following SQL

SELECT Incident.*, Worker.*
FROM HumanFactors RIGHT JOIN 
(Incident LEFT JOIN Worker ON Incident.ID = Worker.IncidentID) ON 
(HumanFactors.ID = Worker.HumanFactor2) AND (HumanFactors.ID = Worker.HumanFactor1)
WHERE (((HumanFactors.HumanFactor)="Fatigue"));

This doesn't work, because those two are always going to be different in practice, so the only returned results are records where I've forced both HumanFactors to be the same. This is incredibly easy to fix via SQL by simply changing to an OR expression

SELECT Incident.*, Worker.*
FROM HumanFactors RIGHT JOIN 
(Incident LEFT JOIN Worker ON Incident.ID = Worker.IncidentID) ON 
(HumanFactors.ID = Worker.HumanFactor2) OR (HumanFactors.ID = Worker.HumanFactor1)
WHERE (((HumanFactors.HumanFactor)="Fatigue"));

This gives me exactly what I'm looking for, but I can't figure out how to create an OR instance in the Builder. The application freaks out when I try and go back to design view.

Is there a way to handle this via the builder without resorting to changing the SQL? My users refuse to do anything more complicated than drag&drop. Or will I have to create a linking table in between? My problem there is that while a linking table makes the query builder work perfectly fine, I'm at a loss to reconnect the data to the controls. There are only two comboboxes, instead of one list that you would expect for that setup. Thanks.

You can add multiple instances of a table in a query that you create with the Query Builder. When you add the table the first time its alias will be just be the table name (eg, [HumanFactors]) and when you add it the second time its name will be the table name with _1 appended. You can change the aliases by clicking on the table and then opening the Properties pane (see screenshot below).

In your case I believe that the query would look something like this:

QueryBuilder.png

(To see a larger version of the screenshot, right-click it and choose "View Image".)

SELECT Incident.*, Worker.*
FROM
    HumanFactors HF
    LEFT JOIN Worker W ON HF.ID = W.HumanFactor1 OR HF.ID = W.HumanFactor2
    LEFT JOIN Incident I ON I.ID = W.IncidentID

How 'bout this:

在此处输入图片说明

This will give you all the records where either human factor is Fatigue. Not exactly the way you had it, but might serve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM