第三次作业

MySQL查询数据(单表查询)

例1:查询student表中所有数据。

select * from student;

例2:查询指定列的数据,查询student表中stuID,stuName两列。

select stuID,stuName from student;

例3:给列指定别名,查询student表中stuID列,指定别名为学号,stuName列,指定别名为姓名。

修改字段别名的语法:as+'名称'

select stuID as 学号,stuName as 姓名 from student;

例4:查询student表中stuID为1001的记录。

select * from student where stuID=1001;;

例5:查询student表中性别为男且年龄小于19岁的记录。

select * from student where stuSex='男' and stuAge<19;;

例6:查询student表中性别为女或年龄等于18岁的记录。

select * from student where stuSex='女' or stuAge=18;

例8:查询student表中学号为1001到1004的记录。

select * from student where stuID between 1001 and 1004 ;

例9:查询student表中学号不包含1001到1004的记录。

select * from student where stuID not between 1001 and 1004 ;

例9:查询student表中所有姓张的同学的信息。

select * from student where stuName like'张%';

例10:查询student表中姓名包含“丽”字的同学的信息。

select * from student where stuName like'%丽%';

例11:分别查询student表中姓名包含“五”字的同学的信息。

select * from student where stuName like'%五%';

例12:查询student表中的记录,根据年龄进行升序排列,降序排列。

select * from student order by stuAge;

select * from student order by stuAge desc;

例13:查询student表中的记录,根据年龄进行升序排列,姓名进行降序排列。

select * from student order by stuAge ,stuName desc;

例14:使用聚合函数查询学生总人数。

select count(stuID) from student;

例15:使用聚合函数查询课程表中所有课程的学分和。

select sum(couCredit) from courses;

例16:分别使用聚合函数查询年龄最大的学生和年龄最小的学生的年龄。

select max(stuAge),min(stuAge) from student;

例17:使用聚合函数查询课程表中所有课程的平均学时数。

select avg(couHours) from courses;

例18:使用分组查询输出学生性别。

select stuSex from student group by stuSex;

例20:分组查询学生表中男女同学的人数。

select stuSex,count(stuID) from student group by stuSex ;

例21:分组查询学生表中男女同学的平均年龄,人数,男女同学年龄最大值,年龄最小值。

select stuSex,count(stuID),avg(stuAge),max(stuAge),min(stuAge) fromstudent group by stuSex ;

例22:分组查询学生表中男女同学各年龄段的人数。

select stuSex,stuAge, COUNT(stuID) from student GROUP BY stuSex,stuAgee;

例25:查询学生表中从第2条记录开始的3条记录。

select * from student limit 2,3;

例25:查询学生表中的前4条记录。

select * from student limit 4;

MySQL多表查询练习

二、题目一

1、查询所有的课程的名称以及对应的任课老师姓名

select b.cname , a.tname from teacher a, course b where a.tid=b.teacher_id;

2、查询学生表中男女生各有多少人

select gender , count(sid) from student group by gender;

3、查询物理成绩等于100的学生的姓名

select a.sname ,b.course_id from student a , score b where a.sid=b.sid and b.course_id=2 and b.num=100;

4、查询平均成绩大于八十分的同学的姓名和平均成绩

select a.sname , temp.avge from student a , (select avg(num) avge ,

student_id from score group by student_id) temp where a.sid=temp.student_id and temp.avge>80;

5、查询所有学生的学号,姓名,选课数,总成绩

select a.sid ,a.sname , temp.num , temp.course from student a , (selectlect sum(num) num , count(course_id) course,student_id from score group bystudent_id) temp where a.sid=temp.student_id;

6、 查询姓李老师的个数

select count(tname) num from teacher where tname like '李%';

7、 查询没有报李平老师课的学生姓名

select distinct a.sname from student a , score b ,teacher c where a.sid=b.stsid=b.student_id and b.course_id=c.tid and c.tname not like '李平';

8、 查询物理课程比生物课程高的学生的学号

select a.student_id from (select num ,student_id from score where course_id=1) a , (select student_id,num from score where course_id=2) b wheree a.student_id=b.student_id and a.num<b.num ;

9、 查询没有同时选修物理课程和体育课程的学生姓名

