Can't do 2 joins on an MS Access query?

◇◆丶佛笑我妖孽 提交于 2020-01-17 04:19:08

问题


So I have checked all the names etc are correct, the query works with only one of the joins not both with a syntax error:

SELECT *, Invoice_Accounts.Title AS t1, Invoice_Accounts.Forename AS f1, Invoice_Accounts.Surname AS s1 
FROM Invoice_Accounts 
RIGHT JOIN  Delivery_Points ON Invoice_Accounts.Account_No = Delivery_Points.Account_No  
RIGHT JOIN Companies ON Invoice_Accounts.account_id = Companies.Company_No 

This is the query I want to run but it comes up with the error:

syntax error (missing operator)

UPDATE:

using:

SELECT  *, 
        Invoice_Accounts.Title AS t1, 
        Invoice_Accounts.Forename AS f1, 
        Invoice_Accounts.Surname AS s1 
FROM    (Invoice_Accounts 
        RIGHT JOIN  Delivery_Points 
            ON Invoice_Accounts.Account_No = Delivery_Points.Account_No)
        RIGHT JOIN Companies 
            ON Invoice_Accounts.account_id = Companies.Company_No

Gets me the error "JOIN expression not supported"


回答1:


In Access you need to use parenthesis around joins if you have more than one:

SELECT  *, 
        Invoice_Accounts.Title AS t1, 
        Invoice_Accounts.Forename AS f1, 
        Invoice_Accounts.Surname AS s1 
FROM    (Invoice_Accounts 
        RIGHT JOIN  Delivery_Points 
            ON Invoice_Accounts.Account_No = Delivery_Points.Account_No)
        RIGHT JOIN Companies 
            ON Invoice_Accounts.account_id = Companies.Company_No 

Two extend this, if you had three joins, you would need another set of parentheses:

SELECT  *
FROM    (( A
        INNER JOIN  B 
            ON B.AID = A.AID)
        INNER JOIN C 
            ON C.BID = B.BID)
        INNER JOIN D
            ON D.CID = C.CID;

EDIT

I did not know that you could not RIGHT JOIN to the same table twice, so the above is an error, having said that I have never used a RIGHT JOIN in production code in my life, I would be inclined to switch to the more widely used LEFT JOIN and change the order of the tables:

SELECT  *, 
        ia.Title AS t1, 
        ia.Forename AS f1, 
        ia.Surname AS s1 
FROM    (Companies AS c
        LEFT JOIN Invoice_Accounts AS ia
            ON ia.account_id = c.Company_No)
        LEFT JOIN  Delivery_Points AS dp
            ON ia.Account_No = dp.Account_No;

N.B. I have used aliases to reduce the amount of text in the query, this (in my opinion) makes them easier to read




回答2:


Figured out the answer eventually:

SELECT Invoice_Accounts.Title AS t1, Invoice_Accounts.Forename AS f1, Invoice_Accounts.Surname AS s1, *

FROM (Companies RIGHT JOIN Invoice_Accounts ON Companies.Company_No = Invoice_Accounts.Company_Name )

RIGHT JOIN Delivery_Points ON Invoice_Accounts.Account_No = Delivery_Points.Account_No;

The issue arises from ancient ms-access technologies where you can't right join to the same table more than once!



来源:https://stackoverflow.com/questions/28479690/cant-do-2-joins-on-an-ms-access-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!