Re: BUG #2178: NOT IN command don't work

Поиск
Список
Период
Сортировка
От Jean-Pierre Pelletier
Тема Re: BUG #2178: NOT IN command don't work
Дата
Msg-id BAYC1-PASMTP0442C4EBB587635B8521F195130@CEZ.ICE
обсуждение исходный текст
Ответ на BUG #2178: NOT IN command don't work  ("Daniel Afonso Heisler" <daniel@solis.coop.br>)
Список pgsql-bugs
The expected behavior can be obtained by filtering out the null in
the subquery or by using "not exists" instead of "not in".

Here is an example:

CREATE TEMPORARY TABLE subquerytable (column1 INTEGER);
INSERT INTO subquerytable VALUES(2);
INSERT INTO subquerytable VALUES(NULL);
INSERT INTO subquerytable VALUES(3);

SELECT true WHERE 1 NOT IN (SELECT column1 FROM subquerytable); -- Wrong
SELECT true WHERE 1 NOT IN (SELECT column1 FROM subquerytable WHERE column1
IS NOT NULL); -- Ok
SELECT true WHERE NOT EXISTS(SELECT * FROM subquerytable WHERE 1 =
column1); -- Ok

It's not clear to me why "not exists" and "not in" return a different result
but it must be per SQL spec
as all DBMS I have seen do that (Oracle, SQL Server, MYSQL, ...)

In most queries I have seen column1 is NOT NULL so IN or EXISTS can both be
used safely.

Jean-Pierre Pelletier
e-djuster

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

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: BUG #2204: Feature Req: Unique output column names
Следующее
От: Alvaro Herrera
Дата:
Сообщение: Re: BUG #2204: Feature Req: Unique output column names