简体   繁体   English

仅当值大于当前值时才更新字段的SQL语句

[英]SQL statement to update a field only if the value is greater than the current one

So far, I have this statement: 到目前为止,我有这样的声明:

String query = "UPDATE Score SET Points = "+scorecard[TOTAL][i]+" WHERE Name = "+players[i].getName()+" AND Lastname = "+players[i].getLastName()";

Any idea how I should go about it? 知道我应该怎么做吗? I saw somewhere to use the CASE WHEN syntax but couldn't make it work. 我看到某处使用CASE WHEN语法但无法使其工作。

You'd want 你想要的

update score set points = ? where
      name = ? and lastname = ?
      and points < ?

As SQL. 作为SQL。 And please use a PreparedStatement. 请使用PreparedStatement。

try(PreparedStatement ps = con.prepareStatement("update score set " + 
     " points = ? where name = ? and lastname = ? and points < ?") {
  ps.setInt(1, scorecard[TOTAL][i]);
  ps.setString(2, players[i].getName());
  ps.setString(3, players[i].getLastName());
  ps.setInt(4, scorecard[TOTAL][i]);
  ps.executeUpdate(); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM