• Teach Your Self With
             w3htmlschool.com

MySQL: Aliases

The MySQL ALIASES can be used to create a temporary or Alternative name for columns or tables.

• COLUMN ALIASES are used to give column headings in your result set , so that it is easier to read.
• TABLE ALIASES is used for giving alternative name to table .so that it is easier to read in SQL statement.In Mysql statement tables can be given an alias alternative name for local use in the statement

• When you write SQL query using TABLE ALIASES a column of an aliased table, the alias must be used as qualifier – not the table name.
• Always alias follows after the table name

Syntax


SELECT column as COLUMN ALIASES
FROM table as TABLE ALIASES
Table
country

+----------------+------------+
| Field | Type |
+----------------+------------+
| Code | char(3) |
| Name | char(52) |
| Continent | char(40) |
| Region | char(26) |
| SurfaceArea | float(10,2)|
| IndepYear | smallint(6)|
| Population | int(11) |
| LifeExpectancy | float(3,1) |
| GNP | float(10,2)|
| GNPOld | float(10,2)|
| LocalName | char(45) |
| GovernmentForm | char(45) |
| HeadOfState | char(60) |
| Capital | int(11) |
| Code2 | char(2) |
+----------------+------------+
City

+-------------+----------+
| Field | Type |
+-------------+----------+
| ID | int(11) |
| Name | char(35) |
| CountryCode | char(3) |
| District | char(20) |
| Population | int(11) |
+-------------+----------+
Example
SELECT C.Name, Country.Name
FROM Country, City AS C
WHERE Capital = C.ID;
Output
mysql> SELECT C.Name, Country.Name
-> FROM Country, City AS C
-> WHERE Capital = C.ID;
+------------+----------------------+
| Name | Name |
+------------+----------------------+
| Kabul | Afghanistan |
| Amsterdam | Netherlands |
| Willemstad | Netherlands Antilles |
| Tirana | Albania |
| Alger | Algeria |
+------------+----------------------+
5 rows in set (0.41 sec)