Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 240
pepe Avatar asked May 08 '11 03:05

pepe


1 Answers

$query = "UPDATE table1 SET field1 = CONCAT(ifnull(field1,''),'$extra') WHERE id = '$id'";
like image 93
Johan Avatar answered Sep 28 '22 07:09

Johan