3.8.1 Problem
You want to refer to a column alias in a WHERE clause.
3.8.2 Solution
Sorry, you cannot.
3.8.3 Discussion
You cannot refer to column aliases in a WHERE clause. Thus, the following query is illegal:
mysql> SELECT t, srcuser, dstuser, size/1024 AS kilobytes -> FROM mail WHERE kilobytes > 500; ERROR 1054 at line 1: Unknown column 'kilobytes' in 'where clause'
The error occurs because aliases name output columns, whereas a WHERE clause operates on input columns to determine which rows to select for output. To make the query legal, replace the alias in the WHERE clause with the column or expression that the alias represents:
mysql> SELECT t, srcuser, dstuser, size/1024 AS kilobytes -> FROM mail WHERE size/1024 > 500; +---------------------+---------+---------+-----------+ | t | srcuser | dstuser | kilobytes | +---------------------+---------+---------+-----------+ | 2001-05-14 17:03:01 | tricia | phil | 2338.36 | | 2001-05-15 10:25:52 | gene | tricia | 975.13 | +---------------------+---------+---------+-----------+
Using the mysql Client Program
Writing MySQL-Based Programs
Record Selection Techniques
Working with Strings
Working with Dates and Times
Sorting Query Results
Generating Summaries
Modifying Tables with ALTER TABLE
Obtaining and Using Metadata
Importing and Exporting Data
Generating and Using Sequences
Using Multiple Tables
Statistical Techniques
Handling Duplicates
Performing Transactions
Introduction to MySQL on the Web
Incorporating Query Resultsinto Web Pages
Processing Web Input with MySQL
Using MySQL-Based Web Session Management
Appendix A. Obtaining MySQL Software
Appendix B. JSP and Tomcat Primer
Appendix C. References