尝试使用内部联接时缺少运算符的语法错误(访问 VBA)
Syntax error of missing operator when trying to use Inner Join (Access VBA)
我已经尝试了很长一段时间来修复下面代码中缺少的运算符错误,这是在我添加 INNER JOIN 之后发生的。
感谢任何帮助
Set qdf = CurrentDb.CreateQueryDef("", " SELECT PlantCode, PotSize, WeightToWatch, DateWatered, WeightAfter FROM ListWaterings " & _
" WHERE DateWatered = (SELECT MAX(T1.DateWatered) FROM ListWaterings AS T1 WHERE T1.PlantCode = ListWaterings.PlantCode) " & _
" INNER JOIN ListPlants ON ListPlants.PlantCode = ListWaterings.PlantCode "
我试图完成的结果如下
Table 称为 ListPlants(简体)
PlantCode
PotSize
WeightToWatch
18
15
1200
19
22
1450
一个叫 ListWaterings 的 Table
PlantCode
Date Watered
WeightAfter
18
23-03-2022 15:50
1250
19
23-03-2022 15:51
1500
18
23-03-2021 15:50
1300
19
23-03-2021 15:51
1550
我的查询结果看起来像
PlantCode
DateWatered
WeightAfter
Potsize
WeightToWatch
18
23-03-2022 15:50
1250
15
1200
19
23-03-2022 15:51
1500
22
1450
T2 只是
返回的标量值的别名
(SELECT MAX(T1.DateWatered) FROM ListWaterings AS T1 WHERE T1.PlantCode = ListWaterings.PlantCode)
您没有在其他任何地方引用它,所以您真的不需要它,当然也不需要在您的 JOIN 的 ON 子句中。我认为这只是一个错字。也许你的意思是
SELECT PlantCode,
DateWatered,
WeightAfter
FROM ListWaterings
WHERE DateWatered =
(SELECT MAX(T1.DateWatered)
FROM ListWaterings AS T1
WHERE T1.PlantCode = ListWaterings.PlantCode)
INNER JOIN ListPlants
ON ListPlants.PlantCode = ListWaterings.PlantCode
编辑我在 JOIN 之前放错了 WHERE。并且 OP 在输出中添加了更多字段,所以这里应该是正确的语法:
SELECT ListPlants.PlantCode,
ListWaterings.[DateWatered],
ListWaterings.WeightAfter,
ListPlants.PotSize,
ListPlants.WeightToWatch
FROM ListPlants
INNER JOIN ListWaterings
ON ListPlants.PlantCode = ListWaterings.PlantCode
WHERE ListWaterings.[DateWatered]=
(SELECT MAX(T1.DateWatered)
FROM ListWaterings AS T1
WHERE T1.PlantCode = ListWaterings.PlantCode)
我已经尝试了很长一段时间来修复下面代码中缺少的运算符错误,这是在我添加 INNER JOIN 之后发生的。
感谢任何帮助
Set qdf = CurrentDb.CreateQueryDef("", " SELECT PlantCode, PotSize, WeightToWatch, DateWatered, WeightAfter FROM ListWaterings " & _
" WHERE DateWatered = (SELECT MAX(T1.DateWatered) FROM ListWaterings AS T1 WHERE T1.PlantCode = ListWaterings.PlantCode) " & _
" INNER JOIN ListPlants ON ListPlants.PlantCode = ListWaterings.PlantCode "
我试图完成的结果如下
Table 称为 ListPlants(简体)
PlantCode | PotSize | WeightToWatch |
---|---|---|
18 | 15 | 1200 |
19 | 22 | 1450 |
一个叫 ListWaterings 的 Table
PlantCode | Date Watered | WeightAfter |
---|---|---|
18 | 23-03-2022 15:50 | 1250 |
19 | 23-03-2022 15:51 | 1500 |
18 | 23-03-2021 15:50 | 1300 |
19 | 23-03-2021 15:51 | 1550 |
我的查询结果看起来像
PlantCode | DateWatered | WeightAfter | Potsize | WeightToWatch |
---|---|---|---|---|
18 | 23-03-2022 15:50 | 1250 | 15 | 1200 |
19 | 23-03-2022 15:51 | 1500 | 22 | 1450 |
T2 只是
返回的标量值的别名(SELECT MAX(T1.DateWatered) FROM ListWaterings AS T1 WHERE T1.PlantCode = ListWaterings.PlantCode)
您没有在其他任何地方引用它,所以您真的不需要它,当然也不需要在您的 JOIN 的 ON 子句中。我认为这只是一个错字。也许你的意思是
SELECT PlantCode,
DateWatered,
WeightAfter
FROM ListWaterings
WHERE DateWatered =
(SELECT MAX(T1.DateWatered)
FROM ListWaterings AS T1
WHERE T1.PlantCode = ListWaterings.PlantCode)
INNER JOIN ListPlants
ON ListPlants.PlantCode = ListWaterings.PlantCode
编辑我在 JOIN 之前放错了 WHERE。并且 OP 在输出中添加了更多字段,所以这里应该是正确的语法:
SELECT ListPlants.PlantCode,
ListWaterings.[DateWatered],
ListWaterings.WeightAfter,
ListPlants.PotSize,
ListPlants.WeightToWatch
FROM ListPlants
INNER JOIN ListWaterings
ON ListPlants.PlantCode = ListWaterings.PlantCode
WHERE ListWaterings.[DateWatered]=
(SELECT MAX(T1.DateWatered)
FROM ListWaterings AS T1
WHERE T1.PlantCode = ListWaterings.PlantCode)