C
C
chelkaz2016-06-05 00:36:18
Laravel
chelkaz, 2016-06-05 00:36:18

How to come up with an auto-cleanup table?

Guys, I have a table with fields

id
name
vid
comm
created_at
updated_at

Data is constantly entered into this table, and then the latest by the date of entry of a maximum of 500 records with the same values ​​in the vid
field is displayed. It is logical that records outside the sample of 500 will no longer be useful.
How to make it so that if there are more than 500 records with the same vid , then delete the older ones by date?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
chelkaz, 2016-06-05
@chelkaz

After reading the docs ... As a result, the ideal option for the Laravel framework without a bicycle will fail:

$count = DB::table('tbl_name')->where('vid', '=', $vid)->count();
DB::table('tbl_name')->where('vid', '=', $vid)
     ->orderBy('created_at', 'asc')
     ->skip(500)->take($count-500)
     ->delete();

A
Andrzej Wielski, 2016-06-05
@wielski

There are many solutions, here is one of them as an example:
1) Using count and group by we get the list vid - quantity, where the number is more than 500
2) Iterate through the vid array, execute a delete query with a limit, and sort by date.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question