【问题标题】:MySQL - suggestions to append data to an existing field/record?MySQL - 建议将数据附加到现有字段/记录?
【发布时间】:2011-08-20 23:54:28
【问题描述】:

我首先将数据添加到表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));
}

这会给我

exceptions
1

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

exceptions
2

等等。

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

exceptions
1,2,3  //etc

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

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

感谢您的帮助。

【问题讨论】:

    标签: php mysql concatenation concat


    【解决方案1】:
    $query = "UPDATE table1 SET field1 = CONCAT(ifnull(field1,''),'$extra') WHERE id = '$id'";
    

    【讨论】:

    • 谢谢 - 看起来它有效,但前提是该字段中已有数据 - 如果一开始是空的,则语句什么也不做 - 我们如何解决这个问题?
    • 实际上,我现在切换到CONCAT_WS(',',FIELD1,'$extra'),现在它可以工作了 - 谢谢!
    • 感谢这段代码。似乎正是我想要的。
    猜你喜欢
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    • 2011-11-03
    • 1970-01-01
    • 2011-04-15
    • 2018-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多