Re: Subselect AS and Where clause

Поиск
Список
Период
Сортировка
От Thomas Kellerer
Тема Re: Subselect AS and Where clause
Дата
Msg-id iholgb$usu$1@dough.gmane.org
обсуждение исходный текст
Ответ на Subselect AS and Where clause  (Uwe Schroeder <uwe@oss4u.com>)
Ответы Re: Subselect AS and Where clause
Список pgsql-general
Uwe Schroeder, 26.01.2011 08:34:
> I have a query like this:
>
> SELECT a,b,c, (select problem from other_table where id=a) as problem FROM
> mytable WHERE a=1
>
> So far so good. Actually "problem" always resolves to one record, so it's not
> the "multiple records returned" problem.
>
> What I try to do is this:
>
> SELECT a,b,c, (select problem from other_table where id=a) as problem FROM
> mytable WHERE a=1 and problem = 3
>
> see the "problem=3" part in the where clause? The error I get is
>
>   SQLError: (ProgrammingError) column "problem" does not exist
>
You need to wrap the whole SELECT in order to be able to use the column alias:

SELECT *
FROM (
   SELECT a,
          b,
          c,
          (select problem from other_table where id=a) as problem
   FROM mytable
) t
WHERE a=1
   AND problem = 3

Regards
Thomas

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

Предыдущее
От: Guillaume Lelarge
Дата:
Сообщение: Re: Install Pgadmin3 1.12 on ubuntu 10.4 lucid client without postgresql server install
Следующее
От: Sim Zacks
Дата:
Сообщение: Re: Subselect AS and Where clause