我有一系列三个表,我想在Excel VBA应用程序中使用ADODB将它们连接在一起。我正在使用以下查询,这会导致“不支持JOIN表达式”错误:
SELECT tb1.date,
tb1.longID,
tb1.fld1,
tb2.fld2,
tb3.shortID,
SUM(tb1.fld3) AS three,
SUM(tb1.fld4) AS four,
SUM(tb3.fld5) AS five
FROM ([Table1$] AS tb1 LEFT JOIN [Table2$] AS tb2 ON tb1.longID = tb2.longID)
LEFT JOIN [Table3$] AS tb3
ON (tb3.shortID = tb2.shortID AND tb1.date = tb3.date)
GROUP BY tb1.date, tb1.longID, tb3.shortID, tb2.fld3, tb1.fld2
如果我忽略shortID
列对,则查询工作正常。如果我省略date
列对,则查询工作正常。但是,当我将两者结合起来时,就遇到了问题。
任何帮助将不胜感激!
谢谢。
答案 0 :(得分:3)
尝试让查询的ON部分中的所有内容都放在括号中。
JOIN操作中的ON语句不完整或包含太多表。您可能需要将ON表达式放在WHERE子句中。
SELECT tb1.date,
tb1.longID,
tb1.fld1,
tb2.fld2,
tb3.shortID,
SUM(tb1.fld3) AS three,
SUM(tb1.fld4) AS four,
SUM(tb3.fld5) AS five
FROM
[Table1$] AS tb1
LEFT JOIN [Table2$] AS tb2 ON (tb1.longID = tb2.longID)
LEFT JOIN [Table3$] AS tb3 ON (tb3.shortID = tb2.shortID)
WHERE tb1.date = tb3.date
GROUP BY tb1.date, tb1.longID, tb3.shortID, tb2.fld3, tb1.fld2
答案 1 :(得分:1)
在线条款的目的是联接 2 表,但是您尝试使用ON (tb3.shortID = tb2.shortID AND tb1.date = tb3.date)
同时联接 3 表。您可以通过两种方式解决此问题:
将ON的一部分移至WHERE子句,以便仅涉及2个表。
...
FROM ([Table1$] AS tb1
LEFT JOIN [Table2$] AS tb2
ON tb1.longID = tb2.longID)
LEFT JOIN [Table3$] AS tb3
ON tb2.shortID = tb3.shortID
WHERE tb1.date = tb3.date
...
使用子查询
SELECT
x.date,
x.longID,
x.fld1,
x.fld2,
tb3.shortID,
SUM(x.fld3) AS three,
SUM(x.fld4) AS four,
SUM(tb3.fld5) AS five
FROM
(SELECT
tb1.date, tb1.longID, tb1.fld1,
tb2.fld2
FROM
[Table1$] AS tb1
LEFT JOIN [Table2$] AS tb2
ON tb1.longID = tb2.longID
) x
LEFT JOIN [Table3$] AS tb3
ON (x.shortID = tb3.shortID AND x.date = tb3.date)
GROUP BY
x.date, x.longID, x.fld1, x.fld2, tb3.shortID