MS Access 2013 SQL查询LEFT JOIN

时间:2015-09-01 15:37:26

标签: ms-access join

FROM (((Project 
INNER JOIN MDS ON Project.PID=MDS.PID) 
INNER JOIN PLocation ON Project.PID = PLocation.PID) 
INNER JOIN Site ON PLocation.ACode = Site.ACode) AS [Prim] 
LEFT JOIN ((Procurement INNER JOIN MagicT ON Procurement.PRNum = MagicT.PRNum) 
INNER JOIN DO ON DO.DoNum = MagicT.DONum) AS [Prim2] ON Prim.PRNum = Prim2.PRNum
嘿伙计们。所以上面的陈述给了我一个错误:

  

from子句

中的语法错误

有趣的是,当我使LEFT JOIN成为INNER JOIN它运行正常时,但遗憾的是,这不是我想要的。我读到我必须重命名内部查询并使用他们的新名称加入它们,但不幸的是我不认为我做得很好。

3 个答案:

答案 0 :(得分:1)

也许这......使用两个内联视图。但你应该能够在Access中只使用()进行此操作...但是如果每个Prim / prim2表中都有相同的命名列,则会出现错误,您可能需要拼出每个内联视图中所需的列名。

FROM (SELECT * FROM Project 
INNER JOIN MDS ON Project.PID=MDS.PID
INNER JOIN PLocation ON Project.PID = PLocation.PID
INNER JOIN Site ON PLocation.ACode = Site.ACode) PRIM
LEFT JOIN (SELECT * from Procurement 
           INNER JOIN MagicT ON Procurement.PRNum = MagicT.PRNum
           INNER JOIN DO ON DO.DoNum = MagicT.DONum) PRIM2
  ON Prim.PRNUM = Prim2.PRNUM

---也许......在访问中,()' s将处理外连接,但我不知道PRNUM来自哪个表(项目,mds,plocation,site)所以项目是一个猜测...

FROM Project 
INNER JOIN MDS ON Project.PID=MDS.PID
INNER JOIN PLocation ON Project.PID = PLocation.PID
INNER JOIN Site ON PLocation.ACode = Site.ACode
LEFT JOIN (SELECT * from Procurement 
           INNER JOIN MagicT ON Procurement.PRNum = MagicT.PRNum
           INNER JOIN DO ON DO.DoNum = MagicT.DONum) PRIM2
  ON Project.PRNUM = Prim2.PRNUM

答案 1 :(得分:1)

你基本上有

FROM Subquery1 LEFT JOIN Subquery2

但您的子查询中没有SELECT 我认为应该是:

FROM 
(SELECT Prim.PRNum FROM
    (((Project 
    INNER JOIN MDS ON Project.PID=MDS.PID) 
    INNER JOIN PLocation ON Project.PID = PLocation.PID) 
    INNER JOIN Site ON PLocation.ACode = Site.ACode) 
AS [Prim]) 

LEFT JOIN 

(SELECT Prim2.PRNum FROM
    ((Procurement INNER JOIN MagicT ON Procurement.PRNum = MagicT.PRNum) 
    INNER JOIN DO ON DO.DoNum = MagicT.DONum) 
AS [Prim2])

ON Prim.PRNum = Prim2.PRNum

(编辑:有点太晚了......)

答案 2 :(得分:0)

我还发现了我的一个问题,就是在Access中,你可以在内连接中嵌套左连接或右连接但在左连接或右连接中不能有内连接。重写查询以在内部联接中不包括这些联接已解决了该问题。

感谢您的帮助。