Delete all but top 50 rows from DB with CodeIgniter

Lauren F

I've been getting a multitude of errors with the request I'm trying to make. I'd like to grab the top 50 ids from my keywords table ordered by count and delete the rest. Any ideas what could be acting up here?

I'm getting a 500 Internal Server Error when making the request...

        //1. get ids of top 50
        $this->CI->db->query("SELECT id FROM keywords ORDER BY count DESC LIMIT 50");
        $top_fifty = $this->CI->db->get();
        //return $top_fifty;

        //2. loop through and create string of ids "1,2,3,4,5"
        $top_fifty_string = implode(",", $top_fifty);

        // 3. DELETE FROM keywords WHERE id NOT IN(string created in step 2)
        $this->CI->db->query("DELETE FROM keywords WHERE id NOT IN($top_fifty_string)");
AdrienXL

Fixed function :

// //1. get ids of top 50
$top_fifty = $this->CI->db->query("SELECT id FROM keywords ORDER BY count DESC LIMIT 50 ");

// //2. array of ids to string "1,2,3,4,5"
$output ="";

foreach ($top_fifty->result() as $k) {
$output .= $k->id . ",";
}
$top_fifty_string = rtrim($output, ",");

// // 3. DELETE FROM keywords WHERE id NOT IN(string created in step 2)
$this->CI->db->query("DELETE FROM keywords WHERE id NOT IN(.$top_fifty_string.)");

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Codeigniter delete row from db

From Dev

Delete rows from all tables

From Dev

Delete rows from different tables in CodeIgniter

From Dev

Delete few rows from db table

From Dev

Codeigniter not returning all the rows from the database

From Dev

Update random top 50 rows

From Dev

Update random top 50 rows

From Dev

delete all rows from sql in android studio

From Dev

Codeigniter cannot delete rows from database table using join method

From Dev

Delete many rows from a large percona mysql db

From Dev

Delete all data rows from an Excel table (apart from the first)

From Dev

Delete all "No Fill" rows

From Dev

Delete all rows in a tableview

From Dev

Delete all migrations and start migrations from current DB

From Dev

How to select all non-distinct rows from a DB?

From Dev

How to select all non-distinct rows from a DB?

From Java

keep x rows and delete all from csv file

From Dev

What is the best way in Android to delete all rows from a table

From Dev

Delete all rows from account, except most recent eleven

From Dev

How to delete all rows from table which has no FK relation

From Dev

Invisible Delete button of my listview from all rows in android

From Dev

What is the best way in Android to delete all rows from a table

From Dev

Delete all rows from MySQL table using PHP

From Dev

How to delete all rows from excel except header in c#

From Dev

How to return all rows in CodeIgniter?

From Dev

How to return all rows in CodeIgniter?

From Dev

Select and display all rows in Codeigniter

From Dev

calling controller function from view and retrieving rows from db using AJAX in codeigniter

From Dev

How to Retrieve either Top N rows OR all rows from a table in SQL Server

Related Related

HotTag

Archive