0

使用 CI,我需要使用子查询进行插入忽略。尝试执行此操作时出现代码点火器错误:

$data = array(
            "user_id" => $user_id,
            "workload_id" => $wid
    );
    $this->db->set($data);
    $this->db->set("order", "(SELECT ifnull(max(`order`)+1, 0) FROM (SELECT * FROM default_workloads_users A ) B WHERE user_id=".$user_id.")", FALSE);
    //$this->db->insert("default_workloads_users");
    $ins = $this->db->insert_string("default_workloads_users");
    $ins = str_replace("INSERT", "INSERT IGNORE", $ins);
    $this->db->query($ins);

它告诉我我缺少 insert_string 的第二个参数。想法?

4

1 回答 1

0

想出了一个破解的方法。首先使用 insert_string,然后将子查询 str_replace 放入语句中。新代码:

$data = array(
            "user_id" => $user_id,
            "workload_id" => $wid,
            "order" => "@order"
    );
$ins = $this->db->insert_string("default_workloads_users", $data);
$ins = str_replace("INSERT", "INSERT IGNORE", $ins);
$ins = str_replace("@order","(SELECT ifnull(max(`order`)+1, 0) FROM (SELECT * FROM default_workloads_users A ) B WHERE user_id=".$user_id.")",$ins );
$this->db->query($ins);
于 2013-03-02T18:30:22.430 回答