'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.
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 noticed
$users = DB::table('users')->groupBy('email')->get();
return all the records I want to keep.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

