目录
MySQL学习总结之路(第一章:服务与数据库管理)
MySQL学习总结之路(第二章:表)
MySQL学习总结之路(第三章:数据类型)
MySQL学习总结之路(第四章:运算符)
1.1、算数运算符
MySQL支持的算术运算符
运算符 | 作用 |
+ | 加法 |
- | 减法 |
* | 乘法 |
/,DIV | 除法,返回商 |
%,MOD | 除法,返回余数 |
例1:+,-,*,/,%,
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.05 sec)
例2:除法运算中,如果除数为0将是返回NULL
mysql> select 1/0;
+------+
| 1/0 |
+------+
| NULL |
+------+
1 row in set (0.00 sec)
例3:模运算的另外一种形式MOD(a,b) 和a%b相同
mysql> select 1%2,mod(1,2);
+------+----------+
| 1%2 | mod(1,2) |
+------+----------+
| 1 | 1 |
+------+----------+
1 row in set (0.00 sec)
1.2、比较运算符
MySQL支持的比较运算符
运算符 | 作用 |
= | 等于 |
<>或!= | 不等于 |
<=> | NULL 安全的等于(NULL-safe) |
< | 小于 |
<= | 小于等于 |
> | 大于 |
>= | 大于等于 |
BETWEEN | 存在与指定范围 |
IN | 存在于指定集合 |
IS NULL | 为 NULL |
IS NOT NULL | 不为 NULL |
LIKE | 通配符匹配 |
REGEXP 或 RLIKE | 正则表达式匹配 |
例1:“<>” 和 “=” 相反,如果两则操作不等 返回结果为1,否则返回结果0,“NULL”不能用于“<>”比较
mysql> select 1<>0,1<>1,null<>null;
+------+------+------------+
| 1<>0 | 1<>1 | null<>null |
+------+------+------------+
| 1 | 0 | NULL |
+------+------+------------+
1 row in set (0.00 sec)
例2:“<=>”安全等于运算符和“=”操作相同返回1,区别在于“<=>”在值为null的时候也可以做比较
mysql> select 1<=>1,1<=>0,null<=>null;
+-------+-------+-------------+
| 1<=>1 | 1<=>0 | null<=>null |
+-------+-------+-------------+
| 1 | 0 | 1 |
+-------+-------+-------------+
1 row in set (0.02 sec)
例3:“<”当左侧小于右侧时,返回1
mysql> select "a"<"b","A"<"b","bdf"<"c",1<2;
+---------+---------+-----------+-----+
| "a"<"b" | "A"<"b" | "bdf"<"c" | 1<2 |
+---------+---------+-----------+-----+
| 1 | 1 | 1 | 1 |
+---------+---------+-----------+-----+
1 row in set (0.02 sec)
例4:between,格式为“a BETWEEN min AND max”,表示a大于等于min并且小于等于max的时候,返回1,否则返回0
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.01 sec)
例5:regexp,格式为“str REGEXP str_pat”,表示str字符串中含有str_pat相匹配的字符串时,则返回1,否则返回0
mysql> select "abcdeef" regexp "ab","abcdef" regexp "g","abcedf" regexp "df";
+-----------------------+---------------------+----------------------+
| "abcdeef" regexp "ab" | "abcdef" regexp "g" | "abcedf" regexp "df" |
+-----------------------+---------------------+----------------------+
| 1 | 0 | 1 |
+-----------------------+---------------------+----------------------+
1 row in set (0.01 sec)
其他的使用简单,直接上语法,示例就不写了
in,使用格式为“a IN (value1,value2,…)”
IS NULL,使用格式为“a IS NULL”
IS NOT NULL,使用格式为“a IS NOT NULL”
LIKE,使用格式为“a LIKE %123%”
1.3、逻辑运算符
MySQL中的逻辑运算符
运算符 | 作用 |
NOT 或! | 逻辑非 |
AND 或&& | 逻辑与 |
OR 或 || | 逻辑或 |
XOR | 逻辑异或 |
例1:““NOT”或“!”表示逻辑非。返回和操作数相反的结果。当操作为0,则返回1;当操作为1,则返回0,但是NOT NLLL返回null
mysql> select not 0,!0,not 1,not null;
+-------+----+-------+----------+
| not 0 | !0 | not 1 | not null |
+-------+----+-------+----------+
| 1 | 1 | 0 | NULL |
+-------+----+-------+----------+
1 row in set (0.00 sec)
例2:“AND”和“&&”表示逻辑与运算,当操作数均为非零值,返回结果1,否则返回0,当操作数中有任意一个为NULL,其中一个值如果为0,返回0;其他值都是>0的数值,另外有NULL,则返回NULL
mysql> select (1 and 1),(0 and 1),(3 and 1),(0 and null),(1 and null);
+-----------+-----------+-----------+--------------+--------------+
| (1 and 1) | (0 and 1) | (3 and 1) | (0 and null) | (1 and null) |
+-----------+-----------+-----------+--------------+--------------+
| 1 | 0 | 1 | 0 | NULL |
+-----------+-----------+-----------+--------------+--------------+
1 row in set (0.00 sec)
mysql> select 1 and NULL and 0;
+------------------+
| 1 and NULL and 0 |
+------------------+
| 0 |
+------------------+
1 row in set (0.00 sec)
mysql> select 1 and NULL and 3;
+------------------+
| 1 and NULL and 3 |
+------------------+
| NULL |
+------------------+
1 row in set (0.00 sec)
例3:“OR”或“||”表示逻辑或运算。当操作数均不为NULL时,任意一方非零,则结果为1,否则为0;当有一个操作数为NULL,如果另一个操作数非零,则结果为1,否则结果为NULL
mysql> select (1 or 0),(0 or 0),(1 or NULL),(0 or NULL),(NULL or NULL);
+----------+----------+-------------+-------------+----------------+
| (1 or 0) | (0 or 0) | (1 or NULL) | (0 or NULL) | (NULL or NULL) |
+----------+----------+-------------+-------------+----------------+
| 1 | 0 | 1 | NULL | NULL |
+----------+----------+-------------+-------------+----------------+
1 row in set (0.00 sec)
例4:XOR表示逻辑异或。当任意一个操作数为 NULL 时,返回值为 NULL。对于非 NULL 的操作数,如果两个的逻辑真假值相异,则返回结果 1;否则返回 0。
mysql> select (0 xor 0),(1 xor 0),(1 xor 1),(1 xor null),(0 xor null),(null xor null);
+-----------+-----------+-----------+--------------+--------------+-----------------+
| (0 xor 0) | (1 xor 0) | (1 xor 1) | (1 xor null) | (0 xor null) | (null xor null) |
+-----------+-----------+-----------+--------------+--------------+-----------------+
| 0 | 1 | 0 | NULL | NULL | NULL |
+-----------+-----------+-----------+--------------+--------------+-----------------+
1 row in set (0.00 sec)
1.4、位运算符
MySQL支持的位运算符
运算符 | 作用 |
& | 位与(位 AND) |
| | 位或 (位 OR ) |
^ | 位异或(位 XOR) |
~ | 位取反 |
>> | 位右移 |
<< | 位左移 |
例1:“位与”对多个操作数的二进制位作逻辑与操作。2&3,2的二进制数为10,3的二进制数为11,将此做与操作,结果还是10,转换为十进制结果就是2
mysql> select 2&3;
+-----+
| 2&3 |
+-----+
| 2 |
+-----+
1 row in set (0.01 sec)
例2:“位或”对多个操作数的二进制位作逻辑或操作。2&3,2的二进制数为10,3的二进制数为11,将此做与操作,结果就变成11,转换为十进制结果就是3
mysql> select 2|3;
+-----+
| 2|3 |
+-----+
| 3 |
+-----+
1 row in set (0.00 sec)
例3:“位异或”对多个操作数的二进制位作异或操作。2^3 ,2的二进制数为10,3的二进制数为11,10^11结果就是01,转换为十进制结果就是1
mysql> select 2^3;
+-----+
| 2^3 |
+-----+
| 1 |
+-----+
1 row in set (0.01 sec)
例4:“位取反”对操作数的二进制位做NOT操作,这里的操作数只能是一位,解释:在mysql中常亮数字默认会以8字节表示,8个字节就是64位,而常量1的二进制 就是前面63个0,1个1,位去反后就是63个1,1个0,转换为二进制后就是 18446744073709551614,
mysql> select ~1,~18446744073709551614
-> ;
+----------------------+-----------------------+
| ~1 | ~18446744073709551614 |
+----------------------+-----------------------+
| 18446744073709551614 | 1 |
+----------------------+-----------------------+
1 row in set (0.01 sec)
mysql> select bin(18446744073709551614);
+------------------------------------------------------------------+
| bin(18446744073709551614) |
+------------------------------------------------------------------+
| 1111111111111111111111111111111111111111111111111111111111111110 |
+------------------------------------------------------------------+
1 row in set (0.03 sec)
例5:“位右移”对左操作数向右移动操作数指定的位数。例如100>>3,对100的二进制数0001100100右移动3位,0000001100,转换为二进制数就是12:
mysql> select 100>>3;
+--------+
| 100>>3 |
+--------+
| 12 |
+--------+
1 row in set (0.00 sec)
例6:“位左移”对左操作数向左移动操作数指定的位数。例如100<<3,对100的二进制数0001100100000右移动3位,1100100000000,转换为二进制数就是800:
mysql> select 100<<3;
+--------+
| 100<<3 |
+--------+
| 800 |
+--------+
1 row in set (0.00 sec)
1.5、运算符的优先级
MySQL中的运算符优先级
优先级顺序 | 运算符 |
1 | := |
2 | ||, OR, XOR |
3 | &&, AND |
4 | NOT |
5 | BETWEEN, CASE, WHEN, THEN, ELSE |
6 | =, <=>, >=, >, <=, <, <>, !=, IS, LIKE, REGEXP, IN |
7 | | |
8 | & |
9 | <<, >> |
10 | -, + |
11 | *, /, DIV, %, MOD |
12 | ^ |
13 | - (一元减号), ~ (一元比特反转) |
14 | ! |
此章节完成