
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to SELECT * and rename a column in MySQL?
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)
- Related Questions & Answers
- Rename column name in MySQL?
- How to rename a column in an existing MySQL table?
- How to rename a table in MySQL?
- How to rename column names in a Pandas DataFrame?
- How to select a column name with spaces in MySQL?
- Rename a table in MySQL using RENAME TABLE command
- How to select the maximum value of a column in MySQL?
- How to rename a single column in an R data frame?
- How to rename an existing column of a table in PostgreSQL?
- Select multiple columns and display in a single column in MySQL?
- How to select ID column as null in MySQL?
- Python - Rename column names by index in a Pandas DataFrame without using rename()
- Select column names containing a string in MySQL?
- How to get a specific column record from SELECT query in MySQL?
- How to select only MySQL date from datetime column?
Advertisements