java - sql - way to only update 1 (the first the query finds) value when i got 2 or more with the same value? -
i have code in java replace minimum score in highscore table if got more 2 or more same score , minimum score in table replace of them, wanna replace 1 of them (anyone of them).
preparedstatement preparedstatement = conn.preparestatement("update highscore set name = ?, score = ?, time = ?, level = ? score = (select top 1 score highscore order score > asc)");
preparedstatement.setstring(1, name);
preparedstatement.setint(2, score);
preparedstatement.setint(3, time);
preparedstatement.setstring(4, level);
int updatecount = preparedstatement.executeupdate();
hey woundering if there way update 1 (the first query finds) value when got 2 or more same value.
this not seem question related java rather 1 related database design.
colloquially, every row in table must uniquely identifiable (according relational model theory); called data integrity. easiest way achieve add attribute serving primary key in table.
commonly, primary key integer number starting @ 1 , incremented each additional row add in table.
now come question: add primary key table , 'where'-condition can modified as:
update highscore set name = ?, score = ?, time = ?, level = ? primarykey = (select top 1 primarykey highscore order score)
note 'where'-condition gets primary key, called 'primarykey' simplicty, scores , updated.
Comments
Post a Comment