I want to insert data into a table on 1st day of every month. And it has to be just for one time. I mean one row for each 1st day of month. I am doing all this in a PHP file and using mySql.
So far i got this much-
if(Date('j')==1)
{
$query = select 1 from table where extract (year from t1) = extract(year from now()) and extract(month from t1) = extract(month from now()) LIMIT 1
if (#mysql_num_rows(mysql_query($query)) == 0)
{
//perform insert operation
}
}
I haven't tried this query till yet.
Maybe this will do the trick
SELECT * FROM xxx WHERE (DATE_FORMAT(CURDATE() ,'%d')==1)
SELECT DATEADD(mm, DATEDIFF(mm,0,GETDATE())-1,0) as StartOfLastMonth
SELECT DATEADD(mm, DATEDIFF(mm,0,GETDATE()),0) as StartOfThisMonth
SELECT DATEADD(mm, DATEDIFF(mm,0,GETDATE()),0) as StartOfNextMonth
Try out below event scheduler
CREATE EVENT myevent
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 MONTH
DO
"YOUR QUERY"
Your have to run this on the date on which you want to run on 1 month interval.
You get more information on http://dev.mysql.com/doc/refman/5.1/ja/create-event.html
You have two solutions, depending on what you exactly want.
If your data must be inserted at the first day: the only way is to insert it using a cron job which run at the correct time. You can use the mysql scheduler to do the job if it's only mysql related.
If your data need to be in the table after the first day, but you don't care if it is added in realtime or only when you access that data then you don't need a cron job. You can check for that data when you access it, then insert it before reading if it's not already present.
you can achieve with mysql event scheduler--
http://dev.mysql.com/doc/refman/5.1/en/events.html
CREATE EVENT IF NOT EXISTS `my_old_data_cleaner_event`
ON SCHEDULE EVERY 23 DAY_HOUR
COMMENT 'Clean up Service Start at 11:00PM daily!'
DO DELETE FROM my_table WHERE created_date < (NOW() - INTERVAL 1 MONTH);
OR for Stored Procedure.
CREATE EVENT IF NOT EXISTS `my_old_data_cleaner_event`
ON SCHEDULE EVERY 23 DAY_HOUR
DO CALL my_sp_cleanup_old_data();
Related
I want to delete data from database after every 24hours insertion.
For example: A user is added today and he is not active, after 24hours the user will be automatically deleted.
I intend doing this without cron because cron is not in my local/offline server.
Please i really need your help.
You could simply use
DELETE FROM your_table
WHERE your_date_col < DATE_SUB(NOW(), INTERVAL 1 DAY))
and use the mysql scheduled events
could be somethings like this
CREATE EVENT my_dayly_event
ON SCHEDULE
EVERY 1 DAY
COMMENT 'delete old values.'
DO
DELETE FROM your_table
WHERE your_date_col < DATE_SUB(NOW(), INTERVAL 1 DAY))
anyway take a look a this doc. https://dev.mysql.com/doc/refman/5.7/en/create-event.html ... could be useful
Im trying to avoid multiple sql inserts in a database. The idea is to wait at least 5 minutes before inserting again.
Getting time from last insert using
$query ="SELECT fecha
FROM almacen
WHERE fecha > NOW() - INTERVAL 5 MINUTE
ORDER BY id DESC LIMIT 1"
$espera=mysqli_query($conexion, $query)
if (empty($espera)) { inserting code } else { close }
But the query returns nothing when it should be returning a value. I was thinking it might be a problem since date was inserted using php date ( "j/n/Y h:i");
Should i change the time format? what should i use?
If you wanted to restrict your rows to only 1 entry per 5 minutes I would:
Create a column called insert_interval DATE and create a unique index on that column. Then just attempt your insert, setting the value of insert_interval to
whatever 5 minute interval the insert time falls under.
E.G. Normalize date to 5 minute interval.
select now() - interval (mod(minute(now()),5)) minute - interval second(now()) second;
Insert
insert into table(columns ..., insert_interval) values(
..., now() - interval mod(minute(now()),5) minute - interval second(now())
);
Only 1 row is allowed per 5 minute interval starting on the hour. So 1 row from 00:00:00 - 00:04:59, and so on. Any attempt to insert another row in that time window, would result in a duplicate key error, that you can catch and take appropriate action.
SQL Fiddle
Uncomment the last insert to see the error on build schema.
I have a table where I have column name 'date' and another column with the name 'status' if I use a cron job to run everyday and check if the date is older than 30 day then to change the status to expired, what code do I need to run using mysqli and php.
MySQL event scheduler is more better.
Start MySQL event scheduler by executing following query in PhpMyAdmin or MySQL command prompt.
Enter this.
SET GLOBAL event_scheduler = 1;
This will update your table.
CREATE EVENT newEvent
ON SCHEDULE EVERY 1 DAY
DO
UPDATE table SET status="expired" WHERE datefield<=CURRENT_DATE - INTERVAL 30 DAY;
Try this:
UPDATE table SET status="expired" where YOURDATEFIELD < date_sub(now(), interval 1 month);
Use INTERVAL in your UPDATE query..
UPDATE table SET status="expired" WHERE datefield<=CURRENT_DATE - INTERVAL 30 DAY
I have a calendar table in my MYSQL database with one field named datefield (type DATE).
Previously I inserted dates from 2008-01-01 to 2010-01-01 but now I want to add every day until the current date. Unfortunately, I'm completely forgotten how I did this.
I'm also wondering if there is any way to automate the process of keeping this table current.
I'm using PHP so I guess I could do a new INSERT every day? Or is there some way to do this directly in MYSQL?
thanks,
tim
Here's a quick hack:
drop table if exists dates;
create table dates(datefield date);
drop procedure if exists insertDates;
delimiter $$
create procedure insertDates(start_date date)
begin
set #days = 0;
set #end_date = str_to_date('1970-01-01', '%Y-%m-%d');
while (#end_date < curdate()) do
insert into dates
select
date_add(start_date, interval #days day)
from (select #days:=#days+1) r
;
set #end_date:=(select max(datefield) from dates);
end while
;
end $$
delimiter ;
call insertDates('2012-06-06');
select * from dates;
For the daily insert you can try a cron job or MySQL offers "EVENTS", something like:
CREATE EVENT yourDB_Schema.insertDateEachDay
ON SCHEDULE EVERY 1 DAY
DO
INSERT INTO yourTable (yourDateColumn) VALUES (CURDATE());
Read more about it here:
MySQL Create Event manual entry
An event is associated with a schema. If no schema is indicated as
part of event_name, the default (current) schema is assumed.
_
Not specifying STARTS is the same as using STARTS
CURRENT_TIMESTAMP—that is, the action specified for the event begins
repeating immediately upon creation of the event.
I think the easiest way to do what you describe is set up a daily recurring task to run a php script, such as a cron job. There is also a web based provider that will call a script from your webserver on a cheap, recurring schedule. http://webcron.org
I want to know how to delete records from a MySQL Database after 1 week
This is the code I have for inserting the record and date of when it was added
INSERT INTO moviesdone VALUES ('" . $id2 . "', NOW())
Where $id is the name of what I am insterting and wanting to delete after 1 week.
I would also like to do this automatically, which I can set up from in phpMyAdmin
So, I only need to know: How do I write the date correctly (if needed) and how do I delete the record one week later?
Run this code at regular intervals, for example once per day:
DELETE FROM moviesdone
WHERE datetimecol < NOW() - INTERVAL 7 DAY
You can use the event scheduler.
If you are using MySQL 5.1.6 or greater you can use CREATE EVENT
Something like
CREATE EVENT del_entries
ON SCHEDULE EVERY 1 DAY
DO
DELETE FROM table WHERE DATEDIFF(NOW(), creation_date) >= 7;