MySQL AS 关键字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19567732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:13:19  来源:igfitidea点击:

MySQL AS keyword

mysql

提问by revo

I'm not professional in query writing, but wrote many from the time I began MySQL. Recently I noticed there is no need to type ASkeyword in name aliasing.

我在查询写作方面并不专业,但从我开始使用 MySQL 开始就写了很多。最近我注意到没有必要AS在名称别名中键入关键字。

SELECT name AS n

equals to

等于

SELECT name n

However I know that this ability is out from years ago. I've 2 questions around this subject:

不过我知道这个能力是多年前就出来的。我有两个关于这个主题的问题:

  1. Is ASkeyword redundant?
  2. Sometimes ago when I encountered with a custom query on a website with having no ASin aliasing, that its executing made MySQL service down, I changed the way of name aliasing with adding ASkeyword and this little change made it work!

    What was the problem here?

  1. AS关键字是多余的吗?
  2. 有时,当我在一个没有AS别名的网站上遇到自定义查询时,它的执行使 MySQL 服务关闭,我通过添加AS关键字更改了名称别名的方式,这个小改动使它起作用了!

    这里的问题是什么?

回答by ModulusJoe

For the answer to 1), the mysql documentation http://dev.mysql.com/doc/refman/5.0/en/select.html:

对于 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 HAVINGclauses. For example:

SELECT CONCAT(last_name,', ',first_name) AS full_name   FROM mytable
ORDER BY full_name;

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

SELECT CONCAT(last_name,', ',first_name) full_name   FROM mytable
ORDER BY full_name;

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:

SELECT columna columnb FROM mytable;

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

可以使用 AS alias_name 为 select_expr 赋予一个别名。别名用作表达式的列名,并且可以在被使用GROUP BYORDER BYHAVING条款。例如:

SELECT CONCAT(last_name,', ',first_name) AS full_name   FROM mytable
ORDER BY full_name;

当使用标识符为 select_expr 设置别名时,AS 关键字是可选的。前面的例子可以这样写:

SELECT CONCAT(last_name,', ',first_name) full_name   FROM mytable
ORDER BY full_name;

但是,因为 AS 是可选的,如果您忘记了两个 select_expr 表达式之间的逗号,则会出现一个微妙的问题:MySQL 将第二个解释为别名。例如,在以下语句中,columnb 被视为别名:

SELECT columna columnb FROM mytable;

因此,在指定列别名时养成明确使用 AS 的习惯是一种很好的做法。

不允许在 WHERE 子句中引用列别名,因为在执行 WHERE 子句时可能尚未确定列值。请参见第 C.5.5.4 节“列别名问题”。

This also very similar syntactically for tables:

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

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

SELECT t1.name, t2.salary FROM employee AS t1, info AS t2   WHERE
t1.name = t2.name;

SELECT t1.name, t2.salary FROM employee t1, info t2   WHERE t1.name =
t2.name;

可以使用 tbl_name AS alias_name 或 tbl_name alias_name 为表引用设置别名:

SELECT t1.name, t2.salary FROM employee AS t1, info AS t2   WHERE
t1.name = t2.name;

SELECT t1.name, t2.salary FROM employee t1, info t2   WHERE t1.name =
t2.name;

For the Answer to 2) I don't think there is anyway you can tell without a full description of the software versions, query being run and error messages received.

对于 2) 的答案,我认为如果没有对软件版本、正在运行的查询和收到的错误消息的完整描述,您就无法判断。