目录 Mysql学习总结之路(第一章:服务与数据库管理) mysql学习总结之路(第二章:表) Mysql学习总结之路(第三章:数据类型) MySQL学习总结之路(第四章:运算符) 1.1、算数运算符 MySQL支持的算术运算符
mysql学习总结之路(第二章:表)
Mysql学习总结之路(第三章:数据类型)
MySQL学习总结之路(第四章:运算符)
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.05 sec)
mysql> select 1/0;
+------+
| 1/0 |
+------+
| NULL |
+------+
1 row in set (0.00 sec)
mysql> select 1%2,mod(1,2);
+------+----------+
| 1%2 | mod(1,2) |
+------+----------+
| 1 | 1 |
+------+----------+
1 row in set (0.00 sec)
MySQL支持的比较运算符
运算符 | 作用 |
= | 等于 |
<>或!= | 不等于 |
<=> | NULL 安全的等于(NULL-safe) |
< | 小于 |
<= | 小于等于 |
> | 大于 |
>= | 大于等于 |
BETWEEN | 存在与指定范围 |
IN | 存在于指定集合 |
IS NULL | 为 NULL |
IS NOT NULL | 不为 NULL |
LIKE | 通配符匹配 |
REGEXP 或 RLIKE | 正则表达式匹配 |
mysql> select 1<>0,1<>1,null<>null;
+------+------+------------+
| 1<>0 | 1<>1 | null<>null |
+------+------+------------+
| 1 | 0 | NULL |
+------+------+------------+
1 row in set (0.00 sec)
mysql> select 1<=>1,1<=>0,null<=>null;
+-------+-------+-------------+
| 1<=>1 | 1<=>0 | null<=>null |
+-------+-------+-------------+
| 1 | 0 | 1 |
+-------+-------+-------------+
1 row in set (0.02 sec)
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)
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)
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%”
MySQL中的逻辑运算符
运算符 | 作用 |
NOT 或! | 逻辑非 |
AND 或&& | 逻辑与 |
OR 或 || | 逻辑或 |
XOR | 逻辑异或 |
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)
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)
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)
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)
MySQL支持的位运算符
运算符 | 作用 |
& | 位与(位 AND) |
| | 位或 (位 OR ) |
^ | 位异或(位 XOR) |
~ | 位取反 |
>> | 位右移 |
<< | 位左移 |
mysql> select 2&3;
+-----+
| 2&3 |
+-----+
| 2 |
+-----+
1 row in set (0.01 sec)
mysql> select 2|3;
+-----+
| 2|3 |
+-----+
| 3 |
+-----+
1 row in set (0.00 sec)
mysql> select 2^3;
+-----+
| 2^3 |
+-----+
| 1 |
+-----+
1 row in set (0.01 sec)
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)
mysql> select 100>>3;
+--------+
| 100>>3 |
+--------+
| 12 |
+--------+
1 row in set (0.00 sec)
mysql> select 100<<3;
+--------+
| 100<<3 |
+--------+
| 800 |
+--------+
1 row in set (0.00 sec)
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 | ! |
此章节完成
--结束END--
本文标题: MySQL学习总结之路(第四章:运算符)
本文链接: https://lsjlt.com/news/8429.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0