Re: Conditional on Select List

Поиск
Список
Период
Сортировка
От Sam Mason
Тема Re: Conditional on Select List
Дата
Msg-id 20080513160417.GD1657@frubble.xen.chris-lamb.co.uk
обсуждение исходный текст
Ответ на Conditional on Select List  (Fernando <fernando@ggtours.ca>)
Список pgsql-general
On Tue, May 13, 2008 at 11:52:24AM -0400, Fernando wrote:
> Is it possible to do this?
>
> SELECT IF(COUNT(colname) > 0, TRUE, FALSE) AS colname FROM table;
>
> What I want is to return a boolean, but when I tried SELECT
> COUNT(colname)::BOOLEAN FROM table; it says it cannot cast bigint to
> boolean.

Why not just do something like this?

  SELECT COUNT(colname) > 0 AS colname FROM table;

If you want a real conditional statement, CASE is probably what you
want:

  SELECT CASE WHEN COUNT(colname) > 0 THEN TRUE ELSE FALSE END AS colname FROM table;

Note that the ELSE clause is executed when the expression evaluates to
either NULL or FALSE, but because COUNT never returns a NULL value it
doesn't matter here.  It's also possible to have multiple WHEN clauses.


  Sam

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

Предыдущее
От: "Joshua D. Drake"
Дата:
Сообщение: Re: Conditional on Select List
Следующее
От: "Adam Rich"
Дата:
Сообщение: Re: Conditional on Select List