Add to existing value in MySQL column using CONCAT function?

MySQLMySQLi Database

To understand the concept, let us first create a demo table.

mysql> create table addToExistingValueDemo
   -> (
   -> Instructor_Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> Instructor_Name varchar(30),
   -> Instructor_TechnicalSubject text
   -> );
Query OK, 0 rows affected (0.54 sec)

Insert some records in the table using insert command. The query is as follows −

mysql> insert into addToExistingValueDemo(Instructor_Name,Instructor_TechnicalSubject) values('John','C,C++');
Query OK, 1 row affected (0.15 sec)
mysql> insert into addToExistingValueDemo(Instructor_Name,Instructor_TechnicalSubject) values('Carol','Java,Python');
Query OK, 1 row affected (0.18 sec)
mysql> insert into addToExistingValueDemo(Instructor_Name,Instructor_TechnicalSubject) values('Bob','MySQL,SQL Server');
Query OK, 1 row affected (0.15 sec)
mysql> insert into addToExistingValueDemo(Instructor_Name,Instructor_TechnicalSubject) values('David','DataStructure');
Query OK, 1 row affected (0.18 sec

Display all records from the table using select statement. The query is as follows −

mysql> select *from addToExistingValueDemo;

The following is the output

+---------------+-----------------+-----------------------------+
| Instructor_Id | Instructor_Name | Instructor_TechnicalSubject |
+---------------+-----------------+-----------------------------+
|             1 | John            | C,C++                       |
|             2 | Carol           | Java,Python                 |
|             3 | Bob             | MySQL,SQL Server            |
|             4 | David           | DataStructure               |
+---------------+-----------------+-----------------------------+
4 rows in set (0.00 sec)

Here is the query to add to existing value in MySQL column using CONCAT function

mysql> update addToExistingValueDemo
   -> set Instructor_TechnicalSubject=concat(Instructor_TechnicalSubject,', Introduction To Algorithm')
   -> where Instructor_Id=4;
Query OK, 1 row affected (0.10 sec)
Rows matched: 1 Changed: 1 Warnings: 0

Let us check the table records once again to see the new changes. The query is as follows −

mysql> select *from addToExistingValueDemo;

The following is the output

+---------------+-----------------+------------------------------------------+
| Instructor_Id | Instructor_Name | Instructor_TechnicalSubject              |
+---------------+-----------------+------------------------------------------+
|             1 | John            | C,C++                                    |
|             2 | Carol           | Java,Python                              |
|             3 | Bob             | MySQL,SQL Server                         |
|             4 | David           | DataStructure, Introduction To Algorithm |
+---------------+-----------------+------------------------------------------+
4 rows in set (0.00 sec)
raja
Published on 01-Apr-2019 11:20:04
Advertisements