数据库表查询进阶(1)

in

in 操作符允许我们在 WHERE 子句中规定多个值。

语法1:

where 字段名 in (值1,值2,值3....)
示例: where age in (20,21,25);
age 字段的值 只要是 20或者21或者25 的数据 都满足条件

语法2:

where not in (值1,值2,值3....)</pre>

示例: where age in (20,21,25);
age 字段的值 只要 不是 20或者21或者25 的数据 都满足条件

实战:

查询sc表中成绩在 70,80,90的数据

select *
from sc
where score in (70,80,90);

查询sc表中成绩不在 70,80,90的数据

select *
from sc
where score not in (70,80,90);

查询 学生表中 生日 在 1990-01-01,1990-12-21,2017-12-20,2012-06-06这四天的学生信息

select *
from student
where birthday in ('1990-01-01','1990-12-21','2017-12-20','2012-06-06');</pre>

查询 学生表中 生日 不在 1990-01-01,1990-12-21,2017-12-20,2012-06-06这四天的学生信息

select *
from student
where birthday not in ('1990-01-01','1990-12-21','2017-12-20','2012-06-06');

limit

MySQL中的分页查询, limit 通常放在sql 语句的最末尾

语法1:

limit 4
查询前4条数据

语法2:

limit 4,3
从第4条数据之后开始,查询3条数据
也就是 第5,6,7条的数据

语法3:

limit 4 offset 10;
offset后面的数字是指记录数
从第10条 之后的数据开始 查询4条数据
也就是 第 11,12,13,14条数据

实战

查询 学生表 中的前五条数据

select * from student limit 5;

查询 学生表 中10到15条数据,包含第十条和第十五条

select * from student limit 9,6;

查询 学生表 中10到15条数据,包含第十条和第十五条 ,使用offset关键字

select * from student limit 6 offset 9;

查询成绩表中 前7条的数据;
select * from sc limit 7;
查询成绩表中 第 5到10 条的数据
select * from sc limit 4,6;
查询成绩表中 第 3到8 条的数据;
select * from sc limit 2 , 6;

is null

如果字段没有要求非空,则字段值 可以为空,就是null值。如果要查询某个 字段 为 null的数据,不能使用 字段名=null,要用 字段名 is null;

语法1:

where 字段名 is null;

字段名 为空的符合条件

语法2:

where 字段名 is not null;

字段名 不为空的符合条件
1.查询teacher表,给tid 起别名为 教师编号,tname 起别名为 教师姓名
select tid as 教师编号,tname as 教师姓名 from teacher
2.查询 sc 表, 给 cid 起别名为 课程编号,sid 起别名为 学生编号, score起别名为分数;
select cid 课程编号 ,sid 学生编号,score 分数 from sc
3.查询 course 表,给表起别名为 c,使用 c.字段查询 表数据
select c.* from course as c ;

别名

起别名是为了后续学习多表联查 做铺垫,语法十分简单

给字段起别名

比如查询 student 表的 学生姓名 和 生日 的sql语句:

select sname,birthday from student;

image.png

给 sname,birthday字段 起别名 ,改为中文

select sname as 姓名,birthday as 生日 from student;


image.png

起了别名后,查出来的字段会以别名展示。
as 关键字也可以省略,字段名 和 别名之间 用空格隔开即可

select sname 姓名, birthday 生日 from student;

给表起别名

例子:

select a.sname, a.birthday from student as a;

给表 起别名 同样是 使用 as关键字(可以使用空格隔开代替as关键字), a 就代表 student表,查询字段时,可以 使用 a.sname 代表 student的sname字段。

给表起别名 通常用在 多表查询,本次使用演示,先熟悉语法。

嵌套查询

在某些复杂的场景下,可能需要多条sql 才能查询出想要的数据,把多条sql拼接起来 就是嵌套查询

常见句式

句式1

select XXX from A表 where xx =/in (select XXX from B表)

句式2

select XXX from (select XXX from XXXX)b

句式3

select XXX from A表 a
join (select XXX from B表)b on a.xx = b.xx

实战

查询周梅的 课程成绩,使用嵌套查询

select * from sc where sid = (select sid from student where sname = '周梅');

查询女生的 课程成绩,使嵌套查询

select * from sc where sid in (select sid from student where sex='女');

查询课程1课程号2成绩高的学生的信息及课程分数

select a.sname,b.score,c.score from student a
inner join (select sid,score from sc where cid =1) b on a.sid = b.sid
inner join (select sid,score from sc where cid = 2) c on a.sid = c.sid
where b.score › c.score;

image.png

聚合函数

聚合函数对一组值执行计算,并返回单个值,也被称为组函数。
就是sql提供的一种查询方法,比如查询 最大值,最小值,总数,平均等等。
聚合函数不能写在where的查询条件里面

min

查询 最小值

语法:

select min(字段) from 表;

max

查询 最大值

语法:

select max(字段) from 表;

1.查询teacher表的最大tid是多少

select max(tid) from teacher;

2.查询teacher表的最小tid是多少

select min(tid) from teacher;

3.查询周梅同学成绩最高的科目是哪个,多少分

select max(a.score) ,b.cname from sc a
inner join course b on a.cid=b.cid
where a.sid =(select sid from student where sname='周梅') group by b.cname limit 1 ;

sum

查询 该字段的 总数

select sum(字段) from 表

avg

查询 该字段的 平均数

select avg(字段) from 表

实战

查询sc表 成绩字段 的 总数是多少

select avg(score) from sc;

查询 男生 的 成绩总分是 多少

select avg(s.score) from student a inner join sc s on a.sid = s.sid
where a.sex ='女';


image.png

count

查询 数据 总条数
count() 是查询总共有多少条数据
count(字段) 是查询该字段 不为 null的 总共有多少条数据
例:
select count(
) from 表

DISTINCT

去重,查询数据时,相同的数据只展示 一条

语法:

select distinct 字段 from 表;
例:去重查询学生表中的生日字段
select distinct birthday from student;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。