Search code examples
mysqlnode-mysql

mysql, sql Update only if a record was found


The requirement is a conditional update where I'd want to update the record only if user row was found. Researched a lot on WHERE EXISTS, IF, and CASE but can't figure out a solution.

serviceRequest.sql = "SELECT @id:=id FROM user_token WHERE authToken = ?;\
     UPDATE user_token SET authToken = ? WHERE id = @id;";

serviceRequest.values = [request.query.authToken, "newToken"];

Solution

  • You would use just the UPDATE. There is no need for a SELECT:

    UPDATE user_token
        SET authToken = ?
        WHERE authToken = ?;