是否可以在codeigniter中运行自定义更新查询。我需要在codeigniter中运行以下查询,
UPDATE `category` SET `online`= IF(online=1,0,1) WHERE id = 10
它在更新前检查在线值。如果在线= 1则为0,如果在线= 0则为1.我测试并且查询工作正常,但如何在codeigniter中运行此查询。
答案 0 :(得分:1)
尝试使用
$updateData = array(
'online' => IF(online=1,0,1)
);
$this->db->where('id', 10);
$this->db->update('category', $updateData);
答案 1 :(得分:0)
您可以像提到的 Nishant Nair 那样,或者您可以使用纯SQL,如:
$this->db->query('YOUR SQL QUERY HERE');
另请查看文档: https://www.codeigniter.com/userguide3/database/query_builder.html#updating-data
答案 2 :(得分:0)
$updateData = array(
'col1' => 999
);
$this->db->set("online","IF(online=1,0,1)",false); //set false third parameter to prevent from auto escaping
$this->db->where('id', 10);
$this->db->update('category', $updateData);
您可以在codeigniter数据库查询帮助器中使用set
命令。如果需要更多updateData array
字段,仍可以在update
命令第二个参数中使用set