【问题标题】:How to run this MySQL query in codeigniter using active record pattern如何使用活动记录模式在 codeigniter 中运行这个 MySQL 查询
【发布时间】:2015-04-06 23:27:48
【问题描述】:

我正在使用 ci 并使用它的活动记录模式来访问数据库。我想使用类似的语句更新表

UPDATE employee_barcode set `count` = `count` + 1
where barcode_id = 2

我尝试使用这样的更新语句

$data = array(
            'count' => 'count' + 1,
        );

$this->db->where('barcode_id', 2);
$this->db->update('employee_barcode', $data);

但结果是错误的。

我该怎么做?

【问题讨论】:

    标签: php mysql codeigniter activerecord


    【解决方案1】:

    这不起作用,因为$this->db->update 无法获得count 的值,因此无法对其加1。您应该使用$this->db->select 获取count 值,然后继续更新该值。

    例如:

    $this->db->where('barcode_id', 2);
    $this->db->select('count');
    $query = $this->db->get('employee_barcode');
    $row = $query->row();
    $this->db->where('barcode_id', 2);
    $this->db->update('employee_barcode', array('count' => ($row->count + 1)));
    

    【讨论】:

      【解决方案2】:

      试试这个..

      $this->db->set('count', '`count+1`', FALSE)
      $this->db->where('barcode_id', 2);
      $this->db->update('employee_barcode');
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-10
        相关资源
        最近更新 更多