mysql 的 WHERE 子句中可以使用别名吗?

发布于 2024-07-08 07:39:04 字数 174 浏览 4 评论 0原文

我需要在 WHERE 子句中使用别名,但它一直告诉我它是一个未知列。 有什么办法可以解决这个问题吗? 我需要选择评级高于 x 的记录。 评级按以下别名计算:

sum(reviews.rev_rating)/count(reviews.rev_id) as avg_rating

I need to use an alias in the WHERE clause, but It keeps telling me that its an unknown column. Is there any way to get around this issue? I need to select records that have a rating higher than x. Rating is calculated as the following alias:

sum(reviews.rev_rating)/count(reviews.rev_id) as avg_rating

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

苄①跕圉湢 2024-07-15 07:39:04

您可以使用 HAVING 子句,它可以看到别名,例如,

 HAVING avg_rating>5

但在 where 子句中,您需要重复表达式,例如

 WHERE (sum(reviews.rev_rating)/count(reviews.rev_id))>5

BUT! 并非所有表达式都被允许 - 使用 SUM 这样的聚合函数将不起作用,在这种情况下,您需要使用 HAVING 子句。

来自 MySQL 手册

不允许引用
WHERE 子句中的列别名,
因为列值可能还没有
当 WHERE 子句被确定时
被执行。 请参阅第 B.1.5.4 节,
“列别名的问题”

You could use a HAVING clause, which can see the aliases, e.g.

 HAVING avg_rating>5

but in a where clause you'll need to repeat your expression, e.g.

 WHERE (sum(reviews.rev_rating)/count(reviews.rev_id))>5

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.

From the MySQL Manual:

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”
.

耀眼的星火 2024-07-15 07:39:04

我不知道这在 mysql 中是否有效,但是使用 sqlserver 你也可以像这样包装它:

select * from (
  -- your original query
  select .. sum(reviews.rev_rating)/count(reviews.rev_id) as avg_rating 
  from ...) Foo
where Foo.avg_rating ...

I don't know if this works in mysql, but using sqlserver you can also just wrap it like:

select * from (
  -- your original query
  select .. sum(reviews.rev_rating)/count(reviews.rev_id) as avg_rating 
  from ...) Foo
where Foo.avg_rating ...
还给你自由 2024-07-15 07:39:04

这个问题已经很老了,一个答案已经获得了 160 票......

不过我还是要澄清这一点:问题实际上不是关于是否可以在 WHERE 条款。

sum(reviews.rev_rating) / count(reviews.rev_id) as avg_rating

是一个聚合。 在 WHERE 子句中,我们通过查看表中的值来限制我们想要的记录。 然而,sum(reviews.rev_ rating) 和 count(reviews.rev_id) 并不是我们在记录中找到的值; 它们是我们只有在汇总记录后才能获得的值。

所以 WHERE 是不合适的。 我们需要HAVING,因为我们想在聚合后限制结果行。 不可能是

WHERE avg_rating > 10

这样,也

WHERE sum(reviews.rev_rating) / count(reviews.rev_id) > 10

不可能是这样。

HAVING sum(reviews.rev_rating) / count(reviews.rev_id) > 10

另一方面是可能的并且符合SQL标准。 而

HAVING avg_rating > 10

只能在 MySQL 中实现。 根据标准,它不是有效的 SQL,因为 SELECT 子句应该在 HAVING 之后执行。 来自 MySQL 文档:

标准 SQL 的另一个 MySQL 扩展允许在 HAVING 子句中引用选择列表中的别名表达式。

MySQL 扩展允许在聚合列的 HAVING 子句中使用别名

https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html

This question is quite old and one answer already gained 160 votes...

Still I would make this clear: The question is actually not about whether alias names can be used in the WHERE clause.

sum(reviews.rev_rating) / count(reviews.rev_id) as avg_rating

is an aggregation. In the WHERE clause we restrict records we want from the tables by looking at their values. sum(reviews.rev_rating) and count(reviews.rev_id), however, are not values we find in a record; they are values we only get after aggregating the records.

So WHERE is inappropriate. We need HAVING, as we want to restrict result rows after aggregation. It can't be

WHERE avg_rating > 10

nor

WHERE sum(reviews.rev_rating) / count(reviews.rev_id) > 10

hence.

HAVING sum(reviews.rev_rating) / count(reviews.rev_id) > 10

on the other hand is possible and complies with the SQL standard. Whereas

HAVING avg_rating > 10

is only possible in MySQL. It is not valid SQL according to the standard, as the SELECT clause is supposed to get executed after HAVING. From the MySQL docs:

Another MySQL extension to standard SQL permits references in the HAVING clause to aliased expressions in the select list.

The MySQL extension permits the use of an alias in the HAVING clause for the aggregated column

https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html

执着的年纪 2024-07-15 07:39:04
SELECT * FROM (SELECT customer_Id AS 'custId', gender, age FROM customer
    WHERE  gender = 'F') AS c
WHERE c.custId = 100;
SELECT * FROM (SELECT customer_Id AS 'custId', gender, age FROM customer
    WHERE  gender = 'F') AS c
WHERE c.custId = 100;
心的位置 2024-07-15 07:39:04

如果您的查询是静态的,您可以将其定义为视图,然后在查询视图时可以在 where 子句中使用该别名。

If your query is static, you can define it as a view then you can use that alias in the where clause while querying the view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文