数据库进阶,数据库表查询进阶(1)
老铁们,大家好,相信还有很多朋友对于数据库进阶和数据库表查询进阶(1)的相关问题不太懂,没关系,今天就由我来为大家分享分享数据库进阶以及数据库表查询进阶(1)的问题,文章篇幅可能偏长,希望可以帮助到大家,下面一起来看看吧!
数据库表查询进阶(1)
in操作符允许我们在 WHERE子句中规定多个值。
where字段名 in(值1,值2,值3....)
示例: where age in(20,21,25);
age字段的值只要是 20或者21或者25的数据都满足条件
where not in(值1,值2,值3....)</pre>
示例: where age in(20,21,25);
age字段的值只要不是 20或者21或者25的数据都满足条件
select*
from sc
where score in(70,80,90);
select*
from sc
where score not in(70,80,90);
select*
from student
where birthday in('1990-01-01','1990-12-21','2017-12-20','2012-06-06');</pre>
select*
from student
where birthday not in('1990-01-01','1990-12-21','2017-12-20','2012-06-06');
MySQL中的分页查询, limit通常放在sql语句的最末尾
limit 4
查询前4条数据
limit 4,3
从第4条数据之后开始,查询3条数据
也就是第5,6,7条的数据
limit 4 offset 10;
offset后面的数字是指记录数
从第10条之后的数据开始查询4条数据
也就是第 11,12,13,14条数据
select* from student limit 5;
select* from student limit 9,6;
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;
如果字段没有要求非空,则字段值可以为空,就是null值。如果要查询某个字段为 null的数据,不能使用字段名=null,要用字段名 is null;
where字段名 is null;
字段名为空的符合条件
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;
给 sname,birthday字段起别名,改为中文
select sname as姓名,birthday as生日 from student;
起了别名后,查出来的字段会以别名展示。
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='女');
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;
聚合函数对一组值执行计算,并返回单个值,也被称为组函数。
就是sql提供的一种查询方法,比如查询最大值,最小值,总数,平均等等。
聚合函数不能写在where的查询条件里面
查询最小值
select min(字段) from表;
查询最大值
select max(字段) from表;
select max(tid) from teacher;
select min(tid) from teacher;
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;
查询该字段的总数
select sum(字段) from表
查询该字段的平均数
select avg(字段) from表
select avg(score) from sc;
select avg(s.score) from student a inner join sc s on a.sid= s.sid
where a.sex='女';
查询数据总条数
count()是查询总共有多少条数据
count(字段)是查询该字段不为 null的总共有多少条数据
例:
select count() from表
去重,查询数据时,相同的数据只展示一条
语法:
select distinct字段 from表;
例:去重查询学生表中的生日字段
select distinct birthday from student;
数据库该怎么学习,纯小白
相信很多数据库入门的新手们在学习数据库方面都存在困惑,本文列出了一个非常完整的数据库学习路线,并对数据库学习过程中的细节进行详细指导。希望能够成为大家学习数据库过程中一份纲领性的教程。
本回答来自:数据库怎么学?数据库学习零基础入门指导_树懒学堂
数据库知识要点学习新手学习数据库务必把握的知识要点:
数据库的安装下载:了解数据库的环境变量,文件目录构造。
数据库网络服务器的启动,登陆与登出。
数据库常用命令及语法标准。
数据库基本数据类型与数据表的实际操作。比如,数据表的增删、单表查寻、多表查询等。
数据库运算符和函数,比如,日期函数,时间函数,信息函数,聚合函数,数据加密涵数,自定义函数等。
数据库存储过程,存储过程的调度。
数据库每个存储引擎的特性。
数据库事务管理的定义和应用等。
数据库管理权限和用户管理等。
数据库学习材料推荐:1.《MySQL必知必会》
这书讲的十分全,从基本要素,到查寻到插入新建表,用户的管理方法,都是有实际的事例,特别适合没有基础的同学们来学习Mysql,总而言之这本书学习的方式便是:
掌握数据库的基本概念
按照示例进行练习
2.《SQL必知必会》
纯新手必读,这也是Amazon上最热销的SQL书籍的汉化版,写的很轻快,定义十分清晰。这本书用于学习关系型数据库也非常好,基本概念比大部头的教材内容说得清晰得多。
网站推荐:树懒学堂_一站式数据知识学习平台
SQL数据库试题求解
------------------------------------------------------
create table students(st_id varchar(20),st_name varchar(50),sex varchar(10))
insert into students(st_id,st_name,sex)
select'st001','张杰','男' union all
select'st002','公孙燕飞','男' union all
select'st003','王楠','女' union all
select'st004','王伟','男' union all
select'st005','李燕纹','女' union all
select'st006','孙武','男'
select*
from students
create table teachers(t_id varchar(20),t_name varchar(50),t_lesson varchar(50))
insert into teachers
select't001','张老师','数学' union all
select't002','李老师','英语'
delete from results
create table results(r_id varchar(20),r_fenshu int,r_stid varchar(50),r_tid varchar(50))
insert into results
select'r001','90','st001','t002' union all
select'r002','68','st005','t001' union all
select'r003','92','st003','t001' union all
select'r004','82','st006','t002' union all
select'r005','70','st002','t002' union all
select'r006','86','st002','t001' union all
select'r007','57','st003','t002' union all
select'r008','76','st006','t001' union all
select'r009','55','st001','t001' union all
select'r010','77','st004','t002' union all
select'r011','58','st005','t002'
----------------------------------------------------------
1.
select st_id
from students
where st_name='王伟'
2.select st_id,st_name
from students
where st_name like'__燕%'
3 select st_name,len(st_name) as名字长度
from students
where sex='男'
4 select min(r_fenshu) as最低分数
from teachers t inner join results r on t.t_id=r.r_tid
where t_lesson='数学'--这个是不考虑成绩中有null值的
5 select s.st_id as学生编号,r_fenshu as分数,r_tid as课目号
from students s inner join results r on s.st_id=r.r_stid
where s.sex='女'
--如果还要课目的名称的话请用下面的
select s.st_id as学生编号,r.r_fenshu as分数,r.r_tid as课目号,t.t_lesson as课目名称
from students s inner join results r on s.st_id=r.r_stid
inner join teachers t on r.r_tid= t.t_id
where s.sex='女'
6 select avg(r.r_fenshu)
from results r inner join teachers t on r.r_tid= t.t_id
where t.t_lesson='英语'
7.select*
from students s inner join results r on s.st_id=r.r_stid
inner join teachers t on r.r_tid= t.t_id
where s.st_id in(select top 2 st_id from students order by st_id desc)
order by s.st_id desc
8 select sum(r.r_fenshu) as总分
from results r inner join students s on r.r_stid=s.st_id
where s.st_name='王楠'
9.select distinct s.st_id,s.st_name
from students s inner join results r on s.st_id= r.r_stid
where st_id not in(select r_stid from results where r_fenshu<60) and st_id not in(select r_stid from results where r_fenshu>=90)
10 update results
set r_fenshu= r_fenshu+ 10
--如果分数不可能大于100请用这句 set r_fenshu= case when r_fenshu+ 10<=100 then r_fenshu+ 10 else 100 end
where r_stid in(select st_id from students where sex='女')
1进阶题
select t.t_name,count(*)
from students s,teachers t,results r
where r.r_tid= t.t_id
and s.st_id=r.r_stid
and r.r_fenshu>= 60
and t.t_id in(select t_id from teachers where t_lesson='数学')
--and t_lesson='数学'
group by t.t_name
2
select top 1 sum(r_fenshu) as总分,t.t_lesson,t_id,t_name
from results r,teachers t
where r.r_tid= t.t_id
group by t.t_lesson,t_id,t_name
order by总分 desc
3. delete from results where r_stid in(select r_stid from results group by r_stid having count(r_tid)= 1)
1选做题
select d.name from sysobjects d where d.xtype='U'
2.select top 5* from students order by newid()
文章到此结束,如果本次分享的数据库进阶和数据库表查询进阶(1)的问题解决了您的问题,那么我们由衷的感到高兴!