我有一张table叫 chest
```
chest_id integer NOT NULL
index integer NOT NULL
我可以通过查询得到下一个索引
select max(index) + 1 from chest group by chest_id
如果顺序中有某个索引未填写,如何获取?例如:
chest_id | index
0 | 0
1 | 1
2 | 2
1 | 4
如何查询以返回第一个可用索引?在上面的例子中是3。但如果它被填满了,下一个可用的将是5
我有一张table叫 chest
```
chest_id integer NOT NULL
index integer NOT NULL
我可以通过查询得到下一个索引
select max(index) + 1 from chest group by chest_id
如果顺序中有某个索引未填写,如何获取?例如:
chest_id | index
0 | 0
1 | 1
2 | 2
1 | 4
如何查询以返回第一个可用索引?在上面的例子中是3。但如果它被填满了,下一个可用的将是5
1条答案
按热度按时间iyr7buue1#
可以使用窗口函数:
这将为您提供第一个可用的
idx
在表中(要么是间隙,要么是最大值+1)。请注意
index
是语言关键字,因此不是列名的好选择。我把它改名为idx
.另一个选择是
not exists
: