Re: How do you do a negative join?

Поиск
Список
Период
Сортировка
От Josh Berkus
Тема Re: How do you do a negative join?
Дата
Msg-id 200403261405.30653.josh@agliodbs.com
обсуждение исходный текст
Ответ на How do you do a negative join?  (dj00302003@yahoo.com (Jay Davis))
Список pgsql-novice
Jay,

> There must be a standard SQL method to query multiple
> tables in the following way.  Lets say we have two
> tables, 'allnames' and 'badnames'. We want to get the
> following result:
>
> "select name from allnames where name-is-not-in-badnames;"
>
> Clearly I'm an SQL novice but I haven't found any examples
> of such a query in my beginning SQL books.

Easy, two syntaxes:

SELECT allnames.name
WHERE allnames.name NOT IN (SELECT badnames.name
                        FROM badnames);

OR

SELECT allnames.name
WHERE NOT EXISTS (SELECT badnames.name
    WHERE badnames.name = allnames.name);

The second is better for PostgreSQL versions 7.2 and 7.3.  The first is better
for version 7.4.

--
-Josh Berkus
 Aglio Database Solutions
 San Francisco


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

Предыдущее
От: Aarni Ruuhimäki
Дата:
Сообщение: Re: Images in Database
Следующее
От: Bruno Wolff III
Дата:
Сообщение: Re: How do you do a negative join?