MySQL中的运算符使用实例展示
![preview](https://dl-preview.csdnimg.cn/12835847/0001-842782772bd8096973f1623e7e60404a_thumbnail.jpeg)
![preview-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/scale.ab9e0183.png)
在MySQL数据库系统中,运算符是用于执行特定计算或比较的特殊符号。本文将通过实例展示MySQL中的主要运算符类型,包括算数运算符、比较运算符、逻辑运算符和位运算符,以及它们的使用方法和优先级顺序。 1. **算数运算符**: - 加(+): `mysql> select 1+2;` 结果为 3。 - 减(-): `mysql> select 2-1;` 结果为 1。 - 乘(*): `mysql> select 2*3;` 结果为 6。 - 除(/): `mysql> select 5/3;` 结果为 1.6666666666666667。 - 商(DIV): `mysql> SELECT 5 DIV 2;` 结果为 2。 - 模(%): `mysql> select 5%2, mod(5,2);` 结果为 1,1。 2. **比较运算符**: - 等于(=): `mysql> select 1=0,1=1,null=null;` 结果为 0, 1, null。 - 不等于(<>): `mysql> select 1<>0,1<>1,null<>null;` 结果为 1, 0, null。 - 安全等于(<=>): 对于NULL值友好,`mysql> select 1<=>1,2<=>0,0<=>0,null<=>null;` 结果为 1, 0, 1, 1。 - 小于(<): `mysql> select 'a'<'b','a'<'a','a'<'c',1<2;` 结果为 1, 0, 1, 1。 - 小于等于(<=): `mysql> select 'bdf'<='b','b'<='b',0<1;` 结果为 0, 1, 1。 - 大于(>): `mysql> select 'a'>'b','abc'>'a',1>0;` 结果为 0, 1, 1。 - 大于等于(>=): `mysql> select 'a'>='b','abc'>='a',1>=0,1>=1;` 结果为 1, 1, 1, 1。 - BETWEEN: `mysql> select 10 between 10 and 20, 9 between 10 and 20;` 结果为 1, 0。 - IN: `mysql> select 1 in (1,2,3), 't' in ('t','a','b','l','e'), 0 in (1,2);` 结果为 1, 1, 0。 - IS NULL 和 IS NOT NULL: 用于检查是否为NULL,如 `mysql> select 0 is null, null is null;` 和 `mysql> select 0 is not null, null is not null;`。 - LIKE: `mysql> select 123456 like '123%', 123456 like '%123%', 123456 like '%321%';` 结果为 1, 1, 0。 - REGEXP: `mysql> select 'abcdef' regexp 'ab', 'abcdefg' regexp 'k';` 结果为 1, 0。 3. **逻辑运算符**: - 非(NOT): `mysql> select not 0, not 1, not null;` 和 `mysql> select ! 0, ! 1, ! null;` 结果分别为 1, 0, null 和 1, 0, null。 - 与(AND): `mysql> select (1 and 1), (0 and 1), (3 and 1), (1 and null);` 结果为 1, 0, 1, null。 - 或(OR): `mysql> select (1 or 0), (0 or 0), (1 or null), (1 or 1), (null or null);` 结果为 1, 0, 1, 1, null。 - 异或(XOR): `mysql> select (1 xor 1), (0 xor 0), (1 xor 0), (0 xor 1), (null xor 1);` 结果为 0, 0, 1, 1, null。 - ^ 运算符也表示异或,如 `mysql> select (1 ^ 1), (0 ^ 0), (1 ^ 0), (0 ^ 1), (null ^ 1);` 结果同上。 4. **位运算符**: - 位与(&): `mysql> select 2&3;` 结果为 2。 - 位或(|): `mysql> select 2|3;` 结果为 3。 - 位异或(^): `mysql> select 2^3;` 结果为 1。 - 位取反(~): `mysql> select ~1,~18446744073709551614;` 结果为 -2, -18446744073709551615。 - 位右移(>>): `mysql> select 100>>3;` 结果为 12。 - 位左移(<<): `mysql> select 100<<3;` 结果为 800。 5. **运算符优先级顺序**: - 运算符的优先级决定了计算的顺序,例如 `:=` 的优先级最高,`BINARY, COLLATE` 优先级最低。完整的顺序见上述列表。 理解并熟练运用这些运算符对于编写高效的SQL查询至关重要。实践中,建议通过实际操作来加深理解和记忆,正如文中所强调的,“纸上得来终觉浅,绝知此事要躬行”。通过实践,可以更好地掌握MySQL中的运算符用法,从而在数据库管理、数据查询等方面游刃有余。
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/octet-stream](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/octet-stream](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/octet-stream](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
- 粉丝: 3
- 资源: 882
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)