database 如何删除多行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10492121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete multiple rows?
提问by Joe
So I can delete one row using:
所以我可以使用以下方法删除一行:
$this->db->delete($tableName,array("id"=>4));
...but I can't figure out how to delete multiple rows. I tried:
...但我不知道如何删除多行。我试过:
$this->db->delete($tableName,array("id"=>4,"id"=5));
as well as:
也:
$this->db->delete($tableName,array(array("id"=>4),array("id"=5)));
...but they both don't work. I feel like this should be pretty easy. Any answers?
...但它们都不起作用。我觉得这应该很容易。任何答案?
回答by Moyed Ansari
Have you tried this ?
你试过这个吗?
$names = array(4,5);
$this->db->where_in('id', $names);
$this->db->delete('mytable');
回答by Abuzer Firdousi
not need of associative array.
不需要关联数组。
$ids[] = 1;
$ids[] = 2;
$this->db->where_in( id, $ids );
$this->db->delete('Table_Name');
回答by Mohit Bumb
write custom query for it
为它编写自定义查询
$this->db->query("DELETE FROM `TABLE_NAME` WHERE `id` IN(1,2,3,4,5)");
回答by Raja Ram T
it is not work
这不行
$names = array(4,5);
$this->db->where_in('id', $names);
$this->db->delete('mytable');
DELETE FROM table_name WHERE id IN ('4,5')
notice one thing not single string/digit ('4,5') we should be dived each and every id with single quotation like this ('4','5')
注意一件事不是单个字符串/数字('4,5')我们应该用这样的单引号('4','5')对每个id进行潜水
The best and good way
最好的方法
//$ids = 1,2,3.....
$ids_exp = explode(',',$ids);
$this->db->where_in('id',$ids_exp);//
$this->db->delete('table_name')
return $this->db->affected_rows();
the above query will be work enjoy..........
上述查询将是工作享受......
回答by Raja Ram T
To delete a single use row:
要删除一次性使用的行:
$this->db->delete('TABLE_NAME', array('id' => 5));
Have you tried doing this? I'm pretty sure it should work.
你试过这样做吗?我很确定它应该有效。
$this->db->delete('TABLE_NAME', array(array('id' => 5), array('id' => 3), ...));

