Appending data to a MySQL field that already has data in it?

MySQLMySQLi Database

You can append data to a MySQL database field with the help of in-built CONCAT() function.

The syntax is as follows −

update yourTableName set yourColumnName = CONCAT(yourColumnName,’AppendValue’);

To understand the above concept, let us create a table. The query to create a table −

mysql> create table AppendingDataDemo
   −> (
   −> FirstNameAndLastName varchar(200)
   −> );
Query OK, 0 rows affected (0.59 sec)

Insert some records in the table with the help of insert command. The query is as follows −

mysql> insert into AppendingDataDemo values('John');
Query OK, 1 row affected (0.27 sec)

mysql> insert into AppendingDataDemo values('Bob');
Query OK, 1 row affected (0.20 sec)

mysql> insert into AppendingDataDemo values('Carol');
Query OK, 1 row affected (0.30 sec)

Display all records with the help of select statement −

mysql> select *from AppendingDataDemo;

The following is the output −

+----------------------+
| FirstNameAndLastName |
+----------------------+
| John                 |
| Bob                  |
| Carol                |
+----------------------+
3 rows in set (0.00 sec)

Here is the query to append the data ”Taylor” to the data already in the column. Therefore, the data will get appended.

The query is as follows −

mysql> update AppendingDataDemo set FirstNameAndLastName = concat(FirstNameAndLastName,' Taylor');
Query OK, 3 rows affected (0.10 sec)
Rows matched: 3 Changed: 3 Warnings: 0

Now you can check with select statement that data has been appended or not. The query is as follows −

mysql> select *from AppendingDataDemo;

The following is the output −

+----------------------+
| FirstNameAndLastName |
+----------------------+
| John Taylor          |
| Bob Taylor           |
| Carol Taylor         |
+----------------------+
3 rows in set (0.00 sec)
raja
Published on 11-Jan-2019 11:27:03
Advertisements