Re: insert with select as value

From: John Sidney-Woollett <johnsw(at)wardbrook(dot)com>
To: Milos Prudek <prudek(at)bvx(dot)cz>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: insert with select as value
Date: 2004-06-22 14:52:49
Message-ID: 40D847C1.9050505@wardbrook.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Try rewriting the inner query as:

SELECT s.* FROM (
SELECT max(idthread)+1 as MX, 'CZE', 'sample text' FROM table WHERE
idsection = 'CZE'
union
SELECT 1 as MX, 'CZE', 'sample text'
) as s
ORDER BY s.MX desc
LIMIT 1

I think that should work, and always return a row.

John Sidney-Woollett

Milos Prudek wrote:

> I need to insert a value = max(value)+1, where max is a select limited
> by a 'where' clause. Like this:
>
> INSERT INTO table (idthread, idsection,txt)
> VALUES (
> (SELECT max(idthread)+1 FROM table WHERE idsection = 'CZE'),
> 'CZE', 'sample text')
> );
>
> This works fine, except when the result of SELECT is empty - which is
> true when the table is empty.
>
> Is it possible to create a "SELECT max(idthread)+1 FROM table WHERE
> idsection = 'CZE';" that will return value 1 instead of value None if
> the SELECT has no results?
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Milos Prudek 2004-06-22 15:01:05 Re: insert with select as value
Previous Message Marcelo Soares 2004-06-22 14:37:40 Re: insert with select as value