更新时间:2024-06-29 GMT+08:00
分享

数字操作符

+

描述:加

示例:

1
2
3
4
5
SELECT 2+3 AS RESULT;
 result 
--------
      5
(1 row)

-

描述:减

示例:

1
2
3
4
5
SELECT 2-3 AS RESULT;
 result 
--------
     -1
(1 row)

*

描述:乘

示例:

1
2
3
4
5
SELECT 2*3 AS RESULT;
 result 
--------
      6
(1 row)

/

描述:除(除法操作符不会取整)

示例:

1
2
3
4
5
SELECT 4/2 AS RESULT;
 result 
--------
      2
(1 row)
1
2
3
4
5
SELECT 4/3 AS RESULT;
      result      
------------------
 1.33333333333333
(1 row)

+/-

描述:正/负

示例:

1
2
3
4
5
SELECT -2 AS RESULT;
 result 
--------
     -2
(1 row)

%

描述:模(求余)

示例:

1
2
3
4
5
SELECT 5%4 AS RESULT;
 result 
--------
      1
(1 row)

@

描述:绝对值

示例:

1
2
3
4
5
SELECT @ -5.0 AS RESULT;
 result 
--------
    5.0
(1 row)

^

描述:幂(指数运算)

MySQL兼容模式下,作用为异或,参见位串函数和操作符章节的操作符“#”。

示例:

1
2
3
4
5
SELECT 2.0^3.0 AS RESULT;
       result       
--------------------
 8.0000000000000000
(1 row)

|/

描述:平方根

示例:

1
2
3
4
5
SELECT |/ 25.0 AS RESULT;
 result 
--------
      5
(1 row)

||/

描述:立方根

示例:

1
2
3
4
5
SELECT ||/ 27.0 AS RESULT;
 result 
--------
      3
(1 row)

!

描述:阶乘

示例:

1
2
3
4
5
SELECT 5! AS RESULT;
 result 
--------
    120
(1 row)

!!

描述:阶乘(前缀操作符)

示例:

1
2
3
4
5
SELECT !!5 AS RESULT;
 result 
--------
    120
(1 row)

&

描述:二进制AND

示例:

1
2
3
4
5
SELECT 91&15  AS RESULT;
 result 
--------
     11
(1 row)

|

描述:二进制OR

示例:

1
2
3
4
5
SELECT 32|3  AS RESULT;
 result 
--------
     35
(1 row)

#

描述:二进制XOR

示例:

1
2
3
4
5
SELECT 17#5  AS RESULT;
 result 
--------
     20
(1 row)

~

描述:二进制NOT

示例:

1
2
3
4
5
SELECT ~1 AS RESULT;
 result 
--------
     -2
(1 row)

<<

描述:二进制左移

示例:

1
2
3
4
5
SELECT 1<<4 AS RESULT;
 result 
--------
     16
(1 row)

>>

描述:二进制右移

示例:

1
2
3
4
5
SELECT 8>>2 AS RESULT;
 result 
--------
      2
(1 row)

相关文档