Finding the next available id in MySQL

前端 未结 15 1826
予麋鹿
予麋鹿 2020-11-29 00:50

I have to find the next available id (if there are 5 data in database, I have to get the next available insert place which is 6) in a MySQL database. How can I do that? I h

15条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-29 01:26

    Given what you said in a comment:

    my id coloumn is auto increment i have to get the id and convert it to another base.So i need to get the next id before insert cause converted code will be inserted too.

    There is a way to do what you're asking, which is to ask the table what the next inserted row's id will be before you actually insert:

    SHOW TABLE STATUS WHERE name = "myTable"
    

    there will be a field in that result set called "Auto_increment" which tells you the next auto increment value.

提交回复
热议问题