rocky9-MySQL运算符篇

发布时间:2023年12月31日

一、算数运算符:

1、+????? :

2、-

3、* ???:乘法

4、%?????? :求余

5、/ ???? :除法

mysql> create table cc(num int);

mysql> insert into cc values (64);

mysql> select * from cc;

+------+

| num? |

+------+

|?? 64 |

+------+

1 row in set (0.00 sec)

mysql> select num,num+10,num-10,num*2,num/5,num%3 from cc;

+------+--------+--------+-------+---------+-------+

| num? | num+10 | num-10 | num*2 | num/5?? | num%3 |

+------+--------+--------+-------+---------+-------+

|?? 64 |???? 74 |???? 54 |?? 128 | 12.8000 |???? 1 |

+------+--------+--------+-------+---------+-------+

1 row in set (0.00 sec)

二、比较运算符:

1、???????? = # 等于

2、???????? <=> #安全等于

3、???????? > #大于

4、???????? >= #大于等于

5、???????? <= #小于等于

6、???????? != 或者<>? #不等于

7、???????? is null #判断一个值是否为null

8、???????? is not null #判断一个值是否不为null

9、???????? least # 当有两个或者多个参数时,返回最大值

10、?????? isnull #与 is null 作用一致

11、?????? in # 判断一个值是否在in列表中

12、?????? not in # 判断一个值是否不在in列表中

13、?????? between and # 判断一个值是否在两个值之间

14、?????? like #通配符

15、?????? regexp #正则表达式

1、等于运算符(=):用来判断字符串,数字和表达式是否相等,如果相等返回值为1 否则返回为0 如果值中有null则返回null

mysql> select 1=1,1=0,5=null;

+-----+-----+--------+

| 1=1 | 1=0 | 5=null |

+-----+-----+--------+

|?? 1 |?? 0 |?? NULL |

2、安全等于运算符(< = >)#和等于运算作用基本一致,但是安全运算符可以判断null值

mysql> select 1<=>1,null<=>null,1<=>0;

+-------+-------------+-------+

| 1<=>1 | null<=>null | 1<=>0 |

+-------+-------------+-------+

|???? 1 |?????????? 1 |???? 0 |

+-------+-------------+-------+

3~6、不等于运算符 (!= <>):用于判断数字,字符串,表达式是否不相等,如果不相等返回值为1,否则返回值为0,不能判断空值

mysql> select 1 != 2,4 != 4,9 <> 10,5 <> 5;

+--------+--------+---------+--------+

| 1 != 2 | 4 != 4 | 9 <> 10 | 5 <> 5 |

+--------+--------+---------+--------+

|????? 1 |????? 0 |?????? 1 |????? 0 |

+--------+--------+---------+--------+

7、isnull 和 is null #用于检验一个值是否为null,如果为null返回值为1,否则返回值为0

mysql> select null is null,isnull(null),isnull(5);

+--------------+--------------+-----------+

| null is null ????| isnull(null) ????| isnull(5) |

+--------------+--------------+-----------+

|??????????? 1 |??????????? 1 |???????? 0 |

+--------------+--------------+-----------+

8、is not null #用于检验一个值是否不为null,如果不为null返回值为1,否则返回值为0

mysql> select null is not null;

+------------------+

| null is not null |

+------------------+

|?????????? ??0 |

+------------------+

mysql> select null is not null,'aa' is not null;

+------------------+------------------+

| null is not null???? ?| 'aa' is not null |

+------------------+------------------+

|??????????????? 0 |??????????????? 1 |

9、获取最大值和最小值

least #当有两个或者两个以上参数时,返回最小值,当有一个值为null,则返回值为null,

greatest #当有两个或者两个以上参数数值时,返回最大值

最小值

mysql> select least(2,6,3),least('a','c','e');

+--------------+--------------------+

| least(2,6,3) ????| least('a','c','e') |

+--------------+--------------------+

|??????????? 2 | a????????????????? |

+--------------+--------------------+

最大值

mysql> select greatest(2,6,3),greatest('a','c','e');

+-----------------+-----------------------+

| greatest(2,6,3)??? ?| greatest('a','c','e') |

