What is the MySQL alias shorthand?

MySQLMySQLi Database

You need to give name explicitly or you can remove AS command. Let us first create a table −

mysql> create table DemoTable
   (
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   Name varchar(20)
   );
Query OK, 0 rows affected (0.21 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(Name) values('John');
Query OK, 1 row affected (0.18 sec)

mysql> insert into DemoTable(Name) values('Larry');
Query OK, 1 row affected (0.07 sec)

mysql> insert into DemoTable(Name) values('Sam');
Query OK, 1 row affected (0.06 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----+-------+
| Id | Name  |
+----+-------+
| 1  | John  |
| 2  | Larry |
| 3  | Sam   |
+----+-------+
3 rows in set (0.00 sec)

Case 1 − Query for MySQL alias with AS command.

mysql> select Id,Name AS FirstName from DemoTable;

This will produce the following output −

+----+-----------+
| Id | FirstName |
+----+-----------+
| 1  | John      |
| 2  | Larry     |
| 3  | Sam       |
+----+-----------+
3 rows in set (0.00 sec)

Case 2: Query for MySQL alias without AS −

mysql> select Id,Name FirstName from DemoTable;

This will produce the following output −

+----+-----------+
| Id | FirstName |
+----+-----------+
| 1  | John      |
| 2  | Larry     |
| 3  | Sam       |
+----+-----------+
3 rows in set (0.00 sec)
raja
Published on 20-May-2019 08:42:46
Advertisements