Reputation: 810
How to run delete query in Codeigniter. I have set of array and a key. In MySQL my query run perfectly.
DELETE FROM `TABLE` WHERE `style_id`=2 && `ID` NOT IN (5,9,12)
In Codeigniter how to write and run this query. I tried below code but not working.
$ids = '5,9,12';
$this->db->where('style_id', $style_id)->where_not_in('ID', $ids);
$this->db->delete('TABLE');
Upvotes: 1
Views: 13080
Reputation: 16436
Try this: you must have $ids as array
$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');
Upvotes: 7
Reputation: 323
You can write this in single line if you want to like this:
$this->db->delete('table name',array('field1'=>'value1','field2 !='=>'value2'));
short and simple.
Upvotes: 1
Reputation: 591
// Try it
$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');
Upvotes: 0
Reputation: 1202
You can run this directly :
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)");
Upvotes: 0
Reputation: 38609
Try this
this->db->where('style_id', $style_id);
this->db->where_not_in('ID', $ids);
$this->db->delete('TABLE');
This will join two arguments with AND
clause
And Data should be
array
$ids = array(10, 20, '30);
Id
$style_id = 15;
Read Looking for Specific Data in codeigniter.com
Upvotes: 0