[GBase 8s 教程]GBase 8s 运算符/函数

运算符是一种告诉编译器执行特定的数学或逻辑操作的符号。
GBase 8s 运算符是一个保留关键字或字符,一般用在 WHERE 语句中,作为过滤条件。

常见的运算符/函数有:

  • 算术运算符/函数
  • 比较运算符
  • 逻辑运算符
  • 按位运算函数

算术运算符

假设变量 A 为 2,变量 B 为 3,则:

运算符、函数描述实例
+A + B 结果为 5
-A - B 结果为 -1
*A * B 结果为 6
/B / A 结果为 1.5
mod模(取余)mod(B,A) 结果为 1
pow指数pow(A,B) 结果为 8
rootroot(25,2) 结果为 5

实例:

> select 2 + 3 from dual;

(constant)

         5

1 row(s) retrieved.

> select 2 - 3 from dual;

(constant)

        -1

1 row(s) retrieved.

> select 2 * 3  from dual;

(constant)

         6

1 row(s) retrieved.

> select 3 / 2 from dual;

      (constant)

1.50000000000000

1 row(s) retrieved.

> select mod(3,2) from dual;

 (constant)

          1

1 row(s) retrieved.

> select pow(2,3) from dual;

    (constant)

8.000000000000

1 row(s) retrieved.

> select root(25,2) from dual;

    (constant)

5.000000000000

1 row(s) retrieved.

比较运算符

假设变量 A 为 10,变量 B 为 20,则:

运算符描述实例
=等于(A = B) 为 false
!=不等于(A != B) 为 true
<>不等于(A <> B) 为 true
>大于(A > B) 为 false
<小于(A < B) 为 true
>=大于等于(A >= B) 为 false
<=小于等于(A <= B) 为 true

实例:
创建 COMPANY 表,及导入测试数据

DROP TABLE IF EXISTS COMPANY;
CREATE TABLE COMPANY(
  ID         SERIAL         PRIMARY KEY,
  NAME       VARCHAR(40)    NOT NULL,
  AGE        INT            NOT NULL,
  ADDRESS    CHAR(50),
  SALARY     DECIMAL(10,2),
  JOIN_DATE  DATE           DEFAULT TODAY
);
INSERT INTO COMPANY VALUES (0, '李雷', 37, '北京', 20000.00,'2005-05-13');
INSERT INTO COMPANY VALUES (0, '韩梅梅', 35, '天津', 16000.00, '2007-12-18');
INSERT INTO COMPANY VALUES (0, '林涛', 36, '上海', 25000.00, '2006-01-04');
INSERT INTO COMPANY VALUES (0, '魏华', 36, '西安', 15000.00, '2007-08-30');
INSERT INTO COMPANY VALUES (0, '露茜', 34, '伦敦', 22000.00, '2008-08-08');
INSERT INTO COMPANY VALUES (0, '莉莉', 34, '伦敦', 22000.00, '2008-08-08');
INSERT INTO COMPANY VALUES (0, '吉姆', 35, '华盛顿', 16000.00, '2010-12-13');
INSERT INTO COMPANY VALUES (0, '汤姆', 36, '渥太华', 21000.00, '2010-04-30');

数据内容如下:

> select name,salary from company;

name                                           salary

李雷                                         20000.00
韩梅梅                                       16000.00
林涛                                         25000.00
魏华                                         15000.00
露茜                                         22000.00
莉莉                                         22000.00
吉姆                                         16000.00
汤姆                                         21000.00

8 row(s) retrieved.

读取SALARY字段 等于 20000的数据

> select name,salary from company where salary = 20000;

name                                           salary

李雷                                         20000.00

1 row(s) retrieved.

读取SALARY字段 不等于 20000的数据

> select name,salary from company where salary != 20000;

name                                           salary

韩梅梅                                       16000.00
林涛                                         25000.00
魏华                                         15000.00
露茜                                         22000.00
莉莉                                         22000.00
吉姆                                         16000.00
汤姆                                         21000.00

7 row(s) retrieved.

-- 或者使用 <>
> select name,salary from company where salary <> 20000;

name                                           salary

韩梅梅                                       16000.00
林涛                                         25000.00
魏华                                         15000.00
露茜                                         22000.00
莉莉                                         22000.00
吉姆                                         16000.00
汤姆                                         21000.00

7 row(s) retrieved.

读取SALARY字段 大于 20000的数据

> select name,salary from company where salary > 20000;

name                                           salary

林涛                                         25000.00
露茜                                         22000.00
莉莉                                         22000.00
汤姆                                         21000.00

4 row(s) retrieved.

读取SALARY字段 小于 20000的数据

> select name,salary from company where salary < 20000;

