I have an SQL query:
DELETE n1
FROM satellites n1
, satellites n2
WHERE n1.id < n2.id
AND n1.norad_cat_id = n2.norad_cat_id
What this query does is delete rows that have the same norad_cat_id and only leave one with the highest id. I don't know if my SQL query is correct, but I will have to see.
I am a bit stuck when it comes to running raw SQL queries in Laravel. From this documentation (https://laravel.com/docs/5.4/database#running-queries) you can see that you have a few options to run the query:
DB::update('SQL QUERY HERE');
DB::delete('SQL QUERY HERE');
DB::statement('SQL QUERY HERE');
DB::select( DB::raw('SQL QUERY HERE'));
In my case I am trying to delete duplicate rows while only leaving the one with the highest id. What Laravel DB statement do I run to achieve the results I want or does it matter at all?
EDIT: SQL query for @MasudMiah
delete satellites
from satellites
inner join (
select max(id) as lastId, norad_cat_id
from satellites
group by norad_cat_id
having count(*) > 1) duplic on duplic.norad_cat_id = satellites.norad_cat_id
where satellites.norad_cat_id < duplic.lastId;
norad_cat_id. I need one left that has highestid