mardi 19 juillet 2016

delete the duplicate records, and keep the one that have lower id

I have

a users table that contains bad data. I want to loop through and delete the duplicate records in term of email, and keep the record that have lower id.

enter image description here

I want to keep only id : 1, 14, 1004, 1005, 1003, 1006, and 1007.


I've tried

$users = DB::table('users')->where('id', DB::raw("(select min(`id`) from users)"))->get();

foreach ($users as $user) {
    if ( ???? )) { // <---- I'm not sure what to put here. 
        $user->delete();
    }
}

I also tried $users = DB::table('users')->distinct()->get();


A little stuck right now, any hints will be much appreciated



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire