Re: using EXISTS instead of IN: how?

Поиск
Список
Период
Сортировка
От Mike Mascari
Тема Re: using EXISTS instead of IN: how?
Дата
Msg-id 3F1D68AD.1040103@mascari.com
обсуждение исходный текст
Ответ на using EXISTS instead of IN: how?  (Rory Campbell-Lange <rory@campbell-lange.net>)
Список pgsql-general
Rory Campbell-Lange wrote:

> I have been informed that at present (postgres 7.3.2) using IN is not
> advised, and I should  replace it with EXISTS. I can't seem to get it to
> work.

...

>
>     SELECT
>         name
>     FROM
>         people
>     WHERE
>         exists (
>             SELECT
>                 1
>             FROM
>                 states
>             WHERE
>                 name ~* 'r'
>         );

You should correlate the subquery with the outer query:

SELECT name
FROM people
WHERE EXISTS (
 SELECT 1
 FROM states
 WHERE people.state = states.id AND
 states.name ~* 'r'
);

But I don't see why you just don't use a join:

SELECT people.name
FROM people, states
WHERE people.state = states.id AND
states.name ~* 'r';

Hope that helps,

Mike Mascari
mascarm@mascari.com





>
> However the second example simply finds all records in people.
>
> Thanks for any help,
> Rory
>



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

Предыдущее
От: DeJuan Jackson
Дата:
Сообщение: Re: using EXISTS instead of IN: how?
Следующее
От: Felipe Schnack
Дата:
Сообщение: Re: using EXISTS instead of IN: how?