Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update a database record and still keep the old values in MySQL?

I want to update a column by adding a new value alongside the old ones. So if column "fruits" has a value of "apples" and I run my query it should then have a value of "apples, oranges".

Right now when I do an update statement"

UPDATE tableName SET fruits='oranges' WHERE id=1;

It just overwrites apples with oranges. How can I get it to ADD the new value alongside the old separated by commas?

like image 624
Lusaunt Avatar asked Mar 03 '10 03:03

Lusaunt


2 Answers

UPDATE tableName SET fruits=CONCAT(fruits, ', oranges') WHERE id=1;

or:

UPDATE tableName SET fruits=CONCAT_WS(', ', fruits, 'oranges') WHERE id=1;
like image 51
ceejayoz Avatar answered Nov 14 '22 22:11

ceejayoz


$reason_old = mysql_query("SELECT $col_name FROM `table` WHERE `unique_field` =$id");
$reason_fetch = mysql_fetch_array($reason_old);
$reason_box = $reason_all[$reason_fetch ];

$anyvariable= "UPDATE `$table_name` SET `$col_name ` = '$new_data , $reason_box' WHERE `unique_field` =$id";
$yes_good = mysql_query( $anyvariable, $conn );
like image 33
Mahabubur Rahman masum Avatar answered Nov 14 '22 23:11

Mahabubur Rahman masum