我在 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。在这两种情况下,我都能在 EXIT_TIME 中获得 current_timestamp。 但是当我在一分钟或一天后用相同的值再次更新 MOD_EID 字段时,EXIT_TIME 字段没有更新到 current_timestamp? 怎么了
更新代码是
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);
}
}
最佳答案
TIMESTAMP 列只有在其他列的值发生变化时才会自动更新。如果您将 MOD_EID 设置为与已有值相同的值,则不会更新 TIMESTAMP。我不知道为什么它过去对你有用,这就是 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).
关于java - mysql 时间戳更新在列中使用相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12594850/