我可以在 SQL 查询中使用 HAVING 而不是 WHERE 吗?

Can I use HAVING instead of WHERE in SQL queries?

我一直以为我做不到,但MSDN说不是。

When GROUP BY is not used, HAVING behaves like a WHERE clause.

我已经检查并收到错误:

Msg 8121:
Column '...' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY.

那么,它是什么?文档中的错误,还是鲜为人知的细节?

这可能是在 Sybase SQL 服务器中起作用的那些东西之一。现在 Microsoft 重写了 SQL 服务器更接近 ANSI 标准,但忘记修复文档。

是这样的吗?

您可以在不使用 group by 的情况下使用 having,但可以使用聚合函数。

select avg(price) from tbltemp having avg(price) >= 2

在您提供的 MSDN link 中,给出的示例指示我们使用 where 而不是 having。

You may NOT write something like "select avg(price) from goods HAVING price >= 1000" but you may write "select avg(price) from goods WHERE price >= > 1000"