Access 2007中的子查询问题

时间:2019-06-23 13:30:42

标签: sql ms-access ms-access-2007

我有一个使用Access 2007的项目,但是正在努力编写一个简单的子查询。在尝试记录一定比率后,我试图从接下来的6次出现的表中获取结果。

我希望以下代码可以在Access中使用:

SELECT [runner name], [race ID], [race date], [placing], [class]
FROM runners
WHERE [runner name] IN
                 (SELECT [runner name] from 6F Full Form WHERE [ratio] >9       
ORDER BY [runner name], [race ID], [date], [placing], [class];

我收到以下错误消息。注意6F Full Form是我编写的查询,我正尝试在子查询中使用它。

  

查询表达式'[runner name] IN(从6F Full Form WHERE [ratio]> 9 中选择[runner name]的语法错误)

2 个答案:

答案 0 :(得分:0)

尝试一下:

 (SELECT [runner name] 
 FROM [6F Full Form] 
 WHERE [ratio] >9       
 ORDER BY [runner name], [race ID], [date], [placing], [class]);

答案 1 :(得分:0)

当前SQL代码的问题在于,包含空格或reserved words的字段/表/查询名称必须用方括号括起来-因此

from 6F Full Form WHERE

应写为:

from [6F Full Form] WHERE

您的order by子句中也可能有错字:

SELECT ..., [race date], ...
...
ORDER BY ..., [date], ...;
                 ^--------------- Should this be [race date]?

值得补充的是,您也可以使用联接来实现此目的,例如:

select
    r.[runner name],
    r.[race id],
    r.[race date],
    r.[placing],
    r.[class]
from
    runners r inner join
    (select distinct f.[runner name] from [6f full form] f where f.[ratio] > 9) q 
    on r.[runner name] = q.[runner name]
order by
    r.[runner name],
    r.[race id],
    r.[race date],
    r.[placing],
    r.[class]