Add a temporary column with a value in MySQL?


You can add a temporary column with value with the help of the following syntax −

select yourColumnName1,yourColumnName2,.....N ,yourTemporaryColumnValue as yourTemporaryColumnName from yourTableName;

To add a temporary column with a value, let us create a table. The following is the query −

mysql> create table TemporaryColumnWithValueDemo
   −> (
      −> StudentId int,
      −> StudentName varchar(100)
   −> );
Query OK, 0 rows affected (0.59 sec)

Inserting some records in the table. The query to insert records are as follows −

mysql> insert into TemporaryColumnWithValueDemo values(101,'John');
Query OK, 1 row affected (0.13 sec)

mysql> insert into TemporaryColumnWithValueDemo values(102,'Johnson');
Query OK, 1 row affected (0.15 sec)

mysql> insert into TemporaryColumnWithValueDemo values(103,'Carol');
Query OK, 1 row affected (0.14 sec)

mysql> insert into TemporaryColumnWithValueDemo values(104,'Sam');
Query OK, 1 row affected (0.16 sec)

Display all records inserted above. The query to display all records is as follows −

mysql> select *from TemporaryColumnWithValueDemo;

The following is the output −

+-----------+-------------+
| StudentId | StudentName |
+-----------+-------------+
|       101 | John        |
|       102 | Johnson     |
|       103 | Carol       |
|       104 | Sam         |
+-----------+-------------+
4 rows in set (0.00 sec)

Now here is the query to add a column with a t.emporary value. The query is as follows −

mysql> select StudentId,StudentName,'M.I.T.' as TempCollegeName from TemporaryColumnWithValueDemo;

The following is the output. The temporary column added successfully −

+-----------+-------------+-----------------+
| StudentId | StudentName | TempCollegeName |
+-----------+-------------+-----------------+
|       101 | John        | M.I.T.          |
|       102 | Johnson     | M.I.T.          |
|       103 | Carol       | M.I.T.          |
|       104 | Sam         | M.I.T.          |
+-----------+-------------+-----------------+
4 rows in set (0.00 sec)

Updated on: 25-Jun-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements