0

Date我对在 MySQL 数据库中添加天数有疑问。以下是我的代码:

res=stat.executeQuery("select st_date from tmp1 where st_date = '"+t1.getText()+"'");
while(res.next())
{
    System.out.println(res.getDate(1));
    int i=0;
    while(i<14)
    {
        statement.executeUpdate("Insert into datetab values(DATE_ADD('"
            +res.getDate("st_date")+"',INTERVAL 1 DAY),'"+tempname+"')");
        i=i+1;
    }
}

表中的所有更新都datetab发生了,但是有一个问题。我将用一个例子来解释这个问题。如果 tmp1 表中的日期是 28-12-2000,那么在使用 执行插入查询后date_add(),会发生 13 个新插入,但所有这些插入都是“29-12-2000”。

4

1 回答 1

0

如果 tmp1 表中的日期是 28-12-2000 那么在使用 date_add() 执行插入查询后,会发生 13 个新插入,但所有这些插入都是“29-12-2000”。

因为这正是你所要求的。您的插入语句是:

"Insert into datetab values(DATE_ADD('" + res.getDate("st_date") + 
    "',INTERVAL 1 DAY),'" + tempname + "')"

由于read.getDate在循环中没有变化,因此每次交互都会插入相同的值。

而不是"Interval 1 DAY", use"Interval " + i + " Day"应该插入不同的日子。那是你要找的吗?

于 2013-01-08T00:11:54.710 回答