Rechercher dans le manuel MySQL
B.4.4.4 Problems with Column Aliases
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:
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:
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
.
In the select list of a query, a quoted column alias can be specified using identifier or string quoting characters:
Elsewhere in the statement, quoted references to the alias
must use identifier quoting or the reference is treated as a
string literal. For example, this statement groups by the
values in column id
, referenced using the
alias `a`
:
But this statement groups by the literal string
'a'
and will not work as expected:
Document created the 26/06/2006, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/mysql-rf-problems-with-alias.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.