Finding the next available id in MySQL

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

    Update 2014-12-05: I am not recommending this approach due to reasons laid out in Simon's (accepted) answer as well as Diego's comment. Please use query below at your own risk.

    The shortest one i found on mysql developer site:

    SELECT Auto_increment FROM information_schema.tables WHERE table_name='the_table_you_want'
    

    mind you if you have few databases with same tables, you should specify database name as well, like so:

    SELECT Auto_increment FROM information_schema.tables WHERE table_name='the_table_you_want' AND table_schema='the_database_you_want';
    

提交回复
热议问题