mysql as,MySQL AS关键字

  • Post author:
  • Post category:mysql


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 AS keyword in name aliasing.

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:

Is AS keyword redundant?

Sometimes ago when I encountered with a custom query on a website with having no AS in aliasing, that its executing made MySQL service down, I changed the way of name aliasing with adding AS keyword and this little change made it work!

What was the problem here?

解决方案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:

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

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;

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.