更新时间:2023-02-05 07:39:48
您可以使用 HAVING 子句,可以看到别名,例如
You could use a HAVING clause, which can see the aliases, e.g.
HAVING avg_rating>5
但是在 where 子句中,您需要重复您的表达式,例如
but in a where clause you'll need to repeat your expression, e.g.
WHERE (sum(reviews.rev_rating)/count(reviews.rev_id))>5
但是!并非所有表达式都被允许 - 使用 SUM 之类的聚合函数将不起作用,在这种情况下,您需要使用 HAVING 子句.
BUT! Not all expressions will be allowed - using an aggregating function like SUM will not work, in which case you'll need to use a HAVING clause.
来自 MySQL 手册:
不允许引用WHERE 子句中的列别名,因为列值可能还没有当 WHERE 子句被确定被执行.参见 B.1.5.4 节,列别名问题".
It is not allowable to refer to a column alias in a WHERE clause, because the column value might not yet be determined when the WHERE clause is executed. See Section B.1.5.4, "Problems with Column Aliases".