我table1 在没有主键的 MySQL Db 中有一个这样的表:
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| value | varchar(25) | YES | | NULL | |
| my_date | date | YES | MUL | NULL | |
| my_time | time | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
在按 my_date 和 my_time 值对表进行排序后,是否有可能从给定值开始更新 id 值?
例如从这组值
+------+------------+----------+
| id | my_date | my_time |
+------+------------+----------+
| 0 | 2018-03-01 | 09:02:00 |
| 0 | 2018-03-01 | 09:01:00 |
| 0 | 2018-03-01 | 09:00:00 |
+------+------------+----------+
我想获得
+------+------------+----------+
| id | my_date | my_time |
+------+------------+----------+
| 100 | 2018-03-01 | 09:00:00 |
| 101 | 2018-03-01 | 09:01:00 |
| 102 | 2018-03-01 | 09:02:00 |
+------+------------+----------+
我尝试过这样的事情但没有成功:
try {
int idFromToStart = 100;
String query = "SELECT IFNULL(id, 0) FROM table1 WHERE my_date > '2018-02-28' order by my_date, my_time";
preparedStatement =con.prepareStatement(query, ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = preparedStatement.executeQuery(query);
System.out.println("query " + query);
if (rs.next()) {
rs.updateLong(1, idFromToStart);
System.out.println("rs.getLong(1)) "+rs.getLong(1)+ " idFromToStart "+idFromToStart);
idFromToStart++;
}
} catch (SQLException ex) {
System.out.println("Error db " + ex.getMessage() + " " + ex.toString());
}
相关分类