Giving Names to Output Columns
Problem
You don’t like the names of the columns in your query result.
Solution
Supply names of your own choosing using column aliases.
Discussion
Whenever you retrieve a result set, MySQL gives every output column a name. (That’s how the mysql program gets the names that you see displayed as the initial row of column headers in result set output.) MySQL assigns default names to output columns, but if the defaults are not suitable, you can use column aliases to specify your own names.
This section explains aliases and shows how to use them to assign column names in queries. If you’re writing a program that needs to retrieve information about column names, see Recipe 9.3.
If an output column in a result set comes directly from a table, MySQL uses the table column name for the result set column name. For example, the following statement selects three table columns, the names of which become the corresponding output column names:
mysql> SELECT t, srcuser, size FROM mail;
+---------------------+---------+---------+
| t | srcuser | size |
+---------------------+---------+---------+
| 2001-05-11 10:15:08 | barb | 58274 |
| 2001-05-12 12:48:13 | tricia | 194925 |
| 2001-05-12 15:02:49 | phil | 1048 |
| 2001-05-13 13:59:18 | barb | 271 |
...
If you generate a column by evaluating an expression, the expression itself is the column name. This can produce rather long and unwieldy names in result sets, as illustrated by the following query that uses an expression to ...
Get MySQL Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.