[GENERAL] why isn't this subquery wrong?

Поиск
Список
Период
Сортировка
От jonathan vanasco
Тема [GENERAL] why isn't this subquery wrong?
Дата
Msg-id 0B9030D9-7B8A-4921-9453-D0E3C6254818@2xlp.com
обсуждение исходный текст
Ответы Re: [GENERAL] why isn't this subquery wrong?  (Steve Crawford <scrawford@pinpointresearch.com>)
Re: [GENERAL] why isn't this subquery wrong?  ("David G. Johnston" <david.g.johnston@gmail.com>)
Re: [GENERAL] why isn't this subquery wrong?  (Tom Lane <tgl@sss.pgh.pa.us>)
Re: [GENERAL] why isn't this subquery wrong?  ("David G. Johnston" <david.g.johnston@gmail.com>)
Список pgsql-general

I ran into an issue while changing a database schema around.  Some queries still worked, even though I didn't expect them to.

Can anyone explain to me why the following is valid (running 9.6) ?

schema
CREATE TEMPORARY TABLE example_a__data (
foo_id INT,
bar_id INT
);
CREATE TEMPORARY TABLE example_a__rollup_source (
id int primary key,
name varchar(64),
foo_id INT,
check_bool BOOLEAN
);
CREATE TEMPORARY TABLE example_a__rollup AS
SELECT id, name, foo_id
FROM example_a__rollup_source
WHERE check_bool IS TRUE
;

query:
SELECT foo_id
FROM example_a__data
WHERE foo_id IN (SELECT bar_id FROM example_a__rollup)
;

a raw select of `SELECT bar_id FROM example_a__rollup;` will cause an error because bar_id doesn't exist

postgres doesn't raise an error because example_a__data does have a bar_id -- but example_a__rollup doesn't and there's no explicit correlation in the query.

can someone explain why this happens?  i'm guessing there is a good reason -- but I'm unfamiliar with the type of implicit join/queries this behavior is enabling.


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

Предыдущее
От: Samuel Williams
Дата:
Сообщение: Re: [GENERAL] Large data and slow queries
Следующее
От: Steve Crawford
Дата:
Сообщение: Re: [GENERAL] why isn't this subquery wrong?