人大金仓KingbaseES数据库管理系统-all表达式用法

关键字:人大金仓、KingbaseES、all

语法

expression operator ALL (expression[,…]|array_expression|subquery)

expression表示表达式,可以是数值常量,字符串常量,列名,函数等或者这几种元素的组合。

operator表示比较操作符,可取=, !=,^=,<>, >, <, <=, >=。

array_expression表示数组表达式,如array[1,2,3],'{1,2,3}'。

subquery表示子查询。

all表达式的结果为boolean类型,取值规则见5.2.1.1.1.2功能描述。

功能描述

条件

要求/结果

operator

单表达式比较

可取=, !=,^=,<>, >, <, <=, >=

一维表达式比较

可取=, !=,^=,<>, >, <, <=, >=

多维表达式比较

可取=, !=,^=,<>, >, <, <=, >=

右表达式形式

列表,数组,子查询

左表达式与右表达式基本元素

成员数目相等,对应成员数据类型可比较(类型相同或存在类型隐式转换,无法转换则报错);

右列表/数组

右数组不包含任何元素(不支持右列表为空列表(不包含任何元素))

all表达式结果为TRUE

左表达式与右列表/数组所有元素比较结果包含至少一个FALSE

all表达式结果为FALSE

左表达式与右列表/数组所有元素比较结果全为TRUE

all表达式结果为TRUE

左表达式与右列表/数组所有元素比较结果不包含FALSE且包含至少一个null

all表达式结果为null

右子查询

右子查询返回空行(包括左表达式为null)

all表达式结果为TRUE

左表达式与右子查询所有行比较结果包含至少一个FALSE

all表达式结果为FALSE

左表达式与右子查询所有行比较结果全为TRUE

all表达式结果为TRUE

左表达式与右子查询所有行比较结果不包含FALSE且包含至少一个null

all表达式结果为null

all表达式位置

投影列,过滤条件

执行层

执行顺序

先转换右表达式元素为同一类型,后逐个比较

优化顺序

无优化,顺序执行,遇到FALSE停止

使用示例

示例1:单表达式-数值常量

表结构构造:

drop table t1;

create table t1(id int, name varchar(20));

insert into t1 select rownum id, 'row'||rownum name from dual connect by level <=7;

temp

语句:

select 4 > all('{1,2,3}');

select 4 > all(1,2,3) as res;

select 4 > all(1,2,4) as res;

select 4 > all(array[1,2,3]) as res;

select 4 > all(array[1,2,4]) as res;

select 4 > all(select id from t1 where id < 4) as res;

select 4 > all(select id from t1 where id < 7) as res;

结果:

temp

结论:

all表达式支持数值常量列表。

判断规则:

  1. 左表达式与右列表/数组/子查询每个元素比较结果全为TRUE时,all表达式的结果为TRUE。
  2. 左表达式与右列表/数组/子查询每个元素比较结果有一个为FALSE时,all表达式的结果为FALSE。

示例2:单表达式-字符串常量

表结构构造:

drop table t1;

create table t1(id int, name varchar(20));

insert into t1 select rownum id, 'row'||rownum name from dual connect by level <=7;

temp

语句:

select 'row4' > all('{"row1", "row2"}') as res;

select 'row4' > all('row1','row2','row3') as res;

select 'row4' > all('row1','row2','row4') as res;

select 'row4' > all(array['row1','row2','row3']) as res;

select 'row4' > all(array['row1','row2','row4']) as res;

select 'row4' > all(select name from t1 where id < 4) as res;

select 'row4' > all(select name from t1 where id < 7) as res;

结果:

temp

结论:

all表达式支持字符串常量列表。

判断规则:

  1. 左表达式与右列表/数组/子查询每个元素比较结果全为TRUE时,all表达式的结果为TRUE。
  2. 左表达式与右列表/数组/子查询每个元素比较结果有一个为FALSE时,all表达式的结果为FALSE。

示例3:单表达式-列名

表结构构造:

drop table t1;

create table t1(id int, name varchar(20));

insert into t1 select rownum id, 'row'||rownum name from dual connect by level <=7;

temp

语句:

select id > all(1,2,id-1) as res from t1;

select id > all(array[1,2,id-1]) as res from t1;

