thejoker
thejoker

Reputation: 159

Deleting multiple rows in SQL/codeigniter

This doesnt seem to work:

 /// delete emails first
         $this->db->where('DateSent', 'DateSent!=NULL');
        $this->db->delete('Emails');

I guess I need to use a loop. Not sure how to go about doing this. Can anyone help?

Cheers

Upvotes: 0

Views: 6074

Answers (3)

GautamD31
GautamD31

Reputation: 28763

First you need to store the selected values into an array say "$checked"

$checked = implode(',',$checked);     //then it looks array('id1','id2','id3',..);

then you can delete as

$this->db->where("iUserId in",$checked);

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

that's it.it works for me.i hope it useful for you.accept if its useful

Upvotes: 0

Josh Holloway
Josh Holloway

Reputation: 1683

If you were to look in the very useful CodeIgniter userguide you'll see the operator should be included as part of the first parameter not second. This should solve it:

$this->db->where('DateSent !=', 'NULL');
$this->db->delete('Emails');

Upvotes: 4

Sergey Telshevsky
Sergey Telshevsky

Reputation: 12197

Try this:

$this->db->query('
    DELETE FROM `Emails`
    WHERE id NOT IN (
        SELECT id
        FROM (
            SELECT id
            FROM `Emails`
            WHERE `Emails`.`DateSent` = NULL
        ) foo
    );
);

Query taken from SQL query: Delete all records from the table except latest N? then modified

Upvotes: 1

Related Questions