name                                           salary

韩梅梅                                       16000.00
魏华                                         15000.00
吉姆                                         16000.00

3 row(s) retrieved.

读取SALARY字段 大于等于 20000的数据

> select name,salary from company where salary >= 20000;

name                                           salary

李雷                                         20000.00
林涛                                         25000.00
露茜                                         22000.00
莉莉                                         22000.00
汤姆                                         21000.00

5 row(s) retrieved.

读取SALARY字段 小于等于 20000的数据

> select name,salary from company where salary <= 20000;

name                                           salary

李雷                                         20000.00
韩梅梅                                       16000.00
魏华                                         15000.00
吉姆                                         16000.00

4 row(s) retrieved.

逻辑运算符

逻辑运算符有以下几种:

序号运算符描述
1AND逻辑与运算符。如果两个操作数都非零,则条件为真。
2NOT逻辑非运算符。用来逆转操作数的逻辑状态。如果条件为真则逻辑非运算符将使其为假。
3OR逻辑或运算符。如果两个操作数中有任意一个非零,则条件为真。

SQL 使用三值的逻辑系统,包括 true(真)、false(假) 和 null(未知)。
and 和 or 运算符

ABA and BA or B
未知未知
未知未知
未知未知未知未知

not 运算符

Anot A
未知未知

实例:
读取的数据内容如下:

> select name,age,salary from company;

name                                             age       salary

李雷                                              37     20000.00
韩梅梅                                            35     16000.00
林涛                                              36     25000.00
魏华                                              36     15000.00
露茜                                              34     22000.00
莉莉                                              34     22000.00
吉姆                                              35     16000.00
汤姆                                              36     21000.00

8 row(s) retrieved.

读取 AGE 字段大于等于 36 且 SALARY 字段大于 20000 的数据:

> select name,age,salary from company where age >= 36 and salary >= 20000;

name                                             age       salary

李雷                                              37     20000.00
林涛                                              36     25000.00
汤姆                                              36     21000.00

3 row(s) retrieved.

读取 AGE 字段大于等于 36 或者 SALARY 字段大于 20000 的数据:

> select name,age,salary from company where age >= 36 or salary >= 20000;

name                                             age       salary

李雷                                              37     20000.00
林涛                                              36     25000.00
魏华                                              36     15000.00
露茜                                              34     22000.00
莉莉                                              34     22000.00
汤姆                                              36     21000.00

6 row(s) retrieved.

读取 SALARY 字段不为 NULL 的数据:

> select name,age,salary from company where salary is not null;

name                                             age       salary

李雷                                              37     20000.00
韩梅梅                                            35     16000.00
林涛                                              36     25000.00
魏华                                              36     15000.00
露茜                                              34     22000.00
莉莉                                              34     22000.00
吉姆                                              35     16000.00
汤姆                                              36     21000.00

8 row(s) retrieved.

位运算函数

位运算函数作用于位,并逐位执行操作。真值表如下所示:

AB
0000
0101
1111
1001

假设如果 A = 60,且 B = 13,现在以二进制格式表示,它们如下所示:
A = 0011 1100
B = 0000 1101

下表显示了 GBase 8s 支持的位运算函数。假设变量 A 的值为 60,变量 B 的值为 13,则:

运算函数描述实例
bitand(A,B)按位与操作,按二进制位进行"与"运算bitand(A,B) 将得到 12,即为 0000 1100
bitor(A,B)按位或运算符,按二进制位进行"或"运算bitor(A,B) 将得到 61,即为 0011 1101
bitxor(A,B)异或运算符,按二进制位进行"异或"运算bitxor(A,B) 将得到 49,即为 0011 0001
bitnot(A)取反运算符,按二进制位进行"取反"运算bitnot(A) 将得到 -61,即为 1100 0011,一个有符号二进制数的补码形式
bitandnot(A,B)结果与BITAND(A,BITNOT(B))相同。按二进制位,B的二进制位为1时,对应的A的二进制位置0bitandnot(A,B) 将得到 48,即为 0011 0000

实例:
位与

> select bitand(60,13) from dual;

 (constant)

         12

1 row(s) retrieved.

位或

> select bitor(60,13) from dual;

 (constant)

         61

1 row(s) retrieved.

位异或

> select bitxor(60,13) from dual;

 (constant)

         49

1 row(s) retrieved.

位取反

> select bitnot(60) from dual;

 (constant)

        -61

1 row(s) retrieved.

位清除

> select bitandnot(60,13) from dual;

 (constant)

         48

1 row(s) retrieved.

-- 或者使用bitand(A,bitnot(B))
> select bitand(60,bitnot(13)) from dual;

 (constant)

         48

1 row(s) retrieved.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值