doushun1870 2017-12-27 20:43
浏览 63
已采纳

重置MySQL中行的递增值

I have a table with 10 rows. Each row has an id, name and a year. Each id has a value that is incremented by 3 from the previous value. For example, the first row has an id of 1, then the next has an id of 4 etc.

After deleting the first 9 rows, the next row I added instead of having an id of 4, it continued from 31.

How can I reset the value of the next added row to start from 4 (or the last value incremented by 3)?

  • 写回答

1条回答 默认 最新

  • drhs13583567608 2017-12-27 21:56
    关注

    Example of a FUNCTION that calculates the new AUTO_INCREMENT value for id field of a test table by a user defined step.

    CREATE FUNCTION `test`(`step` INT) RETURNS int(11)
        NO SQL
    BEGIN
    IF `step`<1 THEN
    SET `step` = 1;
    END IF;
    RETURN `step`+(SELECT MAX(`id`) FROM `test`);
    END
    

    You can use a function like that to calculate the new value for the id (AI) field when inserting new rows to the table. For example.

    INSERT INTO TABLE `test` (`id`,`value`) VALUES ((SELECT `test`(4)),'yourvalue');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容