select id > all(select id from t1 where id < 4) as res from t1;

结果:

temp

结论:

all表达式支持列表。

all表达式的判断规则:

  1. 左表达式与右列表/数组/子查询每个元素比较结果全为TRUE时,all表达式的结果为TRUE。
  2. 左表达式与右列表/数组/子查询每个元素比较结果有一个为FALSE时,all表达式的结果为FALSE。

示例4:单表达式示例-函数

表结构构造:

drop table t1;

create table t1(id int, name varchar(20));

insert into t1 select rownum id, 'row'||rownum name from dual connect by level <=7;

drop function my_add;

create function my_add(p1 int, p2 int) returns int language plpgsql as $$ begin

return (p1 + p2);

end;

$$;

temp

语句:

select '2022-10-13' > all(current_date) as res;

select '2022-10-13' > all(array[current_date]) as res;

select 4 > all(my_add(1,1), my_add(1,2)) as res;

select my_add(1,3) > all(my_add(1,1), my_add(1,2), my_add(1,3)) as res;

select 4 > all(array[my_add(1,1), my_add(1,2)]) as res;

select my_add(1,3) > all(array[my_add(1,1), my_add(1,2), my_add(1,3)]) as res;

select my_add(id,0) > all(select my_add(id,1) from t1 where id < 4) as res from t1;

结果:

temp

结论:

all表达式支持在左右表达式中包含函数。

all表达式的判断规则:

  1. 左表达式与右列表/数组/子查询每个元素比较结果全为TRUE时,all表达式的结果为TRUE。
  2. 左表达式与右列表/数组/子查询每个元素比较结果有一个为FALSE时,all表达式的结果为FALSE。

示例5:一维表达式示例

表结构构造:

drop table t1;

create table t1(id int, name varchar(20));

insert into t1 select rownum id, 'row'||rownum name from dual connect by level <=7;

temp

语句:

select (1,2,3) = all((1,2,3)) as res;

select (1,2,3) = all((1,2,3),(1,2,4)) as res;

select (1,2,3) = all(array[(1,2,3)]) as res;

select (1,2,3) = all(array[(1,2,3),(1,2,4)]) as res;

select (id,id+1,id+2) = all(select id,id+1,id+2 from t1 where id = 1) as res from t1;

结果:

temp

结论:

all表达式支持一维列表。

判断规则:

1)左表达式与右列表/数组/子查询每个元素比较结果全为TRUE时,all表达式的结果为TRUE。

2)左表达式与右列表/数组/子查询每个元素比较结果有一个为FALSE时,all表达式的结果为FALSE。

示例6:多维表达式示例

表结构构造:

drop table t1;

create table t1(id int, name varchar(20));

insert into t1 select rownum id, 'row'||rownum name from dual connect by level <=7;

temp

语句:

select ((1,2),(2,2)) > all(((1,1),(2,2))) as res;

select ((1,2),(2,2)) > all(array[((1,1),(2,2))]) as res;

select ((id+1,id+2),(id+2,id+2)) > all(select (id+1,id+1),(id+2,id+2) from t1 where id < 4) as res from t1;

select (((1,2),(2,2)),((1,2),(2,2))) > all((((1,1),(2,2)), ((1,1),(2,2)))) as res;

select (((1,2),(2,2)),((1,2),(2,2))) > all(array[(((1,1),(2,2)), ((1,1),(2,2)))]) as res;

select (((id+1,id+2),(id+2,id+2)),((id+1,id+2),(id+2,id+2))) > all(select ((id+1,id+1),(id+2,id+2)),((id+1,id+1),(id+2,id+2)) from t1 where id < 4) as res from t1;

结果:

temp

结论:

all表达式支持二维表达式列表,也支持三维表达式列表。

示例7:测试操作符

表结构构造:

drop table t1;

create table t1(id int, name varchar(20));

insert into t1 select rownum id, 'row'||rownum name from dual connect by level <=7;

temp

语句:

select (1,2,3) = all((1,2,3)) as res;

select (1,2,3) <> all((1,2,3)) as res;

select (1,2,3) != all((1,2,3)) as res;

select (1,2,3) ^= all((1,2,3)) as res;

select (1,2,3) > all((1,2,3)) as res;

