文章目录
————————————————————————————————
【1】基本查询
1.查询学生的姓名
select name from student
2.查询学生姓名和性别
select name,sex from student
3.查询学生全部信息
select * from student
SELECT是关键字,表示将要执行一个查询,*
表示”所有列“,FROM表示将要从哪个表查询。
注意:查询结果也是一个二维表,它包含列名和每一行的数据。
【2】条件查询
1、明确查询条件的用等号
SELECT *FROM student where class_num='20201001';
2、查询范围:大于,小于,不等于
SELECT * FROM student where age > 20;
SELECT * FROM student where age != 20;
SELECT * FROM student where age <> 20;
3、查询多列:逗号分隔
查询班级号为20201001的学生的姓名、性别、年龄(班级号也显示出来)
SELECT name,sex,age,class_num FROM student where class_num = '20201001';
4、范围在中间
查询年龄在21-25之间的
select * from student where age between 21 and 25;
5、多条件
查询出班级号为20201001的男生(这里的“和”代码逻辑为与 and)
select * from student age between 21 and 25;
查询出班级号为20201001和班级号为20201002的男生(这里的“和”代码逻辑为或 or)
select * from student where class_num = '20201001' or class_num = '20201002';
6、in 包含——>同一个字段下包含多个值(字符串型需要加单引号)
查询出用户id为1和3的用户
select * from student where id in(1,3)
select * from student where class_num in('20201001','20201002');
7、like(有通配符)模糊查询:多适用于搜索资源
like——>通配符(_:单个字符,%:匹配任意多个字段)
以下是错误的:select * from student where name like '王'
查询出姓王的同学
select * from student where name like '王%';
查询所有带张字的同学
select * from student where name like '%张%';
模糊查询 字段名 like ‘匹配内容’
- _ :下划线 代表匹配任意一个字符;
- % :百分号 代表匹配0个或多个字符;
- []: 中括号 代表匹配其中的任意一个字符;
- [^]: ^尖冒号 代表 非,取反的意思;不匹配中的任意一个字符。
【3】聚合查询
常用的:sum()求和、avg()平均数、max()最大值、min()最小值、c