SQL Query To Append Data

If there exist some record already, and you need to append a string with an existing value of a field. You can use CONCAT function for it. Assume we have a table named students which has following record before we execute the query:


mysql> select * from students;
+----+----------------------+------------------+------------+
| id | name | email | phone |
+----+----------------------+------------------+------------+
| 1 | Alice | alice@gmail.com | 5487845784 |
| 2 | Bob Chris | bob@gmail.com | 879874578 |
| 3 | Oracle | oracle@gmail.com | 548798453 |
+----+----------------------+------------------+------------+

To concat a string with name Alice, record id 1, you can execute following query:

update students set name = CONCAT(name, " Lewis") where id=1

After the query, the record would look like this:
mysql> select * from students;
+----+-------------+------------------+------------+
| id | name | email | phone |
+----+-------------+------------------+------------+
| 1 | Alice Lewis | alice@gmail.com | 5487845784 |
| 2 | Bob Chris | bob@gmail.com | 879874578 |
| 3 | Oracle | oracle@gmail.com | 548798453 |
+----+-------------+------------------+------------+

Comments