1、函数操作符
boolean AND boolean → boolean
boolean OR boolean → boolean
NOT boolean → boolean
SQL使用三值的逻辑系统,包括真、假和null,null表示“未知”。
观察下面的真值表:
a | b | a AND b | a OR b |
---|---|---|---|
TRUE | TRUE | TRUE | TRUE |
TRUE | FALSE | FALSE | TRUE |
TRUE | NULL | NULL | TRUE |
FALSE | FALSE | FALSE | FALSE |
FALSE | NULL | FALSE | NULL |
NULL | NULL | NULL | NULL |
a | NOT a |
---|---|
TRUE | FALSE |
FALSE | TRUE |
NULL | NULL |
2、比较函数和操作符
比较操作符
操作符 | 描述 |
---|---|
datatype < datatype → boolean | 小于 |
datatype > datatype → boolean | 大于 |
datatype <= datatype → boolean | 小于等于 |
datatype >= datatype → boolean | 大于等于 |
datatype = datatype → boolean | 等于 |
datatype <> datatype → boolean | 不等于 |
datatype != datatype → boolean | 不等于 |
所有比较操作符都是二元操作符,返回boolean类型的值。
因此,类似1 < 2 < 3的表达式是无效的。
因为没有<操作符与 3 进行Boolean值比较)。
使用BETWEEN谓词执行范围测试。
比较谓词
谓词
描述
示例(s)
datatype BETWEEN datatype AND datatype → boolean
之间(包括范围端点)。
2 BETWEEN 1 AND 3 → t
2 BETWEEN 3 AND 1 → f
datatype NOT BETWEEN datatype AND datatype → boolean
不在之间 (BETWEEN的否定).
2 NOT BETWEEN 1 AND 3 → f
datatype BETWEEN SYMMETRIC datatype AND datatype → boolean
之间, 在对两个端点值排序之后。
2 BETWEEN SYMMETRIC 3 AND 1 → t
datatype NOT BETWEEN SYMMETRIC datatype AND datatype → boolean
不在之间, 在对两个端点值排序之后。
2 NOT BETWEEN SYMMETRIC 3 AND 1 → f
datatype IS DISTINCT FROM datatype → boolean
不相等, 将空(null)视为可比值。
1 IS DISTINCT FROM NULL → t (而不是 NULL)
NULL IS DISTINCT FROM NULL → f (而不是 NULL)
datatype IS NOT DISTINCT FROM datatype → boolean
相等, 将空(null)视为可比值。
1 IS NOT DISTINCT FROM NULL → f (而不是 NULL)
NULL IS NOT DISTINCT FROM NULL → t (而不是 NULL)
datatype IS NULL → boolean
测试值是否为空。
1.5 IS NULL → f
datatype IS NOT NULL → boolean
测试值是否不为空。
‘null’ IS NOT NULL → t
datatype ISNULL → boolean
测试值是否为空(非标准语法)。
datatype NOTNULL → boolean
测试值是否不为空(非标准语法)。
boolean IS TRUE → boolean
测试布尔表达式是否为真。
true IS TRUE → t
NULL::boolean IS TRUE → f (而不是 NULL)
boolean IS NOT TRUE → boolean
测试布尔表达式是否为假或未知。
true IS NOT TRUE → f
NULL::boolean IS NOT TRUE → t (而不是 NULL)
boolean IS FALSE → boolean
测试布尔表达式是否为假。
true IS FALSE → f
NULL::boolean IS FALSE → f (而不是 NULL)
boolean IS NOT FALSE → boolean
测试布尔表达式是否为真或未知。
true IS NOT FALSE → t
NULL::boolean IS NOT FALSE → t (而不是 NULL)
boolean IS UNKNOWN → boolean
测试布尔表达式是否为未知。
true IS UNKNOWN → f
NULL::boolean IS UNKNOWN → t (而不是 NULL)
boolean IS NOT UNKNOWN → boolean
测试布尔表达式是否为真或假。
true IS NOT UNKNOWN → t
NULL::boolean IS NOT UNKNOWN → f (而不是 NULL)
比较函数
函数
描述
例子
num_nonnulls ( VARIADIC “any” ) → integer
返回非空参数的数量。
num_nonnulls(1, NULL, 2) → 2
num_nulls ( VARIADIC “any” ) → integer
返回空参数的数量。
num_nulls(1, NULL, 2) → 1
3、数学和操作符
数学操作符
操作符
描述
例子
numeric_type + numeric_type → numeric_type
加
2 + 3 → 5
- numeric_type → numeric_type
一元加(无操作) - 3.5 → 3.5
numeric_type - numeric_type → numeric_type
减
2 - 3 → -1
- numeric_type → numeric_type
否定 - (-4) → 4
numeric_type * numeric_type → numeric_type
乘
2 * 3 → 6
numeric_type / numeric_type → numeric_type
除(对于整型,除法将结果截断为零)
5.0 / 2 → 2.5000000000000000
5 / 2 → 2
(-5) / 2 → -2
numeric_type % numeric_type → numeric_type
模(取余); 适用于 smallint,integer,bigint 和 numeric
5 % 4 → 1
numeric ^ numeric → numeric
double precision ^ double precision → double precision
指数 (不像典型的数学实践, 多次使用 ^ 将会从左到有关联)
2 ^ 3 → 8
2 ^ 3 ^ 3 → 512
|/ double precision → double precision
平方根
|/ 25.0 → 5
||/ double precision → double precision
立方根
||/ 64.0 → 4
@ numeric_type → numeric_type
绝对值
@ -5.0 → 5
integral_type & integral_type → integral_type
按位与(AND)
91 & 15 → 11
integral_type | integral_type → integral_type
按位或(OR)
32 | 3 → 35
integral_type # integral_type → integral_type
按位异或(exclusive OR)
17 # 5 → 20
~ integral_type → integral_type
按位求反(NOT)
~1 → -2
integral_type << integer → integral_type
按位左移
1 << 4 → 16
integral_type >> integer → integral_type
按位右移
8 >> 2 → 2
数学函数
函数
描述
例子
abs ( numeric_type ) → numeric_type
绝对值
abs(-17.4) → 17.4
cbrt ( double precision ) → double precision
立方根
cbrt(64.0) → 4
ceil ( numeric ) → numeric
ceil ( double precision ) → double precision
大于或等于参数的最接近的整数
ceil(42.2) → 43
ceil(-42.8) → -42
ceiling ( numeric ) → numeric
ceiling ( double precision ) → double precision
大于或等于参数的最接近的整数 (与 ceil 相同)
ceiling(95.3) → 96
degrees ( double precision ) → double precision
将弧度转换为角度
degrees(0.5) → 28.64788975654116
div ( y numeric, x numeric ) → numeric
y/x 的整数商(截断为零位)
div(9,4) → 2
exp ( numeric ) → numeric
exp ( double precision ) → double precision
指数 (e 的给定次方)
exp(1.0) → 2.7182818284590452
factorial ( bigint ) → numeric
阶乘
factorial(5) → 120
floor ( numeric ) → numeric
floor ( double precision ) → double precision
小于或等于参数的最接近整数
floor(42.8) → 42
floor(-42.8) → -43
gcd ( numeric_type, numeric_type ) → numeric_type
最大公约数 (能将两个输入数整除而无余数的最大正数); 如果两个输入为零则返回 0 ; 适用于 integer, bigint,和 numeric
gcd(1071, 462) → 21
lcm ( numeric_type, numeric_type ) → numeric_type
最小公倍数(两个输入的整数倍的最小的严格正数);如果任意一个输入值为零则返回0;适用于integer,bigint,和 numeric
lcm(1071, 462) → 23562
ln ( numeric ) → numeric
ln ( double precision ) → double precision
自然对数
ln(2.0) → 0.6931471805599453
log ( numeric ) → numeric
log ( double precision ) → double precision
以10为底的对数
log(100) → 2
log10 ( numeric ) → numeric
log10 ( double precision ) → double precision
以10为底的对数 (与 log 相同)
log10(1000) → 3
log ( b numeric, x numeric ) → numeric
以 b 为底的 x的对数
log(2.0, 64.0) → 6.0000000000
min_scale ( numeric ) → integer
精确表示所提供值所需的最小刻度(小数位数)
min_scale(8.4100) → 2
mod ( y numeric_type, x numeric_type ) → numeric_type
y/x的余数; 适用于smallint、integer、bigint、和 numeric
mod(9,4) → 1
pi ( ) → double precision
π的近似值
pi() → 3.141592653589793
power ( a numeric, b numeric ) → numeric
power ( a double precision, b double precision ) → double precision
a的b次幂
power(9, 3) → 729
radians ( double precision ) → double precision
将角度转换为弧度
radians(45.0) → 0.7853981633974483
round ( numeric ) → numeric
round ( double precision ) → double precision
四舍五入到最近的整数
round(42.4) → 42
round ( v numeric, s integer ) → numeric
把 v 四舍五入到 s 位小数
round(42.4382, 2) → 42.44
scale ( numeric ) → integer
参数的刻度(小数点后的位数)
scale(8.4100) → 4
sign ( numeric ) → numeric
sign ( double precision ) → double precision
参数的符号 (-1, 0, 或 +1)
sign(-8.4) → -1
sqrt ( numeric ) → numeric
sqrt ( double precision ) → double precision
平方根
sqrt(2) → 1.4142135623730951
trim_scale ( numeric ) → numeric
通过删除尾数部分的零来降低值的刻度(小数位数)
trim_scale(8.4100) → 8.41
trunc ( numeric ) → numeric
trunc ( double precision ) → double precision
截断整数 (向零靠近)
trunc(42.8) → 42
trunc(-42.8) → -42
trunc ( v numeric, s integer ) → numeric
截断 v 到 s 位小数位置的数字
trunc(42.4382, 2) → 42.43
width_bucket ( operand numeric, low numeric, high numeric, count integer ) → integer
width_bucket ( operand double precision, low double precision, high double precision, count integer ) → integer
返回包含count等宽柱的柱状图中operand所在的柱的编号,范围从low到high。 超出该范围的输入则返回0或计数+1。
width_bucket(5.35, 0.024, 10.06, 5) → 3
width_bucket ( operand anyelement, thresholds anyarray ) → integer
返回一个柱号,这个柱是在给定数组中operand将被分配的柱。 对于一个低于第一个下界的输入返回0。 operand和数组元素可以是具有标准比较操作符的任何类型。 thresholds数组必须被排好序,最小的排在最前面,否则将会得到意想不到的结果。
width_bucket(now(), array[‘yesterday’, ‘today’, ‘tomorrow’]::timestamptz[]) → 2
随机函数
函数
描述
例子
random ( ) → double precision
返回一个范围 0.0 <= x < 1.0 中的随机值
random() → 0.897124072839091
setseed ( double precision ) → void
为后续的random()调用设置种子;参数必须在-1.0和1.0之间,包括边界值
setseed(0.12345)
三角函数
函数
描述
例子
acos ( double precision ) → double precision
反余弦,结果为弧度
acos(1) → 0
acosd ( double precision ) → double precision
反余弦,结果为度数
acosd(0.5) → 60
asin ( double precision ) → double precision
反正弦,结果为弧度
asin(1) → 1.5707963267948966
asind ( double precision ) → double precision
反正弦,结果为度数
asind(0.5) → 30
atan ( double precision ) → double precision
反正切,结果为弧度
atan(1) → 0.7853981633974483
atand ( double precision ) → double precision
反正切,结果为度数
atand(1) → 45
atan2 ( y double precision, x double precision ) → double precision
y/x的反正切,结果为弧度
atan2(1,0) → 1.5707963267948966
atan2d ( y double precision, x double precision ) → double precision
y/x的反正切,结果为度数
atan2d(1,0) → 90
cos ( double precision ) → double precision
余弦,参数为弧度
cos(0) → 1
cosd ( double precision ) → double precision
余弦,参数为度数
cosd(60) → 0.5
cot ( double precision ) → double precision
余切,参数为弧度
cot(0.5) → 1.830487721712452
cotd ( double precision ) → double precision
余切,参数为度数
cotd(45) → 1
sin ( double precision ) → double precision
正弦,参数为弧度
sin(1) → 0.8414709848078965
sind ( double precision ) → double precision
正弦,参数为度数
sind(30) → 0.5
tan ( double precision ) → double precision
正切,参数为弧度
tan(1) → 1.5574077246549023
tand ( double precision ) → double precision
正切,参数为度数
tand(45) → 1
__ 双曲函数__
函数
描述
例子
sinh ( double precision ) → double precision
双曲正弦
sinh(1) → 1.1752011936438014
cosh ( double precision ) → double precision
双曲余弦
cosh(0) → 1
tanh ( double precision ) → double precision
双曲切线
tanh(1) → 0.7615941559557649
asinh ( double precision ) → double precision
反双曲正弦
asinh(1) → 0.881373587019543
acosh ( double precision ) → double precision
反双曲余弦
acosh(1) → 0
atanh ( double precision ) → double precision
反双曲切线
atanh(0.5) → 0.5493061443340548