How to SELECT * and rename a column in MySQL?

MySQLMySQLi Database

Let us first create a table −

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

Insert records in the table using insert command −

mysql> insert into DemoTable(Name,Age) values('John',21);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable(Name,Age) values('Carol',24);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable(Name,Age) values('David',22);
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable(Name,Age) values('Sam',23);
Query OK, 1 row affected (0.23 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable;

This will produce the following output −

+----+-------+------+
| Id | Name  | Age  |
+----+-------+------+
| 1  | John  | 21   |
| 2  | Carol | 24   |
| 3  | David | 22   |
| 4  | Sam   | 23   |
+----+-------+------+
4 rows in set (0.00 sec)

Following is the query to SELECT * and rename a column −

mysql> select tbl.*,Name AS StudentName from DemoTable tbl;

This will produce the following output −

+----+-------+------+-------------+
| Id | Name  | Age  | StudentName |
+----+-------+------+-------------+
| 1  | John  | 21   | John        |
| 2  | Carol | 24   | Carol       |
| 3  | David | 22   | David       |
| 4  | Sam   | 23   | Sam         |
+----+-------+------+-------------+
4 rows in set (0.00 sec)
raja
Published on 03-May-2019 12:20:33
Advertisements