mysql find smallest + unique id available

后端 未结 7 663
忘了有多久
忘了有多久 2020-12-09 02:54

i have a column ID and something like 1000 items, some of then were removed like id=90, id=127, id=326

how can i make a query to look for those availabl

7条回答
  •  我在风中等你
    2020-12-09 03:06

    Note that the answers by shamittomar and Haim Evgi don't work if the lowest ID is free. To allow for the refilling the lowest ID, pre-check to see whether it is available:

    SELECT TRUE FROM tablename WHERE ID = 1;

    If this returns anything, then the ID of 1 is not free and you should use their answer. But if the ID of 1 is free, just use that.

提交回复
热议问题