MySQL AS关键字

参见英文答案 > Why would you use “AS” when aliasing a SQL table?                                    8个
我在查询编写方面不专业,但从我开始使用MySQL时写了很多.最近我发现没有必要在名称别名中键入AS关键字.

SELECT name AS n

等于

SELECT name n

但是我知道这种能力来自多年前.关于这个问题,我有两个问题:

> AS关键字是多余的吗?
>有时候,当我在一个没有AS别名的网站上遇到自定义查询时,它正在执行MySQL服务,我改变了名称别名的方式,添加了AS关键字,这个小小的改动让它起作用了!

这是什么问题?

最佳答案
对于1)的答案,mysql文档http://dev.mysql.com/doc/refman/5.0/en/select.html

A select_expr can be given an alias using AS alias_name. The alias is
used as the expression’s column name and can be used in GROUP BY,
ORDER BY, or HAVING clauses. For example:

06000

The AS keyword is optional when aliasing a select_expr with an
identifier. The preceding example could have been written like this:

06001

However, because the AS is optional, a subtle problem can occur if you
forget the comma between two select_expr expressions: MySQL interprets
the second as an alias name. For example, in the following statement,
columnb is treated as an alias name:

06002

For this reason, it is good practice to be in the habit of using AS
explicitly when specifying column aliases.

It is not permissible 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 C.5.5.4, “Problems with Column
Aliases”.

对于表,这在语法上也非常相似:

A table reference can be aliased using tbl_name AS alias_name or
tbl_name alias_name:

06003

对于2)的答案我不认为没有完整的软件版本描述,运行查询和收到错误消息,你无法告诉我.

转载注明原文:MySQL AS关键字 - 代码日志