sql-access:HAVING子句和!=运算符

时间:2010-05-12 02:15:31

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

我是新手来访问sql,我构建了一个如下所示的select语句:

SELECT [Lab Occurrence Form].[Practice Code], 
       Count([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]) AS [CountOf1 0 Preanalytical (Before Testing)], 
       [Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]
  FROM [Lab Occurrence Form]
 WHERE ((([Lab Occurrence Form].[Occurrence Date]) Between #9/1/2009# And #9/30/2009#))
having ([CountOf1 0 Preanalytical (Before Testing)] != 0)
GROUP BY [Lab Occurrence Form].[Practice Code], [Lab Occurrence Form].[1 0 Preanalytical (Before Testing)];

它不喜欢我的HAVING条款。这有什么问题?我该怎么做!= ??

2 个答案:

答案 0 :(得分:2)

它不起作用,因为HAVING子句必须 GROUP BY之后 - 使用:

  SELECT [Lab Occurrence Form].[Practice Code], 
         Count([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]) AS [CountOf1 0 Preanalytical (Before Testing)], 
         [Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]
    FROM [Lab Occurrence Form]
   WHERE ((([Lab Occurrence Form].[Occurrence Date]) Between #9/1/2009# And #9/30/2009#))
GROUP BY [Lab Occurrence Form].[Practice Code], [Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]
  HAVING Count([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]) != 0);

此外,您通常不能在GROUP BY / HAVING子句中使用列别名。

!=是ANSI 92吗?标准,但<>也应该有用。

答案 1 :(得分:1)

使用<>。 IIRC,!=将在MySQL工作