【问题标题】:codeigniter sort id after delete [duplicate]删除后的codeigniter排序ID [重复]
【发布时间】:2016-05-24 13:32:12
【问题描述】:

我想在删除原始后重建 id....

e.x

1 Text1 Number
2 Text2 Number
3 Text3 Number
4 Text4 Number
5 Text5 Number

删除 raw 4. 我希望表格看起来像:

1 Text1 Number
2 Text2 Number
3 Text3 Number
4 Text5 Number

当我添加新项目时添加到 id 5

我在使用 codeigniter,我的控制器是:

删除函数:

$this->db->query("DELETE FROM bookmark WHERE id = '$id'");

添加功能:

 $this->db->query("INSERT INTO bookmark VALUES ('$id', '$text', '$number')");

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    删除后运行这个mysql命令

    SET @num := 0;
    
    UPDATE your_table SET id = @num := (@num+1);
    
    ALTER TABLE tableName AUTO_INCREMENT = 1;
    

    在 codeigniter 活动记录中用作:-

    $this->db->query("SET @num := 0;");
    $this->db->query("UPDATE your_table SET id = @num := (@num+1);");
    $this->db->query("ALTER TABLE tableName AUTO_INCREMENT = 1;");
    

    在此处查找更多信息link

    【讨论】:

    • 是否可以在删除行后自动执行此操作?在codeigniter php代码中
    • 我收到了这个错误:prntscr.com/a2teti
    • 手动删除 phpmyadmin 中的一行并在 phpmyadmin 中运行此查询语句并告诉我那里运行良好?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    • 1970-01-01
    • 2012-07-16
    • 1970-01-01
    • 2013-05-02
    • 2011-02-09
    • 1970-01-01
    相关资源
    最近更新 更多