MySQL - 建议将数据附加到现有字段/记录?

pep*_*epe 6 php mysql concat concatenation

我首先将数据添加到表events,现场exceptions像这样

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));
}
Run Code Online (Sandbox Code Playgroud)

这会给我

exceptions
1
Run Code Online (Sandbox Code Playgroud)

如果我再次运行,$evt_id = 2我会得到

exceptions
2
Run Code Online (Sandbox Code Playgroud)

等等.

我想实际将新数据附加到该字段而不是更新,所以我会(注意逗号)

exceptions
1,2,3  //etc
Run Code Online (Sandbox Code Playgroud)

我认为这可以通过调用DB,读取行/字段的内容,并通过PHP连接 - 然后更新记录来完成.但我想避免这种开销.

有没有办法通过MySQL做到这一点CONCAT

谢谢你的帮助.

Joh*_*ica 15

$query = "UPDATE table1 SET field1 = CONCAT(ifnull(field1,''),'$extra') WHERE id = '$id'";
Run Code Online (Sandbox Code Playgroud)

  • 实际上,我现在切换到`CONCAT_WS(',',FIELD1,'$ extra')`现在它可以工作了 - 谢谢! (2认同)