Kimberypalet
Kimberypalet

Reputation: 119

Is it possible to insert another value from database with existing value?

Is it possible to insert or append another value from database cell with existing data?

Example table:

     tbl_name
------------------
|  id  |  Name  |
-----------------
|   0  |  Jones |
|   1  |  Bryan |
|   2  |  Fate  |
------------------

For example I want to add , james (on table row 1 column name). The result must be:

------------------------
|  id  |  Name         |
------------------------
|   0  |  Jones        |
|   1  |  Bryan, james |
|   2  |  Fate         |
------------------------

Query:

insert into tbl_name (name) Values (, james) WHERE id = 1

This query does not work.

Upvotes: 0

Views: 81

Answers (2)

Stephan Kulow
Stephan Kulow

Reputation: 66

What you want is UPDATE:

MariaDB [db]> update tbl_name set text=concat(text, ", james") where id=1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [db]> select * from tbl_name;
+------+--------------+
| id   | text         |
+------+--------------+
|    0 | Jones        |
|    1 | Bryan, james |
|    2 | Fate         |
+------+--------------+

Upvotes: 2

ScaisEdge
ScaisEdge

Reputation: 133400

for change existing content You can use update

 update my_table  
 set Name  = concat(name , ', james')
 where id = 1

anyway the use of comma separated value is often based on bad design .. try take a look at mysql json fields try a normalization for multiple values

Upvotes: 2

Related Questions