我们使用DML语句来操作数据库的内容,这是种类型语句只于内容有关,不于数据库的结构相关,它使 用的关键字insert、delete、update、select等,四大关键字
基础的DML语句
所有的查询语句的语法格式都是固定的
select 列名 from 表名 where 条件;
2、as别名转换,讲一个列或一个表的查询结果做别名转换
select count(*) as 'totalCount' from stuinfo;
如果做了别名转换,可以省略as
select count(*) 'totalCount' from stuinfo;
3、模糊查询使用关键字like,通配符%,占位符_
--查询学生信息中,所有姓名为蔡的
select * from stuinfo where sname like '蔡%';
--查询所有生日是15号的学生
select * from stuinfo where sbirthday like '';
--查询所有姓名是三个字的学生
select * from stuinfo where sname like '___';
4、在stuinfo查询学位为2005050209或2005050102的学生
select * from stuinfo where sid = '2005050209' or sid = '2005050102';
5、早stuinfo查询学号为2005010101或2005010102或2005010103
select * from stuinfo where sid in ("2005010101","2005010102","2005010103");
6、在stuinfo中查询学号不为2005050209且不为2005050102的学生
select * from stuinfo where sid != '2005050209' and sid != '2005050102';
7、查询所有学生信息,显示当前学生所对应的班级名称
select a.*,b.cname from stuinfo a
inner join classinfo b on a.cid = b.cid
多表联合查询,目前阶段我们使用inner join后面再使用on来设置字段链接的依据 也可以使用下面的方式完成
select *,(select cname from classinfo where cid = stuinfo.cid) as 'cname' from
stuinfo
8、查询所有学生信息,显示当前学生所对应的班级名称以及该系别名
select a.*,b.cname,c.dname from stuinfo a
inner join classinfo b on a.cid = b.cid
inner join departmentinfo c on b.did = c.did;
--或
select a.*,b.cname,c.dname from stuinfo a,classinfo b,departmentinfo c WHERE
a.cid = b.cid and b.did = c.did;
9、查询所有学生信息,但是它的老师只能是孙鹏的,并且学生性别为女
select a.* from stuinfo a
inner join classinfo b on a.cid = b.cid
inner join teacherinfo c on b.tid = c.tid
where c.tname = "孙鹏" and a.ssex = "女";
--或
select a.* from stuinfo a,classinfo b,teacherinfo c
where a.cid = b.cid and b.tid = c.tid
and c.tname = "孙鹏" and a.ssex = "女";
分析: 1、学生信息表stuinfo里面是没有老师信息,我们要想办法找到老师信息 2、所以我们通过stuinfo找到classinfo ,再找到teacherinfo这样就可以找到老师信息 3、所加到联查查询的每个表都可以再where 里面添加过滤条件
10、查询学生信息表中有多少个男生,多少个女生
select ssex,count(*) 'totalCount' from stuinfo group by ssex;
11、查询学生信息表中每个班各多少人
select cid,count(*) 'totalCount' from stuinfo group by cid;
12、查询学生信息表中每个班各多少,同时显示班级名称
select c.cid,c.cname,count(*) 'totalCount' from
(select a.*,b.cname from stuinfo a
inner join classinfo b on a.cid = b.cid) as c
group by c.cid;
分析: 上面应用到子查询,括号里面的sql语句会优先执行,查询完毕以后把这个查询结果当成一个表c,然后 再到c表里面做分类统计 先内联了classinfo表
13、查询学生成绩表,显示每个学生的平均成绩,是同显示这个学生的姓名
select a.*,b.sname from (select sid,avg(score) 'avgscore' from gradeinfo group by
sid) a
inner join stuinfo b on a.sid = b.sid;
14、查询每一个班的平均成绩
select c.cid,avg(score) from
(select a.*,b.cid from gradeinfo a inner join stuinfo b on a.sid = b.sid) c
group by c.cid;
15、在成绩信息表当中,求出每个学生的总成绩
select sid,sum(score) 'sumScore' from gradeinfo
group by sid;
16、在成绩信息表里面,求出学生在其中考试于期末考试的总成绩
select sid,eid,sum(score) 'sumScore' from gradeinfo
group by sid,eid;
分析:在这条sql语句中,我们将group by进行了两次分组,分组其实可以多次的
17、查询老师里面年龄大于“孙鹏”老师的
select * from teacherinfo,
(select tage from teacherinfo where tname = '孙鹏') a
where teacherinfo.tage > a.tage;
上面其实是多表联查的一种法方式
18、查询辅导员为王鹏的学生
select a.* from stuinfo a inner join classinfo b on a.cid = b.cid
inner join instructor c on c.instructorid = b.instructorid
where c.instructorname = "王鹏";
19、查询成绩信息表里面每一科的最高分,并且显示学生姓名
select b.*,c.sname from
(select courseid,max(score) 'maxScore' from gradeinfo group by courseid) a
inner join gradeinfo b on a.courseid = b.courseid and a.maxScore = b.score
inner join stuinfo c on b.sid = c.sid;