为什么在这些查询中选择的元组数量不同
Why does the number of tuples selected vary in these queries
我是 DBMS 新手 SQL。在练习一些基本查询时,我遇到了这个疑问。
假设此查询的结果为空:select * from table2 b where b.age>50;
即表 2 中没有年龄大于 50。
为什么像 select * from table1 a where a.age> all(select b.age from table2 b where b.age>50);
select 这样的查询 table1
中的所有元组
而查询:select * from table1 a where a.age> any(select b.age from table2 b where b.age>50);
selects 0 个元组。
这些背后的逻辑是什么(可能是SQL解释器的观点)?
select * from table1 a where a.age > all(select b.age from table2 b where b.age>50);
您的第一个查询从 table1
获取每条记录,然后检查 age
是否大于子查询的 all 结果。由于子查询没有结果,所以总是这样。
select * from table1 a where a.age > any(select b.age from table2 b where b.age>50);
您的第二个查询从 table1
获取每条记录,然后检查 age
是否大于子查询的 any 结果。由于子查询没有结果,所以这不是真的。
对于ANY
(或SOME
),强调的是:
Compares a value to each value in a list or returned by a query. Must be preceded by =, !=, >, <, <=, >=. Can be followed by any expression or subquery that returns one or more values.
Evaluates to FALSE if the query returns no rows.
对于ALL
:
Compares a value to every value in a list or returned by a query. Must be preceded by =, !=, >, <, <=, >=. Can be followed by any expression or subquery that returns one or more values.
Evaluates to TRUE if the query returns no rows.
由于子查询没有任何行,因此预计 ANY
版本的计算结果为 false,因为
select * from table1 a where a.age> all(select b.age from table2 b where b.age>50);
计算为(忽略你不能明确地拥有 true/false):
select * from table1 a where false; -- or more legally: where 1=0
所以 returns 没有行;
但是 ALL
条件计算结果为真,因为
select * from table1 a where true; -- or more legally: where 1=1
等等 returns 所有行。
我是 DBMS 新手 SQL。在练习一些基本查询时,我遇到了这个疑问。
假设此查询的结果为空:select * from table2 b where b.age>50;
即表 2 中没有年龄大于 50。
为什么像 select * from table1 a where a.age> all(select b.age from table2 b where b.age>50);
select 这样的查询 table1
而查询:select * from table1 a where a.age> any(select b.age from table2 b where b.age>50);
selects 0 个元组。
这些背后的逻辑是什么(可能是SQL解释器的观点)?
select * from table1 a where a.age > all(select b.age from table2 b where b.age>50);
您的第一个查询从 table1
获取每条记录,然后检查 age
是否大于子查询的 all 结果。由于子查询没有结果,所以总是这样。
select * from table1 a where a.age > any(select b.age from table2 b where b.age>50);
您的第二个查询从 table1
获取每条记录,然后检查 age
是否大于子查询的 any 结果。由于子查询没有结果,所以这不是真的。
对于ANY
(或SOME
),强调的是:
Compares a value to each value in a list or returned by a query. Must be preceded by =, !=, >, <, <=, >=. Can be followed by any expression or subquery that returns one or more values.
Evaluates to FALSE if the query returns no rows.
对于ALL
:
Compares a value to every value in a list or returned by a query. Must be preceded by =, !=, >, <, <=, >=. Can be followed by any expression or subquery that returns one or more values.
Evaluates to TRUE if the query returns no rows.
由于子查询没有任何行,因此预计 ANY
版本的计算结果为 false,因为
select * from table1 a where a.age> all(select b.age from table2 b where b.age>50);
计算为(忽略你不能明确地拥有 true/false):
select * from table1 a where false; -- or more legally: where 1=0
所以 returns 没有行;
但是 ALL
条件计算结果为真,因为
select * from table1 a where true; -- or more legally: where 1=1
等等 returns 所有行。