我有一个关联数组,比如:
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)
将关联的数组值获取到单个数组中,然后传递到查询中
$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')