我正在寻找一个解决方案,以便在表格中找到一个未使用的数字。到目前为止,我遇到的大多数解决方案都是创建一个包含所有数字的临时表,并使用left join查找未使用的数字。在我的例子中,我没有机会创建临时表。
前导为零的数字范围:0001-1999。这些号码是拨号盘号码,长度必须为4位数字。
列表:
+----+--------+------------+
| id | title | pad_number |
+----+--------+------------+
| 1 | Foo | 0001 |
| 2 | bar | 0005 |
| 3 | Baz | 1999 |
| 10 | FooBar | 0002 |
+----+--------+------------+预期结果:
0003有没有办法找回号码?
发布于 2018-03-15 11:10:45
我会这样做:
select lpad(min(pad_number) + 1, 4, '0')
from t
where not exists (select 1 from t t2 where t2.pad_number = t.pad_number + 1);发布于 2018-03-15 08:51:30
使用not exists。
select lpad(cast(cast(pad_number as unsigned)+1 as char(4)),4,'0')
from tbl t
where not exists (select 1 from tbl t1
where cast(t.pad_number as unsigned)=cast(t1.pad_number as unsigned)-1)
and cast(pad_number as unsigned) >=0 and cast(pad_number as unsigned) < 1999
order by 1
limit 1https://stackoverflow.com/questions/49289885
复制相似问题