简体   繁体   English

SQL命令插入的值将大于1然后大于最大值

[英]Sql command for inserting value that will be bigger in 1 then the maximal value

I need that the minimal id will be 0(zero). 我需要最小ID为0(零)。

I tried something like this: 我尝试过这样的事情:

INSERT INTO users(id, name) 
VALUES ( (SELECT MAX(id) FROM users) + 1 , 'andrey6' )

But I have a problem when the "users" table is empty, I just cant figure out how to keep the minimal value to be 0. 但是当“用户”表为空时,我有一个问题,我只是想不出如何将最小值保持为0。

Does this do what you want: 这是否满足您的要求:

INSERT INTO users(id, name) 
VALUES ( (SELECT IFNULL(MAX(id) + 1,0) FROM users) , 'andrey6' )

?

You could just use COALESCE like so: 您可以像这样使用COALESCE

INSERT INTO users(id, name) 
VALUES ( (SELECT COALESCE(MAX(id), 0) FROM users) + 1 , 'andrey6' )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM