Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete record while inserting value to database

Tags:

php

mysql

I have the db having the location log of the vehicle which means every coordinates will be stored in the db. Now for vehicle 1, assume there are 30 entries in the table, meaning 30 locations. Now when the 31st entry comes, i want the FIRST entry to get removed which means, the maximum numbers of location log per vehicle should be 30.

Right now the insert query is

mysqli_query($con,
    "INSERT INTO $location_history
    VALUES('$id','$lat','$lng','$pwd','$v1','$v2','$v3','$v4','$status' )")
or die("Count not insert to location history!");
like image 785
Venkateshwaran Selvaraj Avatar asked Dec 16 '13 13:12

Venkateshwaran Selvaraj


1 Answers

You should use a trigger function that is going to work on every insert, count the entries, and if there are more than 30, delete the oldest ones until there are less than 30 entries.

like image 147
mehmetseckin Avatar answered Oct 19 '22 20:10

mehmetseckin