mysql 5.7 表别名_MySQL5.7中别名使用(GROUP BY, ORDER BY, 或者 HAVING 中,不能用于where中)...

本文介绍了MySQL中别名在查询语句中的使用规定,强调了alias只能在GROUP BY, ORDER BY, 或 HAVING子句中使用,而在WHERE子句中引用列别名是标准SQL所不允许的,因为WHERE子句的评估时机可能导致列值未知。举例说明了这一限制及其原因。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

官网文档:

https://dev.mysql.com/doc/refman/8.0/en/problems-with-alias.html

别名只能用于 GROUP BY, ORDER BY, 或者 HAVING 中,不能用于where中。

原因如下:

An alias can be used in a query select list to give a column a different name. You can use the alias in GROUP BY, ORDER BY, or HAVING clauses to refer to the column:

SELECT SQRT(a*b) AS root FROM tbl_name

GROUP BY root HAVING root > 0;

SELECT id, COUNT(*) AS cnt FROM tbl_name

GROUP BY id HAVING cnt > 0;

SELECT id AS 'Customer identity' FROM tbl_name;

Standard SQL disallows references to column aliases in a WHERE clause. This restriction is imposed because when the WHERE clause is evaluated, the column value may not yet have been determined. For example, the following query is illegal:

SELECT id, COUNT(*) AS cnt FROM tbl_name

WHERE cnt > 0 GROUP BY id;

The WHERE clause determines which rows should be included in the GROUP BY clause, but it refers to the alias of a column value that is not known until after the rows have been selected, and grouped by the GROUP BY.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值