那么你可以使用 update 表名 set id=id-4999
如果你原表主键ID不是连续的,那上面这个方法行不通
先执行第一句 :SELECT @rownum := 0
然后再执行下面这一句
update 表名 as t set t.ID = (SELECT (@rownum :=@rownum+1))
工具:SQLyog
DELIMITER $$USE `test`$$
DROP PROCEDURE IF EXISTS `p_update_student_score`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `p_update_student_score`()
BEGIN
DECLARE ids INT
DECLARE ranks INT
DECLARE stopFlag INT
DECLARE cursor_ids CURSOR FOR SELECT t.id ids,@a := @a + 1 ranks FROM (SELECT * FROM STUDENT ORDER BY score DESC) t ,(SELECT @a := 0) AS a
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET stopFlag=1
OPEN cursor_ids
REPEAT
FETCH cursor_ids INTO ids,ranks
UPDATE STUDENT SET RANK = ranks WHERE id = ids
UNTIL stopFlag END REPEAT
CLOSE cursor_ids
END$$
DELIMITER
用法:
CALL p_update_student_score结果:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)