mysql基础(23)_运算符

查询中的运算符

算术运算符
基本算术运算符:+、-、*、%
基本算术运算:通常不在条件中使用,而是用于结果运算(select 字段中)

mysql> create table math_operator(
    -> int_1 int,
    -> int_2 int,
    -> int_3 int,
    -> int_4 int
    -> )charset=utf8;
Query OK, 0 rows affected (1.86 sec)

mysql> insert into math_operator values(100,-100,0,default);
Query OK, 1 row affected (1.55 sec)

mysql> select * from math_operator;
+-------+-------+-------+-------+
| int_1 | int_2 | int_3 | int_4 |
+-------+-------+-------+-------+
|   100 |  -100 |     0 |  NULL |
+-------+-------+-------+-------+
1 row in set (0.00 sec)
-- 除法用浮点数表示,无意义的除法会显示null,与null的任何运算都是null
mysql> select int_1+int_2,int_1-int_2,int_1*int_2,int_1/int_2,int_2/int_3,int_2/6,int_4/5 from math_operator;
+-------------+-------------+-------------+-------------+-------------+----------+---------+
| int_1+int_2 | int_1-int_2 | int_1*int_2 | int_1/int_2 | int_2/int_3 | int_2/6  | int_4/5 |
+-------------+-------------+-------------+-------------+-------------+----------+---------+
|           0 |         200 |      -10000 |     -1.0000 |        NULL | -16.6667 |    NULL |
+-------------+-------------+-------------+-------------+-------------+----------+---------+
1 row in set (0.00 sec)

总结:
1、在mysql中除法的运算结果是用浮点数表示。
2、除法中除数如果为0,系统会给NULL。
3、NULL进行任何算术运算结果都为NULL。

比较运算符
>、>=、<、<=、=、<>
通常是用来在条件中进行限定结果。
注意:
=:在mysql中,没有对应的==比较符就是使用=来进行相等判断,一般是数字比较。
<=>:相等比较,等价于=,可以用于字符串上。
特殊应用:就是在字段结果中进行比较运算。(系统会自动转换成同类型值进行比较)

-- mysql中没有规定select中必须有数据表,比较运算与数据表无关,所以可以在字段中进行字符串与数字之间比较
mysql> select 1<=>'1',2=2,3<=>0;
+---------+-----+-------+
| 1<=>'1' | 2=2 | 3<=>0 |
+---------+-----+-------+
|       1 |   1 |     0 |
+---------+-----+-------+
1 row in set (0.00 sec)

在条件判断的时候,还有对应的比较运算符:between...and...
计算区间(该区间为闭区间) 
基本语法:字段名 between 条件1 and 条件2;(条件1<=条件2)

