代码点火器 删除给定数组中的位置条件和位置



如何在Codeigniter中运行删除查询。我有一组数组和一个键。在MySQL中,我的查询运行完美。

DELETE FROM `TABLE` WHERE `style_id`=2 && `ID` NOT IN (5,9,12)

在 Codeigniter 中如何编写和运行此查询。我尝试了下面的代码,但不起作用。

$ids = '5,9,12';
$this->db->where('style_id', $style_id)->where_not_in('ID', $ids);
$this->db->delete('TABLE');

试试这个:你必须有$ids数组

$ids = array(5,9,12);
$style_id= 2;

$this->db->where('style_id', $style_id);
$this->db->where_not_in('ID', $ids);
$this->db->delete('TABLE');

如果你想喜欢这样,你可以用一行写这个:

$this->db->delete('table name',array('field1'=>'value1','field2 !='=>'value2'));

简短而简单。

试试这个

this->db->where('style_id', $style_id);
this->db->where_not_in('ID', $ids);
$this->db->delete('TABLE');

这将用AND子句连接两个参数


数据应该是

数组

$ids = array(10, 20, '30);

编号

$style_id = 15;

阅读在 codeigniter.com 中查找特定数据

你可以直接运行这个:

DELETE FROM `TABLE` WHERE `style_id`=2 && `ID` NOT IN (5,9,12)
in $this->db->query("DELETE FROM `TABLE` WHERE `style_id`=2 && `ID` NOT IN (5,9,12)");

//试试吧

$ids = array(5,9,12);
$style_id =2;
$this->db->where('ID', $style_id);
$this->db->where_not_in('ID', $ids);
$this->db->delete('TABLE');

最新更新