我試圖通過創建列自動增量來跟蹤點/統計。但是,它不能按我的需要工作。如果行得到更新,我希望它自動增加,而不是如果添加了新行。例如,如果我運行update命令,它將只爲我更新的行添加一個「count」列。 如果我添加一個新行,它將從0開始!MySQL/SQL遞增列
這裏是我的代碼創建一個表:
statement = connection.prepareStatement(
"CREATE TABLE IF NOT EXISTS stats" +
"(" +
"id varchar(100) not null," +
"count int not null auto_increment," +
"PRIMARY KEY (id)," +
"KEY (count)" +
")"
);
statement.execute();
這是我如何更新到一個特定的行:
connection = plugin.getHikari().getConnection();
statement = connection.prepareStatement("INSERT INTO stats (id) VALUES(?) ON DUPLICATE KEY UPDATE id=?");
statement.setString(1, id.toString());
statement.setString(2, id.toString());
statement.execute();
感謝, - Nicster
而在更新它不能從表中選擇相同的查詢,所以這可能會有點毛毛。 –