pepe
pepe

Reputation: 9919

MySQL - suggestions to append data to an existing field/record?

I begin by adding data to table events, field exceptions like this

function events_exceptions($rec_id, $evt_id)
{
//$evt_id = 1;

$this->db->where('record_id', $rec_id);
$this->db->update('events', array('exceptions' => $evt_id));
}

This will give me

exceptions
1

If I run this again with $evt_id = 2 I'll get

exceptions
2

and so on.

I want to actually append the new data to that field instead of update, so I would have (note the comma)

exceptions
1,2,3  //etc

I think this can be done by making a call to DB, reading the content of the row/field, and concatenating via PHP - then updating the record. But I wanted to avoid such overhead.

Is there a way of doing this via MySQL CONCAT?

Thanks for your help.

Upvotes: 6

Views: 8630

Answers (1)

Johan
Johan

Reputation: 76670

$query = "UPDATE table1 SET field1 = CONCAT(ifnull(field1,''),'$extra') WHERE id = '$id'";

Upvotes: 15

Related Questions