Possible to implement a manual increment with just simple SQL INSERT?

后端 未结 11 2019
刺人心
刺人心 2021-01-05 11:54

I have a primary key that I don\'t want to auto increment (for various reasons) and so I\'m looking for a way to simply increment that field when I INSERT. By simply, I mean

11条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-05 12:29

    Try this instead:

    INSERT INTO Table1 (id, data_field)
    SELECT id, '[blob of data]' FROM (SELECT MAX(id) + 1 as id FROM Table1) tbl
    

    I wouldn't recommend doing it that way for any number of reasons though (performance, transaction safety, etc)

提交回复
热议问题