猿问

按多列删除 Codeigniter 中的多行

我有一个关联数组,例如:


array(3) {

  [0]=>

  array(2) {

    ["userId"]=>

    string(1) "10"

    ["customerId"]=>

    string(3) "1809"

  }

  [1]=>

  array(2) {

    ["userId"]=>

    string(1) "13"

    ["customerId"]=>

    string(3) "1094"

  }

  [2]=>

  array(2) {

    ["userId"]=>

    string(1) "45"

    ["customerId"]=>

    string(2) "210"

  }

}

我正在尝试从数据库中删除这些行,但找不到要运行的正确 Codeigniter 查询。


生成的查询应该是这样的:


DELETE FROM table

WHERE (userId,customer_id) IN ( (10,1809),(10,1809),(45,210) )

如果我试试这个


$this->db->where_in( '(userId, customer_id)', array( array(10,1809), array(10,1809), array(45,210) ));

$this->db->delete('table');

die(var_dump($this->db->last_query()));

我明白了,这是不正确的,当然:


DELETE FROM `table`

WHERE (userId, customer_id) IN(Array, Array, Array)


holdtom
浏览 77回答 2
2回答

慕码人2483693

将关联的数组值获取到单个数组中,然后将其传递给查询$assoicative_array = array(array(10,20,30));    $ids=array();    foreach($assoicative_array as $key =>$value ){        $ids[]=$value;    }    $this->db->where_in('userId',$ids);    $this->db->where_in('customer_id',$ids);    $this->db->delete('table');    die(var_dump($this->db->last_query()));

撒科打诨

这是我尝试过的并且有效:foreach ( $data as $row ){    $userIds[] = $row['userId'];    $customerIds[] = $row['customerId'];}$this->db->where_in('userId', $userIds);$this->db->where_in('customer_id', $customerIds);$this->db->delete('table');这将删除行,生成的查询是:DELETE FROM `table`WHERE `userId` IN('10', '11', '54')AND `customer_id` IN('1809', '1904', '201')
随时随地看视频慕课网APP
我要回答