這個問題在這里已經有了答案: postgresql 子查詢在無效列上失敗時回傳行 (2 個回答) 10 小時前關閉。
create table table1
(
abool boolean
);
create table table2
(
astring varchar
);
select *
from table1
where (
select abool
from (select astring from table2 limit 1) x
);
小提琴:http ://sqlfiddle.com/#!17/b2f9ae/6
有人可以理解選擇,更具體地說是子選擇,其中看起來像 abool 是從只包含 astring 列的結果集中選擇的嗎?
我的意思是,我明白會發生什么,abool 實際上指的是 table1,但在我看來它是錯誤的。
如果有人可以向我指出一個更一般的原則來解釋這一點,我將不勝感激。
uj5u.com熱心網友回復:
為什么有可能?所有四個查詢在邏輯上是等價的:
select *
from table1
where abool
select *
from table1
where (select abool)
select *
from table1
where (select abool from (select 1) sub)
select *
from table1
where (select abool from (select 1 from table2 limit 1) sub)
查詢有意義嗎?只有當你真的想讓你的同事生活更艱難或對他們耍花招時。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/340190.html
標籤:sql PostgreSQL
