1

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.

4

5 回答 5

1

也许这会成功

SELECT * FROM xxx WHERE (DATE_FORMAT(CURDATE() ,'%d')==1)
于 2013-08-19T09:55:11.723 回答
0
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
于 2013-08-19T09:46:10.197 回答
0

试试下面的事件调度程序

CREATE EVENT myevent
    ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 MONTH
    DO
      "YOUR QUERY"

您必须在您希望每隔 1 个月运行的日期运行它。

您可以在http://dev.mysql.com/doc/refman/5.1/ja/create-event.html获得更多信息

于 2013-08-19T09:46:17.137 回答
0

你可以用mysql事件调度器来实现——

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);

或存储过程。

CREATE EVENT IF NOT EXISTS `my_old_data_cleaner_event`
ON SCHEDULE EVERY 23 DAY_HOUR
DO CALL my_sp_cleanup_old_data();
于 2013-08-19T09:48:54.130 回答
0

您有两种解决方案,具体取决于您到底想要什么。

如果您的数据必须在第一天插入:唯一的方法是使用在正确时间运行的 cron 作业插入它。如果仅与 mysql 相关,您可以使用 mysql 调度程序来完成这项工作。

如果您的数据需要在第一天之后出现在表中,但您不关心它是实时添加还是仅在您访问该数据时添加,那么您不需要 cron 作业。您可以在访问该数据时检查它,然后在读取之前插入它(如果它不存在)。

于 2013-08-19T09:49:51.547 回答