GNU Info

Info Node: (mysql.info)Problems with alias

(mysql.info)Problems with alias


Next: Deleting from related tables Prev: Problems with NULL Up: Query Issues
Enter node , (file) or (file)node

Problems with `alias'
---------------------

You can use an alias to refer to a column in the `GROUP BY', `ORDER
BY', or in the `HAVING' part.  Aliases can also be used to give columns
better names:

     SELECT SQRT(a*b) as rt FROM table_name GROUP BY rt HAVING rt > 0;
     SELECT id,COUNT(*) AS cnt FROM table_name GROUP BY id HAVING cnt > 0;
     SELECT id AS "Customer identity" FROM table_name;

Note that ANSI SQL doesn't allow you to refer to an alias in a `WHERE'
clause.  This is because when the `WHERE' code is executed the column
value may not yet be determined. For example, the following query is
*illegal*:

     SELECT id,COUNT(*) AS cnt FROM table_name WHERE cnt > 0 GROUP BY id;

The `WHERE' statement is executed to determine which rows should be
included in the `GROUP BY' part while `HAVING' is used to decide which
rows from the result set should be used.


automatically generated by info2www version 1.2.2.9