环境:
MySQL支持多种类型的运算符,可以用来连接表达式的项。运算符的类型主要包括 算术运算符、比较运算符、逻辑运算符 和 位运算符。
算术运算符包括 加、减、乘、除 和 模 运算符。
运算符 | 作用 |
---|---|
+ | 加法 |
- | 减法 |
* | 乘法 |
/,DIV | 除法,返回商 |
%,MOD | 除法,返回余数 |
这几种运算符的简单使用如下:
mysql> select 0.1+0.333, 0.1-0.333, 0.1*0.333, 1/2, 1%2;
+-----------+-----------+-----------+--------+------+
| 0.1+0.333 | 0.1-0.333 | 0.1*0.333 | 1/2 | 1%2 |
+-----------+-----------+-----------+--------+------+
| 0.433 | -0.233 | 0.0333 | 0.5000 | 1 |
+-----------+-----------+-----------+--------+------+
1 row in set (0.01 sec)
在 除法 和 模 运算中,如果除数是0,将是非法除数,返回结果为null,如下:
// 除数为0是可以的
mysql> select 0/2,0%2;
+--------+------+
| 0/2 | 0%2 |
+--------+------+
| 0.0000 | 0 |
+--------+------+
1 row in set (0.00 sec)
// 被除数如果为0,非法,返回null
mysql> select 2/0, 2%0;
+------+------+
| 2/0 | 2%0 |
+------+------+
| NULL | NULL |
+------+------+
1 row in set, 2 warnings (0.00 sec)
除法 和 模 运算符中,分别还有 DIV 和 MOD 的使用,说明如下:
// 这里,10除以3的结果是3.3333...,但由于DIV是整数除法,所以它只返回整数部分,即3。
mysql> select 10/3, 10 DIV 3, 10 div 3;
+--------+----------+----------+
| 10/3 | 10 DIV 3 | 10 div 3 |
+--------+----------+----------+
| 3.3333 | 3 | 3 |
+--------+----------+----------+
1 row in set (0.00 sec)
mysql> select 10%3, MOD(10,3), mod(10,3), 10 MOD 3, 10 mod 3;
+------+-----------+-----------+----------+----------+
| 10%3 | MOD(10,3) | mod(10,3) | 10 MOD 3 | 10 mod 3 |
+------+-----------+-----------+----------+----------+
| 1 | 1 | 1 | 1 | 1 |
+------+-----------+-----------+----------+----------+
1 row in set (0.00 sec)
当时用SELECT查询的时候,允许对表达式左侧和右侧的操作数进行比较,比较结果为真,则返回1,为假则返回0,比较结果不确定则返回NULL。
运算符 | 作用 |
---|---|
= | 等于 |
<>或!= | 不等于 |
<=> | NULL安全的等于(NULL-safe) |
< | 小于 |
<= | 小于等于 |
> | 大于 |
>= | 大于等于 |
BETWEEN | 存在于指定范围 |
IN | 存在于指定集合 |
IS NULL | 为NULL |
IS NOT NULL | 不为NULL |
LIKE | 通配符匹配 |
REGEXP 或 RLIKE | 正则表达式 |
比较运算符可以比较 数字、字符串 和 表达式。数字作为浮点数比较,字符串以不区分大小写的方式进行比较。
mysql> select 1=0, 1=1, NULL=NULL, null=null, null=NULL;
+-----+-----+-----------+-----------+-----------+
| 1=0 | 1=1 | NULL=NULL | null=null | null=NULL |
+-----+-----+-----------+-----------+-----------+
| 0 | 1 | NULL | NULL | NULL |
+-----+-----+-----------+-----------+-----------+
1 row in set (0.00 sec)
mysql> select 1!=0, 1<>0, 1<>1, null<>null;
+------+------+------+------------+
| 1!=0 | 1<>0 | 1<>1 | null<>null |
+------+------+------+------------+
| 1 | 1 | 0 | NULL |
+------+------+------+------------+
1 row in set (0.00 sec)
mysql> select 1=0, 1=1, null=null, null<=>null, null<=>NULL;
+-----+-----+-----------+-------------+-------------+
| 1=0 | 1=1 | null=null | null<=>null | null<=>NULL |
+-----+-----+-----------+-------------+-------------+
| 0 | 1 | NULL | 1 | 1 |
+-----+-----+-----------+-------------+-------------+
1 row in set (0.00 sec)
mysql> select 1<2, 'a'<'b', 'bdf'<='b', 'b'<='b', 'a'>'b', 'abc'>'a', 1>0, 'a'>='b','abc'>='a', 1>=0, 1>=1;
+-----+---------+------------+----------+---------+-----------+-----+----------+------------+------+------+
| 1<2 | 'a'<'b' | 'bdf'<='b' | 'b'<='b' | 'a'>'b' | 'abc'>'a' | 1>0 | 'a'>='b' | 'abc'>='a' | 1>=0 | 1>=1 |
+-----+---------+------------+----------+---------+-----------+-----+----------+------------+------+------+
| 1 | 1 | 0 | 1 | 0 | 1 | 1 | 0 | 1 | 1 | 1 |
+-----+---------+------------+----------+---------+-----------+-----+----------+------------+------+------+
1 row in set (0.00 sec)
mysql> select 10 between 10 and 20, 9 between 10 and 20;
+----------------------+---------------------+
| 10 between 10 and 20 | 9 between 10 and 20 |
+----------------------+---------------------+
| 1 | 0 |
+----------------------+---------------------+
1 row in set (0.05 sec)
mysql> select 1 in (1,2,3), 't' in ('t','a','b'), 0 in (1,2);
+--------------+----------------------+------------+
| 1 in (1,2,3) | 't' in ('t','a','b') | 0 in (1,2) |
+--------------+----------------------+------------+
| 1 | 1 | 0 |
+--------------+----------------------+------------+
1 row in set (0.00 sec)
mysql> select 0 is null, null is null;
+-----------+--------------+
| 0 is null | null is null |
+-----------+--------------+
| 0 | 1 |
+-----------+--------------+
1 row in set (0.00 sec)
mysql> select 0 is not null, null is not null;
+---------------+------------------+
| 0 is not null | null is not null |
+---------------+------------------+
| 1 | 0 |
+---------------+------------------+
1 row in set (0.00 sec)
mysql> select 123321 like '%123%', 123456 like '%123%', 123456 like '%321%';
+---------------------+---------------------+---------------------+
| 123321 like '%123%' | 123456 like '%123%' | 123456 like '%321%' |
+---------------------+---------------------+---------------------+
| 1 | 1 | 0 |
+---------------------+---------------------+---------------------+
1 row in set (0.00 sec)
mysql> select 'abcded' regexp 'cd', 'abcded' rlike 'cd', 'abc' regexp 'cd';
+----------------------+---------------------+-------------------+
| 'abcded' regexp 'cd' | 'abcded' rlike 'cd' | 'abc' regexp 'cd' |
+----------------------+---------------------+-------------------+
| 1 | 1 | 0 |
+----------------------+---------------------+-------------------+
1 row in set (0.00 sec)
又称为布尔运算符,确认表达式的真假。
运算符 | 作用 |
---|---|
NOT 或 ! | 逻辑非 |
AND 或 && | 逻辑与 |
OR 或 || | 逻辑或 |
XOR | 逻辑异或 |
mysql> select not 1, not 0, !1, !0, not null;
+-------+-------+----+----+----------+
| not 1 | not 0 | !1 | !0 | not null |
+-------+-------+----+----+----------+
| 0 | 1 | 0 | 1 | NULL |
+-------+-------+----+----+----------+
1 row in set, 2 warnings (0.01 sec)
mysql> select 1 and 1, 1 && 0, 1 and null;
+---------+--------+------------+
| 1 and 1 | 1 && 0 | 1 and null |
+---------+--------+------------+
| 1 | 0 | NULL |
+---------+--------+------------+
1 row in set, 1 warning (0.01 sec)
mysql> select 1 or 0, 0 || 0, 0 or null, 1 or null, null or null;
+--------+--------+-----------+-----------+--------------+
| 1 or 0 | 0 || 0 | 0 or null | 1 or null | null or null |
+--------+--------+-----------+-----------+--------------+
| 1 | 0 | NULL | 1 | NULL |
+--------+--------+-----------+-----------+--------------+
1 row in set, 1 warning (0.00 sec)
mysql> select 1 xor 1, 0 xor 0, 1 xor 0, null xor 0;
+---------+---------+---------+------------+
| 1 xor 1 | 0 xor 0 | 1 xor 0 | null xor 0 |
+---------+---------+---------+------------+
| 0 | 0 | 1 | NULL |
+---------+---------+---------+------------+
1 row in set (0.01 sec)
将给定的操作数转化为二进制后,对各个操作数每一位进行指定的逻辑运算,得到的二进制结果转换为十进制后就是位运算的结果。
运算符 | 作用 |
---|---|
& | 位与(位AND) |
| | 位或(位OR) |
^ | 位异(位XOR) |
~ | 位取反 |
>> | 位右移 |
<< | 位左移 |
位运算符这里不多做讲解说明。
以上这么多运算符,很可能混合使用,那么它们的优先级是什么呢?如下:
优先级高到低 | 运算符 |
---|---|
1 | := |
2 | ||、OR、XOR |
3 | &&、AND |
4 | NOT |
5 | BETWEEN、CASE、WHEN、THEN、ELSE |
6 | =、<=>、>=、>、<、<=、<>、!=、IS、LIKE、REGEXT、IN |
7 | | |
8 | & |
9 | <<、>> |
10 | -、+ |
11 | *、/、DIV、%、MOD |
12 | ^ |
13 | - 和 ~ |
14 | ! |
实际上,没人记得住这些,实际使用我们都是用 ()将需要有限的操作括起来,这样可以起到优先的作用,又方便理解。