Re: subselect return in where clause

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: subselect return in where clause
Дата
Msg-id 13472.1043769388@sss.pgh.pa.us
обсуждение исходный текст
Ответ на subselect return in where clause  (Michael Hostbaek <mich@the-lab.org>)
Список pgsql-sql
Michael Hostbaek <mich@the-lab.org> writes:
> select some_stuff, more_stuff, 
> (select other_stuff from other_table where other_table.stuff ilike
> stuff.match) as other_stuff from stuff where other_stuff = 'hello'

The above is nonsensical: WHERE cannot refer to values computed in
the SELECT list, because WHERE logically occurs before the SELECT
list is executed (indeed, the SELECT list will not be executed at
all, if WHERE returns false).

You could repeat yourself:

select some_stuff, more_stuff, 
(select other_stuff from other_table where other_table.stuff ilikestuff.match) as other_stuff from stuff
where
(select other_stuff from other_table where other_table.stuff ilikestuff.match) = 'hello'

or if it really bugs you to write the expression twice, perhaps
use a sub-select:

select * from
(select some_stuff, more_stuff, (select other_stuff from other_table where other_table.stuff ilike stuff.match) as
other_stufffrom stuff) as subselect
 
where other_stuff = 'hello'

although you shouldn't have any illusions about this being more
efficient than writing the expression twice.
        regards, tom lane


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

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: CR/LF conversion (was: import error)
Следующее
От: "Pedro Igor Craveiro e Silva"
Дата:
Сообщение: Cross-database references