且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在MySQL中获取更新的值,而不是受影响的行

更新时间:2022-02-21 22:29:34

您可以使用存储过程来完成此操作,该存储过程将进行更新,然后将新值选择为输出参数. 以下返回具有新值的一列new_score.

You can do it with a stored procedure that updates, and then selects the new value into an output parameter. The following returns one column new_score with the new value.

DELIMITER $$   -- Change DELIMITER in order to use ; withn the procedure
CREATE PROCEDURE increment_score
(
   IN id_in INT
)
BEGIN
    UPDATE item SET score = score + 1 WHERE id = id_in;
    SELECT score AS new_score FROM item WHERE id = id_in;
END
$$            -- Finish CREATE PROCEDURE statement
DELIMITER ;   -- Reset DELIMITER to standard ;

在PHP中:

$result = mysql_query("CALL increment_score($id)");
$row = mysql_fetch_array($result);
echo $row['new_score'];