select distinct a.student_id,c.sname from score a , course b ,studentt c where b.cname not like'体育' and b.cname not like'物理' and a.course_id==b.cid and a.student_id=c.sid;

10、查询挂科超过两门(包括两门)的学生姓名和班级

select a.sname from (select temp.student_id ssid,count(temp.student_id)id) from (select student_id,num from score where num>60) temp group by temp..student_id having count(temp.student_id)<=2) b , student a where b.ssid=a.s

sid;

11、查询选修了所有课程的学生姓名

select a.sname from (select temp.student_id ssid,count(temp.course_id) from (select student_id,course_id from score) temp group by temp.student_id having count(temp.course_id)=4) b , student a where b.ssid=a.sid;

12、查询李平老师教的课程的所有成绩记录

select a.num from (select a.cid cid from course a , teacher b where

b.tname='李平老师' and b.tid=a.teacher_id) temp, score a where a.course_id=ttemp.cid;

14、查询每门课程被选修的次数

select course_id , count(course_id) from score group by course_id;

15、查询之选修了一门课程的学生姓名和学号

select a.sname ,a.sid from (select temp.student_id ssid,count(temp.course_id) from (select student_id,course_id from score) temp group by temp.student_id having count(temp.course_id)=1) b , student a where b.ssid=a.sid;

16、查询所有学生考出的成绩并按从高到低排序(成绩去重)

select distinct num from score order by num desc;

17、查询平均成绩大于85的学生姓名和平均成绩

select a.sname ,temp.avg from student a ,(select student_id,avg(num) avg from score group by student_id) temp where a.sid=temp.student_id and temp.avg>85;

18、查询生物成绩不及格的学生姓名和对应生物分数

select b.sname ,a.student_id ,a.num from score a,student b ,coursec where a.course_id=c.cid and c.cname='生物' and a.num<60 and a.student_id=b

b.sid;

1. 查询每个雇员的所有记录;

Select * from Employee;

2. 查询前5个会员的所有记录;

Select * from Employee limit 5;

3. 查询每个雇员的地址和电话;

Select addr,tel from Employee ;

4. 查询num为001的雇员地址和电话;

Select addr,tel from Employee where num=1 ;

5. 查询表Employee表中女雇员的地址和电话,使用AS子句将结果列中各列的标题分别指定为地址、电话;

Select addr as ‘地址’,tel as ‘电话’ from Employee where sex=’女’ ;

6. 计算每个雇员的实际收入;

select inCome-outCome from salay;

7. 找出所有性王的雇员的部门号(部门号不能重复显示);

select distinct depno from Employee where name like '王%';

8. 找出所有收入在2000-3000元之间的雇员编号

select num from salay where (inCome-outCome) between 2000 and 3000;

练习2:子查询的使用(答案可以不唯一)

1. 查找在财务部工作的雇员情况;

select a.* from Employee a , department b where b.depName='财务部' and a.depno=b.depno;

2. 查找在财务部且年龄不低于研发部任一个雇员年龄的雇员的姓名;

select a.name from Employee a , department b ,(select min(birth) minfrom Employee , department b where Employee.depno=b.depno and b.depName='.发部') c where b.depName='财务部' and a.depno=b.depno and a.birth<c.min;

练习3:连接查询的使用

1. 查找每个雇员的情况及薪水情况;

select a.* , b.inCome from Employee a , salay b where a.num=b.num;

2. 查找财务部收入在2200元以上的雇员姓名及其薪水详细情况;

select a.name , b.inCome from Employee a , salay b ,department c where a.depno=c.depno and c.depName='财务部' and a.num=b.num and b.inCome>2200;

练习4:数据汇总

1. 求财务部雇员的平均实际收入;

select avg(b.inCome) from Employee a , salay b ,department c where a.depnodepno=c.depno and c.depName='财务部' and a.num=b.num ;

2. 求财务部雇员的总人数;

select count(a.name) from Employee a , salay b ,department c where a.depno=c.depno=c.depno and c.depName='财务部' and a.num=b.num ;

1. 求各部门的雇员数(要求显示,部门号、部门名称和部门雇员数);

select c.depno,c.depName,count(a.depno) from Employee a ,department c where a.depno=c.depno group by c.depno;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值