SQL has the capability to give new labels to the columns in the result set.
Syntax 1
SELECT column_name1 alias_name1, column_name2 alias_name2.....FROM {table_name}
Syntax 2
SELECT column_name1 AS alias_name1, column_name2 AS alias_name2.....FROM {table_name}
mysql> SELECT * FROM employee;
+--------+------------+-----------+
| emp_id | first_name | last_name |
+--------+------------+-----------+
| 1 | krishna | gurram |
| 2 | gopi | battu |
| 3 | joel | chelli |
+--------+------------+-----------+
3 rows in set (0.00 sec)
Example 1: Print first_name as emp_firstName using syntax1.
SELECT emp_id, first_name emp_first_name, last_name FROM employee;
mysql> SELECT emp_id, first_name emp_first_name, last_name FROM employee;
+--------+----------------+-----------+
| emp_id | emp_first_name | last_name |
+--------+----------------+-----------+
| 1 | krishna | gurram |
| 2 | gopi | battu |
| 3 | joel | chelli |
+--------+----------------+-----------+
3 rows in set (0.00 sec)
Example 2: Print first_name as emp_firstName using syntax2.
SELECT emp_id, first_name AS emp_first_name, last_name FROM employee;mysql> SELECT emp_id, first_name AS emp_first_name, last_name FROM employee;
+--------+----------------+-----------+
| emp_id | emp_first_name | last_name |
+--------+----------------+-----------+
| 1 | krishna | gurram |
| 2 | gopi | battu |
| 3 | joel | chelli |
+--------+----------------+-----------+
3 rows in set (0.00 sec)
I prefer to use ‘AS’ keyword, since it is more readable.
No comments:
Post a Comment