How do I delete blank rows in Mysql?

前端 未结 5 1117
忘了有多久
忘了有多久 2021-02-07 18:30

I do have a table with more than 100000 data elements, but there are almost 350 blank rows within. How do I delete this blank rows using phpmyadmin? Manually deleting is a tedio

5条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-07 18:44

    I know this has already been answered and has got a tick, but I wrote a small function for doing this, and thought it might be useful to other people.

    I call my function with an array so that I can use the same function for different tables.

    $tableArray=array("Address", "Email", "Phone"); //This is the column names
    $this->deleteBlankLines("tableName",$tableArray);
    

    and here is the function which takes the array and builds the delete string

    private function deleteBlankLines($tablename,$columnArray){
        $Where="";
        foreach($columnArray as $line):
            $Where.="(`".$line."`=''||`".$line."` IS NULL) && ";
        endforeach;
        $Where = rtrim($Where, '&& ');  
        $query="DELETE FROM `{$tablename}` WHERE ".$Where;
        $stmt = $this->db->prepare($query);
        $stmt->execute();
    }
    

    You can use this function for multiple tables. You just need to send in a different table name and array and it will work.

    My function will check for a whole row of empty columns or NULL columns at the same time. If you don't need it to check for NULL then you can remove that part.

提交回复
热议问题