获取最后一个自动增量 php [英] get last auto increment php

查看:39
本文介绍了获取最后一个自动增量 php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序有问题,它从我的数据库表中一次调用一个问题列(问题),以 id=1、id=2、id=3 等开头.Id 是 Auto_increment.当我从数据库中删除一个 id 时,我的程序就搞砸了.

I'm having an issue with my program where it calls questions column(questions) one at a time from my database table starting with id=1, id=2, id=3 and so on. Id is Auto_increment. When I delete a id from the database, my program messes up.

数据库

    id     question
    1     Php?
    2     HTML?
    3     CSS?
    4     Java?

网页应该这样显示

#1 Php?
#2 HTML?
#3 CSS?
#4 Java?

当我删除或添加一个 id 乱序或丢失的问题时会出现问题

The problem happens when I delete or add a question with an id out of order or missing

        id     question
        1     Php?
        2     HTML? <--- 3 is deleted and webpage messes up.
        4     Java?
        5     Oracle
        6     C++

我如何解决这个问题,其中 id 总是 id=1,2,3,4,5,6...n 并确保当我在底部添加一个新问题时,id 将是 last_id+1 使用php和mysql?

How would I fix this problem where id will always be id=1,2,3,4,5,6...n and make sure when I add a new question at the bottom, the id will be last_id+1 using php and mysql?

我认为我没有正确解释我的问题.

I don't think I explained my problem correctly.

我会看看我是否可以一次解决一件事.

I'll see if I can fix one thing at a time.

如果我的数据库中有一个 id 列,id=1,2,3,4,5,6,如果我已经删除了 id=,我将如何调用数据库中最后一个 id=6 的最后一个 id=7,8?当我插入一个新问题时,由于自动递增,问题 ID 将是 id=9.我希望新问题的 id 为 id=7.也许我可以先寻求帮助来解决这个问题.

If I have an id column in my database with id=1,2,3,4,5,6, how would I call the last id which last id=6 in the database if I've already deleted id=7,8? When I insert a new question the question id will be id=9 because of auto increment. I want the new question's id to be id=7. Maybe I can get help to fix this issue first.

推荐答案

区分作为主键的 id 和行的序号很重要.

It is important to differentate between the id as the primary key, and the sequence number of the row.

试试这个:

SELECT
  id, question,
  @seq:=@seq+1 AS seq
FROM
  questions,
  (SELECT @seq:=0) AS init
;

您现在可以使用 seq 来显示数字,始终以正确的顺序,但请确保您将您的操作与 id 相关联,而不是与序列相关联,以便您可以通过 PK 引用 rwo.

You can now use seq to show the number, always in correct sequence, but make sure, that you associate your actions with the id, not the sequence, so you can reference the rwo by PK.

这篇关于获取最后一个自动增量 php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