排名
5
文章
229
粉丝
15
评论
7
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:
50010702506256


欢迎加群交流技术

代码如下:
-- 查询年龄小于30岁的学生姓名,年龄,职位,班级
-- 查询拥有学生的班级id和班级名称
select distinct c.id, c.ClassName from students s inner join class c on s.classid = c.Id
-- 查询班级与班级所拥有的学生人数
select class.ClassName,count(*) as '人数'
from students inner join class on students.classid = class.Id group by class.ClassName
-- 方法2:子查询
select classname,(select count(*) from students where classid = class.Id) as '人数' from class
-- 查询年龄大于40岁的学生,以及他的班级名称,如果该学生没有班级,则显示"休学中" (一个left join与函数太简单了)
/*
查询所有学生的薪资等级
可以先分析连接条件:薪水比最低高,比最高少即可也就是>=low and <=hight
*/
-- 方法1:显示内连接
select s.UserName,c.grade from students s ,salaryGrade c where s.salary BETWEEN c.losal and c.hisal
-- 方法2:隐式内连接
select s.UserName,c.grade,c.losal,c.hisal from students s join salaryGrade c on s.salary BETWEEN c.losal and c.hisal
/* 查询2班学生的薪水等级(相当于三表连查) */
-- 方法1:直接join三张表
select s.UserName,c.grade,c.losal,c.hisal from students s
join salaryGrade c on s.salary BETWEEN c.losal and c.hisal
join class on s.classid = class.Id where class.ClassName = '计网1212'
-- 方法2:先子查询出来2班学生在join薪水等级表
/* 查询3班学生的平均薪水与最高薪水 */
/* 查询2班中工资比紫嫣高的学生 */
/* 查询3班中工资高于学生平均薪水的 */
/* 查询低于本班级平均薪水的学生 */
-- a:先查询某个部门的平均薪资
select avg(salary) from students where classid = 2
-- b:查询这个部门低于平均薪资的(比如这里的1部门低于平均薪资的人)
select * from students where classid = 1 and
salary < (select avg(salary) from students where classid = 1)
-- c:把写死的部门换成活的。(写完后可以执行第一句验证一下这些是不是都是低于本部门平均薪资的)
select * from students s2 where
salary < (select avg(salary) from students s1 where s1.classid = s2.classid)
/* 查询学生选择的课程,返回学生名称,课程名称 。 需要增加一个课程表 */
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739。有需要软件开发,或者学习软件技术的朋友可以和我联系~(Q:815170684)
评价