How do I get first unused ID in the table?

后端 未结 6 1964
温柔的废话
温柔的废话 2021-01-12 23:58

I have to write a query wherein i need to allocate a ID (unique key) for a particular record which is not being used / is not being generated / does not exist i

6条回答
  •  猫巷女王i
    2021-01-13 00:29

    Should work under MySql.

    SELECT TOP 100
        T1.ID + 1 AS FREE_ID 
    FROM TABLE1 T1
    LEFT JOIN TABLE2 T2 ON T2.ID = T1.ID + 1
    WHERE T2.ID IS NULL
    

提交回复
热议问题