I currently have my database updating the timestamp when new records are inserted. It formats like this:
2017-08-24 15:48:30.189182
My question is: how do I delete records that are over, suppose, 3 days old?
I'm assuming I can run a cron job nightly to remove the records older than 3 days. Can someone give me the format for it? I'm not sure how to work with the timestamp data.
Thank you for your help!
different database has different way to compute time difference between two days...
MySQL: DATEDIFF(date1,date2);
Oracle: select date1 - date2 from dual;
and so on...
Assuming the column is of type DATE, you can run a query that deletes all columns where the difference between today and your column is greater than 3.
Replace table with your table, and date_column with your column-name. Again, this assumes that you've got columns that is of the DATE-type, and not a VARCHAR or something.
DELETE FROM table WHERE DATEDIFF(CURDATE(), date_column) > 3
SQLFiddle
MySQL Date functions
Related
I have a database table that has two columns, one called exstdate which is the start date and exrdate which is the end date and is a lot of records in the table and I want to be find the average repair time in days of all the records dates and output it.
I came up with a SQL query but not 100% sure, it outputs the result as 2.4 so might be correct but 100% sure if it's the correct query
SELECT ROUND(AVG(DATEDIFF( exrdate, exstdate )), 1) AS avg_repair_time FROM repairs
Can anyone tell me if that is the correct query please to output the average repair time in days from all the records start and end date columns, the type for the dates columns in the database table is using date
I want to do a query with thousands of tuples. I need to save the first ID, last ID and date saved in a historic table by day in a new table. I have data from 2020 to 2022. Every day could 600.000 rows or more. I have thought two solutions:
Doing a query every time with limit 600.000 and save the first id, last id and date, all of this order by dates or ids.
Doing a query day by day and get the first and the last id.
The problems are that these querys could delay so much because i am doing orderings.
I´m doing this with SQL and need execute this in PHP with a cron every day to save the data of the day. First, i´m building the new table with the data of past.
Someone would know one tip or antoher form to do this.
THANKS!
You can do this (result here)
select date, min(id) as min, max(id) as max
from logs
group by date
I want to automatically delete a record from my database sql after 2 days of inserting the record. I am currently developing a simple reservation. How can I achieve this??
Add an event to your table that runs once every day and deletes those entries. You need to have a date column though to recognize these records.
Assuming you're using some sort of timestamp on insert then you can run a scheduled job that checks the age of the record with datediff and then deletes the record if datediff = 2 days
I'm using Laravel 3 - PHP and MySQl.
I need to select all records created between two dates (created_at DATETIME) and between two times (3pm and 6pm).
$query->where_between('created_at', $this->s['start'], $this->s['end']);
How can I add a time range to that for between 3pm and 6pm between those days?
Edit
$this->s['start'] = '2014-2-14';
$this->s['end'] = '2014-2-16';
Timerange: 06:00:00 and 09:00:00 UTC
Dates are stored in Datetime like 2014-2-15 06:43:56
Example data
Records:
Between 2014-2-14 and 2014-2-17 AND between 07:00:00 and 14:00:00.
2014-2-15 06:43:56
2014-2-16 08:43:56*
2014-2-17 15:43:56
2014-2-17 10:43:56*
2014-2-18 12:43:56
2014-2-19 14:43:56
2014-2-20 16:43:56
* selected record
Along the lines of:
$query->raw_where('EXTRACT(HOUR_SECOND from `created_at`) between 070000 and 140000');
Simpler way to do this?
Why are you not doing this using sql? Filtering is part and parcel of SQLs raison d'etre. Indeed, the between clause exists for precisely this reason:
SELECT pkid from tbl where datetime_field between 7:00:00 and 14:00:00
I'd recommend keeping the filtering as close to the database as is reasonably possible, given the constraints of the database itself.
I think that you just have to add an other where_between constraint to your request :
$query->where_between('created_at', $this->s['start'], $this->s['end'])
->where_between('created_at', '6:00:00', '9:00:00');
Never tried but it should do the job !
Thanks to hd1 for SQL tip.
I have two field in a database table departureDateTime and arrivalDateTime and the values like
departureDateTime=03/18/2012 1:05 PM
arrivalDateTime=03/18/2012 3:15 PM
I have hundreds of records in the table.
I need to sort and display according to the duration from these two time. I know to calculate the duration from two dates
Duration=(strtotime($arrivalDateTime') - strtotime($departureDateTime))/3600
But how I write a mysql query to sort and display these 100 records from database
Does any one any idea?
Thanks
Change your departureDateTime to DATETIME instead of varchar or whatever you are currently using. Then you would use something like this:
SELECT other, stuff, TIMEDIFF(departureDateTime, arrivalDateTime) as theDifference FROM myTable ORDER BY theDifference ASC LIMIT 0, 100
If you kept your date and times in a date time column type then you wouldn't have this problem. Consider changing.
There are mysql functions to calculate dates and time too
http://dev.mysql.com/doc/refman/5.6/en/datetime.html
If not, you can do sorting in PHP using usort.