首页 文章

在codeigniter中查询“select for update”

提问于
浏览
3

我有查询= SELECT @condi:=id FROM table WHERE id>1 LIMIT 1 for UPDATE; UPDATE table set aa="ok" WHERE id=@condi

我用 $this->db->query(query) 但是codeigniter信息错误"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'update table set aa="确定" where id=@condi' at line 1"小姐";"我用phpmyadmin执行这个查询,它的结果行正确没有错误查询

也许核心codeigniter错误你能帮我在codeigniter中执行我的查询,谢谢

1 回答

  • 2

    您可以尝试使用查询构建器来防止SQL错误:

    $query = $this->db->select()
                ->from('table')
                ->where('id >', 1)
                ->limit(1)
                ->get_compiled_select();
    $data = $this->db->query("{$query} FOR UPDATE")->row_array();
    
    $this->db->where('id', $condi)->update('table', ['aa'=>'ok']);
    

    因为Codeigniter 3构建器不支持 FOR UPDATE ,所以我只使用编译器来重建查询 .

    Codeigniter get_compiled_select()yidas / codeigniter-model - 悲观锁定

相关问题