有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用列中的相同值更新java mysql时间戳

我在MySQL表中有一个字段,我将它设置为默认值作为时间戳,当我第一次更新它时,它会更新到当前时间戳,当我第二次更新它时,它也会更新到当前时间戳bt,当我第三次更新时,它会更新到列时间戳字段中的相同值,而不是更新到当前时间戳

我的表格结构是:

+-----------+-----------+------+-----+-------------------+-----------------------------+
| Field     | Type      | Null | Key | Default           | Extra                       |
+-----------+-----------+------+-----+-------------------+-----------------------------+
| EID       | int(11)   | NO   | PRI | 0                 |                             |
| MOD_EID   | int(11)   | YES  |     | NULL              |                             |
| EXIT_TIME | timestamp | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-----------+-----------+------+-----+-------------------+-----------------------------+

当我第一次更新时,我在EID中插入所需值,在MOD_EID中插入0。当我第二次更新时,我正在用所需的值更新MOD_EID。在这两种情况下,我都能够在退出时间中获得当前的时间戳。 但是,当我在一分钟或一天之后用相同的值再次更新MOD_EID字段时,是否退出_TIME字段而不更新到当前的_时间戳? 怎么了

更新的代码是

String vexit=request.getParameter("string");

StringTokenizer tok=new StringTokenizer(vexit,"");

String sql,query,query2;
int count2=0;


    while(tok.hasMoreTokens())
    {
            String ide=tok.nextToken();
sql="SELECT count(EID) FROM tm_exit where EID='"+ide+"' ";
            ResultSet rs=st.executeQuery(sql);
            while(rs.next())
            {
            count2=rs.getInt(1);
}
        if(count2 >0)
            {
query="UPDATE tm_exit set MOD_EID='"+ide+"' WHERE EID='"+ide+"'";
int flag=st.executeUpdate(query);
}
        else
            {
query2="INSERT INTO tm_exit (EID,MOD_EID)values('"+ide+"',0)";
int flag2=st.executeUpdate(query2);
}
    }

共 (1) 个答案

  1. # 1 楼答案

    只有当其他列的值发生变化时,时间戳列才会自动更新。如果将MOD_EID设置为与之前相同的值,则不会更新时间戳。我不知道为什么它曾经对你有用,下面是documentation所说的:

    If the column is auto-updated, it is automatically updated to the current timestamp when the value of any other column in the row is changed from its current value. The column remains unchanged if all other columns are set to their current values. To prevent the column from updating when other columns change, explicitly set it to its current value. To update the column even when other columns do not change, explicitly set it to the value it should have (for example, set it to CURRENT_TIMESTAMP).