MySQL 5.1 リファレンスマニュアル :: B Errors, Error Codes, and Common Problems :: B.1 Problems and Common Errors :: B.1.5 Query-Related Issues :: B.1.5.4 Problems with Column Aliases
« B.1.5.3 Problems with NULL Values

B.1.5.5 Rollback Failure for Non-Transactional Tables »
Section Navigation      [Toggle]
  • B.1.5 Query-Related Issues
  • B.1.5.1 Case Sensitivity in Searches
  • B.1.5.2 Problems Using DATE Columns
  • B.1.5.3 Problems with NULL Values
  • B.1.5.4 Problems with Column Aliases
  • B.1.5.5 Rollback Failure for Non-Transactional Tables
  • B.1.5.6 Deleting Rows from Related Tables
  • B.1.5.7 Solving Problems with No Matching Rows
  • B.1.5.8 Problems with Floating-Point Comparisons

B.1.5.4. Problems with Column Aliases

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

SELECT SQRT(a*b) AS root FROM tbl_name GROUP BY root HAVING root > 0;
SELECT id, COUNT(*) AS cnt FROM tbl_name GROUP BY id HAVING cnt > 0;
SELECT id AS 'Customer identity' FROM tbl_name;

Standard SQL doesn't allow you to refer to a column alias in a WHERE clause. This restriction is imposed 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 tbl_name WHERE cnt > 0 GROUP BY id;

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

Copyright © 1997, 2010, Oracle and/or its affiliates. All rights reserved. Legal Notices
Top / Previous / Next / Up / Table of Contents
© 2010, Oracle Corporation and/or its affiliates