+-----------------+-----------------------+

|?????????????? 6 | e???????????????????? |

+-----------------+-----------------------+

11~12、in? |? not in

in #判断一个值是否在in列表中,如果值在列表中,返回值为1,否则返回值为0

not in # 判断一个值是否不在in列表中,如果值在列表中。返回值为1,否则返回值为0

mysql> select 5 in (1,2,3,4,5),5 in (1,2,3,4),'a' not in ('a','s','f');

+------------------+----------------+--------------------------+

| 5 in (1,2,3,4,5) ?????| 5 in (1,2,3,4) ????| 'a' not in ('a','s','f') ????????|

+------------------+----------------+--------------------------+

|???????????? ????1 |??????????? ???0 |??????????????????????? 0 ?|

+------------------+----------------+--------------------------+

13、between and #判断一个值是否在两个值之间,如果在两个值之间,返回值为1 否则返回值为0

?mysql> select 5 between 1 and 10,10 between 1 and 5;

+--------------------+--------------------+

| 5 between 1 and 10? ?| 10 between 1 and 5 |

+--------------------+--------------------+

|????????????????? 1 |????????????????? 0 |

+--------------------+--------------------+

14、like #通配符用来匹配字符串,如果匹配则返回值为1,否则返回值为0

like #常用的两中通配符:

% #用于匹配0个或多个任意字符

_ #用于匹配一个任意字符

mysql> select 'hello' like 'he%','hello' like'he_';

+--------------------+-------------------+

| 'hello' like 'he%' ??????| 'hello' like'he_' ????|

+--------------------+-------------------+

|????????????????? 1 ?|?????????????? 0 ??|

+--------------------+-------------------+

1 row in set (0.00 sec)

后面是三下划线

mysql> select 'hello' like 'he%','hello' like'he_','hello' like 'he___';

+--------------------+-------------------+----------------------+

| 'hello' like 'he%' ?????| 'hello' like'he_'?? ????| 'hello' like 'he___'??? ?|

+--------------------+-------------------+----------------------+

|???????????????? ??1 |??????????????? ???0 |??????????????????? 1 |

+--------------------+-------------------+----------------------+

15、regexp #正则匹配,用于匹配字符串,如果匹配则返回值为1, 否则返回值为0

常用的通配符

^ …… #以……开头

……$ #以……结尾

.? #应用匹配一个任意字符

*? #匹配零个或多个前面的字符

[……] #用于匹配在方括号内的任意字符

mysql> select 'hello' regexp '^h','helllo' regexp 'o$','hello' regexp'l*','hello' regexp 'h$';

+---------------------+----------------------+--------------------+---------------------+

| 'hello' regexp '^h' ????| 'helllo' regexp 'o$' ?????| 'hello' regexp'l*' ????| 'hello' regexp 'h$' |

+---------------------+----------------------+--------------------+---------------------+

|?????????????????? 1 ?|??????????????????? 1 ?|????????????????? 1 |?????????????????? 0 |

+---------------------+----------------------+--------------------+---------------------+

三、逻辑运算符:

1、逻辑非not 或者 !

2、逻辑与and 或者 &&

3、逻辑或 or 或者ll

4、逻辑异或 xor

1、逻辑非(not 或者 !)

当操作数为0时,返回值为1

当操作数为非0时,返回值为0

当操作数为null时,返回值为null

mysql> select not 2,not 0,not null;

+-------+-------+----------+

| not 2 ??| not 0 ??| not null |

+-------+-------+----------+

|???? 0 |???? 1 |???? NULL |

+-------+-------+----------+

2、逻辑与and 或者 &&

当所有的操作数都为非0且不为null时,返回值为1

当有一个或多个操作数为0时,返回值为0

其余情况返回值为null

mysql> select 1 and 5,5 and 0,null and 5;

+---------+---------+------------+

| 1 and 5 ??| 5 and 0 ??| null and 5 |

+---------+---------+------------+

|?????? 1 |?????? 0 |?????? NULL |

+---------+---------+------------+

3、逻辑或 or 或者ll

当两个操作数都非null值,且任意一个操作数为非0值时,结果返回值为1,否则返回值为0

