mhearse has asked for the wisdom of the Perl Monks concerning the following question:
I have a web page which I used to add values to the table. I'd like to use ajax to retrieve the first unused externalPort integer. Not the max, the first unused integer. For example, assume the table has the following rows:create table natRules ( id integer primary key asc, description varchar(255), externalPort integer not null, unique(externalPort) )
So, in this case the first unused integer would be 2. How do I programatically retrieve that value. I'd like to use sql if possible.1|value1|3221 2|value2|78 3|value3|32 4|value4|1114 5|value5|5 6|value6|1
SELECT id FROM ( SELECT 1 AS id ) q1 WHERE NOT EXISTS ( SELECT 1 FROM unused WHERE id = 1 ) UNION ALL SELECT * FROM ( SELECT id + 1 FROM unused t WHERE NOT EXISTS ( SELECT 1 FROM unused ti WHERE ti.id = t.id + 1 ) ORDER BY id LIMIT 1 ) q2 ORDER BY id LIMIT 1
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Getting first unused integer from a sqlite integer column
by afoken (Chancellor) on Oct 21, 2010 at 19:08 UTC | |
by mhearse (Chaplain) on Oct 21, 2010 at 19:32 UTC | |
|
Re: Getting first unused integer from a sqlite integer column
by locked_user sundialsvc4 (Abbot) on Oct 22, 2010 at 00:52 UTC | |
by aquarium (Curate) on Oct 22, 2010 at 03:59 UTC | |
|
Re: Getting first unused integer from a sqlite integer column
by aquarium (Curate) on Oct 21, 2010 at 22:49 UTC |