select (1,2,3) >= all((1,2,3)) as res;

select (1,2,3) < all((1,2,3)) as res;

select (1,2,3) <= all((1,2,3)) as res;

结果:

temp

结论:

all表达式对一维列表支持=,<>,!=,^=,>,>=,<,<=等操作符。

示例8:右列表/数组/子查询不包含任何元素

表结构构造:

drop table t1;

create table t1(id int, name varchar(20));

insert into t1 select rownum id, 'row'||rownum name from dual connect by level <=7;

temp

语句:

select (4>all()) as res;

select (4>all(array[]::int[])) as res;

select (null>all(array[]::int[])) as res;

select (4>all(select id from t1 where id<1)) as res;

select (null>all(select id from t1 where id<1)) as res;

结果:

temp

结论:

all表达式不支持列表不包含任何元素,同Oracle 21c行为一致。

判断规则:右数组/子查询不包含任何元素时,all表达式的结果为TRUE。

示例9:右列表/数组包含单元素

表结构构造:

测试语句:

select (4>all(2)) as res;

select (4>all(4)) as res;

select (4>all(array[2])) as res;

select (4>all(array[4])) as res;

select (4 > all('{2}')) as res;

select (4 > all('{4}')) as res;

select ('row4' > all('row2')) as res;

select ('row4' > all('row4')) as res;

测试结果:

temp

测试结论:

all表达式对数组及数组标量的支持,也支持右列表包含一个元素。

示例10:null测试

表结构构造:

drop table t1;

create table t1(id int, grade int);

insert into t1 select rownum id, rownum*10 grade from dual connect by level <=7;

insert into t1(id) values(8);

temp

语句:

select (null>all(1)) as res;

select (null>all(null)) as res;

select (null>all(array[1])) as res;

select (null>all(array[null])) as res;

select (null>all(select grade from t1)) as res;

select (4>all(null,null,2,5)) as res;

select (4>all(null,null,2)) as res;

select (4>all(array[null,null,2,5])) as res;

select (4>all(array[null,null,2])) as res;

select (4>all(select grade from t1 where id>6)) as res;

结果:

temp

结论:

判断规则:

  1. 左表达式与右列表/数组/子查询每个元素比较结果包含一个FALSE时,all表达式的结果为FALSE。
  2. 左表达式与右列表/数组/子查询每个元素比较结果不包含FALSE且至少包含一个null时,all表达式的结果为null。

示例11:视图备份与还原

表结构构造:

drop table t1;

create table t1(id int, grade int);

insert into t1 select rownum id, rownum*10 grade from dual connect by level <=7;

insert into t1(id) values(8);

temp

语句:

create view v1 as select * from t1 where id > all(1, '2.3', 3.5);

select * from v1;

\d+ v1

结果:

temp

退出psql,在命令行下输入 bin/pg_dump -f test.sql test 。

用vi查看test.sql中v1定义,结果如下

temp

进入psql,删除数据库test,创建新的数据库test2,重新加载test.sql。

temp

temp

结论:

视图定义时,对于all表达式,已经确定了左右表达式的类型。视图定义中包含all表达式,可以正常备份与还原。

对比总结

(1)Oracle all表达式描述

Oracle 21c对all条件表达式描述如下:

op ALL

"op" must be one of =, !=, >, <, <=, or >=.

op ALL compares a value on the left side either to each value in a list, or to each value returned by a subquery, whichever is specified on the right side, using the condition op.

If any of these comparisons returns FALSE, op ALL returns FALSE.

If all of these comparisons return TRUE, or the subquery on the right side returns no rows, op ALL returns TRUE .

Otherwise, the return value is UNKNOWN.

语法结构如下:

关于UNKNOWN,Oracle 21c如此描述,

Comparison conditions compare one expression with another. The result of such a comparison can be TRUE, FALSE, or UNKNOWN.

Table 6-4 Logical Conditions

Type of Condition

Operation

NOT

Returns TRUE if the following condition is FALSE. Returns FALSE if it is TRUE. If it is UNKNOWN, then it remains UNKNOWN.

AND

Returns TRUE if both component conditions are TRUE. Returns FALSE if either is FALSE. Otherwise returns UNKNOWN.

OR

