MySQL数据库(19):高级数据操作-查询数据
【摘要】
查询中的运算符
1、算术运算符
+ 加
- 减
* 乘
/ 除
% 取余/取模
12345
通常不在条件中使用,而是用于结果运算(select字段中)
null进行任何算术运算,结果都为null ...
查询中的运算符
1、算术运算符
+ 加
- 减
* 乘
/ 除
% 取余/取模
- 1
- 2
- 3
- 4
- 5
通常不在条件中使用,而是用于结果运算(select字段中)
null进行任何算术运算,结果都为null
除法中除数如果为0,结果为null
mysql> select 1+1, 2-1, 2*3, 6/2, 7%2, 1/0, 1/null;
+-----+-----+-----+--------+------+------+--------+
| 1+1 | 2-1 | 2*3 | 6/2 | 7%2 | 1/0 | 1/null |
+-----+-----+-----+--------+------+------+--------+
| 2 | 1 | 6 | 3.0000 | 1 | NULL | NULL |
+-----+-----+-----+--------+------+------+--------+
- 1
- 2
- 3
- 4
- 5
- 6
2、比较运算符
> 大于
>= 大于等于
< 小于
<= 小于等于
= 等于 (<=>)
<> 不等于
- 1
- 2
- 3
- 4
- 5
- 6
用来在条件中限定结果
select '1' = 1, 0.02 = 0;
+---------+----------+
| '1' = 1 | 0.02 = 0 |
+---------+----------+
| 1 | 0 |
+---------+----------+
- 1
- 2
- 3
- 4
- 5
- 6
- MySQL 中没有规定select必须有数据表
- MySQL中数据会先自动转成同类型,再比较
- MySQL中没有bool类型,1代表true, 0代表false
计算闭区间
-- 条件1 需要比 条件2小
字段 between 条件1 and 条件2
- 1
- 2
mysql> select * from my_student;
+----+--------+----------+------+--------+
| id | name | class_id | age | gender |
+----+--------+----------+------+--------+
| 1 | 刘备 | 1 | 18 | 2 |
| 2 | 李四 | 1 | 19 | 1 |
| 3 | 王五 | 2 | 20 | 2 |
| 7 | 张飞 | 2 | 21 | 1 |
| 8 | 关羽 | 1 | 22 | 2 |
+----+--------+----------+------+--------+
-- 查找年龄在[19, 21]区间的学生
mysql> select * from my_student where age between 19 and 21;
+----+--------+----------+------+--------+
| id | name | class_id | age | gender |
+----+--------+----------+------+--------+
| 2 | 李四 | 1 | 19 | 1 |
| 3 | 王五 | 2 | 20 | 2 |
| 7 | 张飞 | 2 | 21 | 1 |
+----+--------+----------+------+--------+
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
3、逻辑运算符
and 逻辑与
or 逻辑或
not 逻辑非
- 1
- 2
- 3
-- 查找年龄在[19, 21]区间的学生
select * from my_student where age >= 19 and age <= 21;
+----+--------+----------+------+--------+
| id | name | class_id | age | gender |
+----+--------+----------+------+--------+
| 2 | 李四 | 1 | 19 | 1 |
| 3 | 王五 | 2 | 20 | 2 |
| 7 | 张飞 | 2 | 21 | 1 |
+----+--------+----------+------+--------+
-- 查找年龄大于20或者是男性的学生
select * from my_student where age > 20 or gender = 1;
mysql> select * from my_student where age > 20 or gender = 1;
+----+--------+----------+------+--------+
| id | name | class_id | age | gender |
+----+--------+----------+------+--------+
| 2 | 李四 | 1 | 19 | 1 |
| 7 | 张飞 | 2 | 21 | 1 |
| 8 | 关羽 | 1 | 22 | 2 |
+----+--------+----------+------+--------+
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
4、in运算符
用来替代等号,判断集合
基本语法
in (数值1, 数值2...)
- 1
-- 按照学号查找学生
mysql> select * from my_student where id in (1, 3);
+----+--------+----------+------+--------+
| id | name | class_id | age | gender |
+----+--------+----------+------+--------+
| 1 | 刘备 | 1 | 18 | 2 |
| 3 | 王五 | 2 | 20 | 2 |
+----+--------+----------+------+--------+
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
5、is运算符
判断字段值是否为null
基本语法
is null
is not null
- 1
- 2
mysql> select * from my_student;
+----+--------+----------+------+--------+
| id | name | class_id | age | gender |
+----+--------+----------+------+--------+
| 1 | 刘备 | 1 | 18 | 2 |
| 2 | 李四 | 1 | 19 | 1 |
| 3 | 王五 | 2 | 20 | 2 |
| 7 | 张飞 | 2 | 21 | 1 |
| 8 | 关羽 | 1 | 22 | 2 |
| 9 | 曹操 | 1 | 20 | NULL |
+----+--------+----------+------+--------+
-- 查询为null的数据
mysql> select * from my_student where gender is null;
+----+--------+----------+------+--------+
| id | name | class_id | age | gender |
+----+--------+----------+------+--------+
| 9 | 曹操 | 1 | 20 | NULL |
+----+--------+----------+------+--------+
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
6、like运算符
模糊匹配
like 匹配模式
占位符:
- 下划线
_
匹配单个字符 - 百分号
%
匹配多个字符
mysql> select * from my_student where name like '曹_';
+----+--------+----------+------+--------+
| id | name | class_id | age | gender |
+----+--------+----------+------+--------+
| 9 | 曹操 | 1 | 20 | NULL |
+----+--------+----------+------+--------+
mysql> select * from my_student where name like '曹%';
+----+--------+----------+------+--------+
| id | name | class_id | age | gender |
+----+--------+----------+------+--------+
| 9 | 曹操 | 1 | 20 | NULL |
+----+--------+----------+------+--------+
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。
原文链接:pengshiyu.blog.csdn.net/article/details/125052159
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)