当有一个操作数为null时,且另一个操作数为非0时,结果返回值为1,否则返回值为null当两个操作数都为null,结果返回值为null

mysql> select 1 or 5, 1 or 0,null or 5,null or null;

+--------+--------+-----------+--------------+

| 1 or 5 ??| 1 or 0 ???| null or 5 ??| null or null ??|

+--------+--------+-----------+--------------+

|????? 1 |??? ?????1 |???????? 1 |??? NULL

+--------+--------+-----------+--------------+

4、逻辑异或 xor

当任意一个操作数为null时,返回值为null,对于非null的操作数,如果两个操作数都为非0时或者都为0时,返回结果为0 如果一个操作数为0值,另一个操作数为非0时,结果返回值为1

mysql> select null xor null,0 xor 0,5 xor 6,0 xor 7;

+---------------+---------+---------+---------+

| null xor null ????| 0 xor 0 ??| 5 xor 6 ??| 0 xor 7 ?|

+---------------+---------+---------+---------+

|???? NULL ?????|?????? 0? |?????? 0 ?|?????? 1 |

+---------------+---------+---------+---------+

四、位操作运算符:

1、|?? #位或?????? 有1全1

2、&? #位与

3、^? #位异或

4、<< #位左移

5、>> #位右移

6、~? #位取反

1、位或运算符(|) #将对应的二进制位有一个或者两个为1时,则计算结果为1,否则结果为0

mysql> select 10 |15;

+--------+? 将10转换为二进制为:1010

| 10 |15 |?????? 将15转换为二进制位:1111

+--------+??????????????? 有一得一???? 1111

|???? 15 |

+--------+

2、位与运算符(&):对应的二进制位都为1计算结果为1,否则为0

mysql> select 10 & 15, 10 & 18;

+---------+---------+?? 10转换为二进制为:? 1010

| 10 & 15 | 10 & 18 |????????? ? 15转换为二进制位:1111

+---------+---------+?? ? ??????????????????????????????? 1010

|????? 10 |?????? 2 |

+---------+---------+

3、位异或运算符(^) #对应的二进制位不同时,计算机结果为1,否则计算机结果为0

mysql> select 10 ^ 15, 10 ^ 20;

+---------+---------+?? 10转换为二进制为:1010

| 10 ^ 15? ?| 10 ^ 20 |????????????? 15转换为二进制位:1111

+---------+---------+??????????????????????? ????????????? 0101

|?????? 5 |????? 30 |

+---------+---------+

4、位左移(<<):将指定二进制位向左移动指定的位数,左移动指定的位数后,左边高位的数将被移除,空余位置使用0补齐

mysql> select 1 << 2 ,1 << 3;

+--------+--------+????? 00000100

| 1 << 2 ??| 1 << 3 |???????????????? ? 4

+--------+--------+????? 00001000

|????? 4 |????? 8 |??????????? ???? 8

+--------+--------+

5、位右移 (>>) 将二进制位向右移动指定的位数,右移动之后,右边低位的数值将被丢弃,左边高位空出的位置使用0 补齐

mysql> select 16 >> 2, 16 >> 3;

+---------+---------+??? 00010000

| 16 >> 2 ?| 16 >> 3 |????????????? ? 16

+---------+---------+?? ? 00000100

|?????? 4 |?????? 2 |?????????????????????? 4

+---------+---------+

6、位取反(~)将对应的二进制位逐位取反,即1取反后位0,0取反后位1

mysql> select? ~? 1, ~ 2;

+----------------------+----------------------+???????????? 63个0取反(数值很大)

| ~? 1?????????????? ???| ~ 2????????????????? |

+----------------------+----------------------+

| 18446744073709551614 | 18446744073709551613 |

+----------------------+----------------------+

mysql> select ~ 18446744073709551614,~18446744073709551613 ;

+------------------------+-----------------------+???????

| ~ 18446744073709551614 | ~18446744073709551613 |

+------------------------+-----------------------+

|????????????????????? 1 |???????????????????? 2 |

+------------------------+-----------------------+

文章来源:https://blog.csdn.net/bql2022/article/details/135311065
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。