Re: Query to get the "next available" unique suffix for a name

Поиск
Список
Период
Сортировка
От John R Pierce
Тема Re: Query to get the "next available" unique suffix for a name
Дата
Msg-id 4CA14B30.1010609@hogranch.com
обсуждение исходный текст
Ответ на Query to get the "next available" unique suffix for a name  (Mike Christensen <mike@kitchenpc.com>)
Список pgsql-general
  On 09/27/10 6:36 PM, Mike Christensen wrote:
> Thus, the users table already has:
>
> MikeChristensen1
> MikeChristensen2
> MikeChristensen3
> MikeChristensen4
>
> I want to write a SQL query that figures out that MikeChristensen5 is
> the next available username and thus suggest it.  Here's some things I
> could do:

bogo pseudocode.
$n is 'MikeChristensen'

     table users {
         uid serial,
         username text unique,
         userroot text,
         namesuffix integer,
         ...
         }

     sql.exec ("begin;");
     sql.exec ("select max(namesuffix) from users where userroot=$1;", $n);
     $i = sql.result(1);
     newname = $n.$i;
     sql.exec("insert ......");
     sql,exec("commit;");



of course, you need to deal with an unique constraint exception on that
insert, and rollback and retry the whole thing.

В списке pgsql-general по дате отправления:

Предыдущее
От: Mike Christensen
Дата:
Сообщение: Re: Installing pgAdmin on a Mac
Следующее
От: Tim Uckun
Дата:
Сообщение: Re: Deleting orphaned records (not exists is very slow)