mysql> select * from data_groupby;
+----+------+------+-------+
| id | name | sex  | score |
+----+------+------+-------+
|  1 | 静静 | 男   |    78 |
|  2 | 何萍 | 女   |    98 |
|  1 | 李艾 | 女   |    96 |
|  2 | 萧何 | 男   |    99 |
|  3 | 雯雯 | 女   |    82 |
|  1 | 张少 | 男   |    90 |
+----+------+------+-------+
6 rows in set (1.71 sec)
-- 条件1必须(=条件2
mysql> select name,score from data_groupby where score between 100 and 90;
Empty set (0.00 sec)
-- 包含90和100
mysql> select name,score from data_groupby where score between 90 and 100;
+------+-------+
| name | score |
+------+-------+
| 何萍 |    98 |
| 李艾 |    96 |
| 萧何 |    99 |
| 张少 |    90 |
+------+-------+
4 rows in set (0.00 sec)

逻辑运算符(and、or、not)
and:逻辑与
or:逻辑或
not:逻辑非

-- 等价于上图查询命令between...and...,效果一样。
mysql> select name,score from data_groupby where score >=90 and score<=100;
+------+-------+
| name | score |
+------+-------+
| 何萍 |    98 |
| 李艾 |    96 |
| 萧何 |    99 |
| 张少 |    90 |
+------+-------+
4 rows in set (0.00 sec)
-- 查询一班三班学生姓名、分数。
mysql> select id,name,score from data_groupby where id=1 or id=3;
+----+------+-------+
| id | name | score |
+----+------+-------+
|  1 | 静静 |    78 |
|  1 | 李艾 |    96 |
|  3 | 雯雯 |    82 |
|  1 | 张少 |    90 |
+----+------+-------+
4 rows in set (0.00 sec)

in 运算符
in:在什么里面,是用来替代=,当结果不是一个值,而是一个结果集的时候。
基本语法:字段名 in(结果1,结果2,结果3...),只要当前条件在结果集中出现过,那么就成立。

-- 查询名字包含'萧何','雯雯'的信息。
mysql> select * from data_groupby where name in('萧何','雯雯');
+----+------+------+-------+
| id | name | sex  | score |
+----+------+------+-------+
|  2 | 萧何 | 男   |    99 |
|  3 | 雯雯 | 女   |    82 |
+----+------+------+-------+
2 rows in set (1.54 sec)

is运算符
is是专门用来判断字段是否为NULL的运算符
基本语法:字段名 is null/is not null;

mysql> select * from int_data;
+-------+-------+--------+----------+------------+-------+-------+-------+
| int_1 | int_2 | int_3  | int_4    | int_5      | int_6 | int_7 | int_8 |
+-------+-------+--------+----------+------------+-------+-------+-------+
|    10 | 10000 | 100000 | 10000000 | 1000000000 |  NULL |  NULL |  NULL |
|   127 |   255 |    255 |      255 |        255 |  NULL |  NULL |  NULL |
|  -128 |   255 |    255 |      255 |        255 |  NULL |  NULL |  NULL |
|   100 |   255 |    255 |      255 |        255 |   255 |  NULL |  NULL |
|     6 |     6 |      6 |        6 |          6 |     6 |   006 |  NULL |
|     6 |     6 |      6 |        6 |          6 |     6 |   006 |    06 |
|   100 |   100 |    100 |      100 |        100 |   100 |   100 |   100 |
+-------+-------+--------+----------+------------+-------+-------+-------+
7 rows in set (0.00 sec)
-- 查null此时in匹配不到,因为和in中null匹配时值都为null
mysql> select * from int_data where int_6 in(null);
Empty set (0.00 sec)
-- 专门为查null准备的。
mysql> select * from int_data where int_6 is null;
+-------+-------+--------+----------+------------+-------+-------+-------+
| int_1 | int_2 | int_3  | int_4    | int_5      | int_6 | int_7 | int_8 |
+-------+-------+--------+----------+------------+-------+-------+-------+
|    10 | 10000 | 100000 | 10000000 | 1000000000 |  NULL |  NULL |  NULL |
|   127 |   255 |    255 |      255 |        255 |  NULL |  NULL |  NULL |
|  -128 |   255 |    255 |      255 |        255 |  NULL |  NULL |  NULL |
+-------+-------+--------+----------+------------+-------+-------+-------+
3 rows in set (0.00 sec)

like运算符
like运算符:是用来进行模糊匹配(匹配字符串)
基本语法;like '匹配模式';

匹配模式中,有两种占位符:
_:匹配对应的单个字符
%:匹配多个字符

-- 注意%之后可以包含多个字符,_只能匹配单个字符。
mysql> select * from data_groupby where name like '李%';
+----+------+------+-------+
| id | name | sex  | score |
+----+------+------+-------+
|  1 | 李艾 | 女   |    96 |
+----+------+------+-------+
1 row in set (0.00 sec)

mysql> update data_groupby set name='李清照' where score=96;
Query OK, 1 row affected (1.62 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from data_groupby where name like '李%';
+----+--------+------+-------+
| id | name   | sex  | score |
+----+--------+------+-------+
|  1 | 李清照 | 女   |    96 |
+----+--------+------+-------+
1 row in set (0.11 sec)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值