Finding the next available id in MySQL

前端 未结 15 1805
予麋鹿
予麋鹿 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:51

    It's too late to answer this question now, but hope this helps someone.

    @Eimantas has already given the best answer but the solution won't work if you have two or more tables by the same name under the same server.

    I have slightly modified @Eimantas's answer to tackle the above problem.

    select Auto_increment as id from information_schema.tables where table_name = 'table_name' and table_schema = 'database_name'
    

提交回复
热议问题