Returns TRUE if either component condition is TRUE. Returns FALSE if both are FALSE. Otherwise returns UNKNOWN.

由此可见,UNKNOWN实际上时null在boolean类型下的别名。以后,我们用null表示UNKNOWN。

概述起来,all表达式相关信息如下。

条件

结果

Op

单表达式比较

可取=, !=,^=,<>, >, <, <=, >=

一维表达式比较

可取=, !=,^=,<>

多维表达式比较

右表达式形式

列表,子查询

左表达式与右表达式基本元素

成员数目必须相等,对应成员可比较;比如(1,2)= all((1,0),(1,1))

右列表

右列表为空列表

文档未提及 (实测语法不支持,见4.1.3.11测试用例11)

左表达式与右列表某一元素比较结果为FALSE

all表达式结果 FALSE

左表达式与右列表所有元素比较结果为TRUE

all表达式结果 TRUE

其他情况

all表达式结果 null

右子查询

右子查询返回空行

all表达式结果 TRUE

左表达式与右列表某一元素比较结果为FALSE

all表达式结果 FALSE

左表达式与右列表所有元素比较结果为TRUE

all表达式结果 TRUE

其他情况

all表达式结果 null

(2)MySQL all表达式描述

MySQL8.0对all条件表达式描述如下:

Syntax:

operand comparison_operator ALL (subquery)

The word ALL, which must follow a comparison operator, means “return TRUE if the comparison is TRUE for ALL of the values in the column that the subquery returns.” For example:

SELECT s1 FROM t1 WHERE s1 > ALL (SELECT s1 FROM t2);

Suppose that there is a row in table t1 containing (10). The expression is TRUE if table t2 contains

(-5,0,+5) because 10 is greater than all three values in t2. The expression is FALSE if table t2

contains (12,6,NULL,-100) because there is a single value 12 in table t2 that is greater than 10. The

expression is unknown (that is, NULL) if table t2 contains (0,NULL,1).

Finally, the expression is TRUE if table t2 is empty. So, the following expression is TRUE when table t2 is empty:

SELECT * FROM t1 WHERE 1 > ALL (SELECT s1 FROM t2);

In addition, the following expression is NULL when table t2 is empty:

SELECT * FROM t1 WHERE 1 > ALL (SELECT MAX(s1) FROM t2);

In general, tables containing NULL values and empty tables are “edge cases.” When writing subqueries,

always consider whether you have taken those two possibilities into account.

概述起来,all表达式相关信息如下。

条件

结果

Op

单表达式比较

可取=, !=,<>, >, <, <=, >=

一维表达式比较

未提及,待测

右表达式形式

子查询

左表达式与右表达式基本元素

成员数目必须相等,对应成员可比较;

右子查询

右子查询返回空行

all表达式结果 TRUE

左表达式与右列表某一元素比较结果为FALSE

all表达式结果 FALSE

左表达式与右列表所有元素比较结果为TRUE

all表达式结果 TRUE

其他情况

all表达式结果 null

(3)KingbaseES all表达式描述

KingbaseES 支持两种all表达式(右侧表达式支持数组表达式和子查询):

expression operator ALL (array_expression)

expression operator ALL (subquery)

KingbaseES 中all表达式总结如下,

条件

结果

Op

单表达式比较

可取=, !=,^=,<>, >, <, <=, >=

一维表达式比较

可取=, !=,^=,<>, >, <, <=, >=

右表达式形式

数组,子查询

左表达式与右表达式基本元素

成员数目必须相等,对应成员可比较;

右数组

右数组为空数组

all表达式结果 TRUE

左表达式与右数组某一元素比较结果为FALSE

all表达式结果 FALSE

左表达式与右数组所有元素比较结果为TRUE

all表达式结果 TRUE

其他情况

all表达式结果 null

右子查询

右子查询返回空行

all表达式结果 TRUE

左表达式与右列表某一元素比较结果为FALSE

all表达式结果 FALSE

左表达式与右列表所有元素比较结果为TRUE

all表达式结果 TRUE

其他情况

all表达式结果 null

总的来说,KingbaseES中all表达式的能力要强于Oracle中all表达式,KingbaseES中all表达式不支持表达式列表。KingbaseES中all表达式的能力也强于MySQL中all表达式。

  • 16
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值