开发者

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 o开发者_如何学JAVAld 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?


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

or:

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


$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 );


What I would recommend that you do is maintain two tables. The first table is like so.

TableFruit -> FruitID (PRIMARY KEY) + general fruit attributes

And then another table that maintains versions like so.

TableFruitVersions -> VersionID (PRIMARY KEY), FruitID (FOREIGN KEY), IsActive (BOOLEAN) + specific fruit attributes

You can add a single record the first time in both tables. The generic attributes of the fruit, or the context within which it appears, will go into the first table; and the specific fruit ("apple") will go into the next.

The next time you will add a specific fruit ("orange"), make the previous record in the second table false, and add the new record immediately thereafter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