Re: Setting a default value for a select statement without results

Поиск
Список
Период
Сортировка
От Ian Lawrence Barwick
Тема Re: Setting a default value for a select statement without results
Дата
Msg-id CAB8KJ=iG_zZ+3sui5u8t48YxTb7-CU+JUDu=pzH4_C6b8M9EXA@mail.gmail.com
обсуждение исходный текст
Ответ на Setting a default value for a select statement without results  (JORGE MALDONADO <jorgemal1960@gmail.com>)
Список pgsql-sql
2013/2/6 JORGE MALDONADO <jorgemal1960@gmail.com>:
> I have an UPDATE query with the following general structure:
>
> UPDATE table1 SET (SELECT field FROM table2 WHERE conditions ORDER BY
> order_field LIMIT 1)
>
> Is it possible to assign a default value in case no results are returned by
> the SELECT statement?

One option would be to do something with UNION along these lines:

UPDATE table1 SET field = (SELECT field FROM table2 WHERE conditions     UNION  SELECT 'default_value' WHERE NOT EXISTS
(SELECTfield FROM table2
 
WHERE conditions )  ORDER BY order_field LIMIT 1 )

HTH

Ian Barwick



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

Предыдущее
От: Anton Gavazuk
Дата:
Сообщение: Re: Setting a default value for a select statement without results
Следующее
От: Wayne Cuddy
Дата:
Сообщение: index scan vs bitmap index scan