diff --git "a/10 \347\250\213\346\231\264/.keep" "b/10 \347\250\213\346\231\264/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/10 \347\250\213\346\231\264/10/lenovo20230217214915.png" "b/10 \347\250\213\346\231\264/10/lenovo20230217214915.png" new file mode 100644 index 0000000000000000000000000000000000000000..2a1af1b26fadda23a2bb3ef3da8453f12934f988 Binary files /dev/null and "b/10 \347\250\213\346\231\264/10/lenovo20230217214915.png" differ diff --git "a/10 \347\250\213\346\231\264/50\351\242\230\347\233\256.sql" "b/10 \347\250\213\346\231\264/50\351\242\230\347\233\256.sql" new file mode 100644 index 0000000000000000000000000000000000000000..83f651560e1c86a5a13d4deca43ab3080aea5db3 --- /dev/null +++ "b/10 \347\250\213\346\231\264/50\351\242\230\347\233\256.sql" @@ -0,0 +1,447 @@ +create database ClassicDb +GO +drop database ClassicDb; + +use ClassicDb +GO + +create table StudentInfo +( + Id int PRIMARY key not null IDENTITY, + StudentCode nvarchar(80), + StudentName nvarchar(80), + Birthday date not null, + Sex nvarchar(2), + ClassId int not null +) + +GO + +-- select * from StudentInfo + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + +GO + + +CREATE TABLE Teachers +( + Id int PRIMARY key not null IDENTITY, + TeacherName nvarchar(80) +) + +go +-- select * from Teachers + +insert into Teachers (TeacherName) values('张三') +insert into Teachers (TeacherName) values('李四') +insert into Teachers (TeacherName) values('王五') + +GO + +create table CourseInfo +( + Id int PRIMARY key not null IDENTITY, + CourseName NVARCHAR(80) not null, + TeacherId int not null +) + +go +-- select * from CourseInfo + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + +GO + +create table StudentCourseScore +( + Id int PRIMARY key IDENTITY, + StudentId int , + CourseId int , + Score int +) +go +-- select * from StudentCourseScore + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) + +go + +--1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +select* from StudentInfo join (select studentid,score 语文 from StudentCourseScore where courseid=1) 语文 +on StudentInfo.Id=语文.StudentId +join (select studentid,score 数学 from StudentCourseScore where courseid=2) 数学 +on 语文.StudentId=数学.StudentId +where 数学.数学>语文.语文 + + +--1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +select studentinfo.Id 学生编号,studentname 学生姓名 ,语文,数学 from StudentInfo +join (select studentid,CourseId 语文 from StudentCourseScore where courseid=1) 语文 +on StudentInfo.Id =语文.StudentId +join (select studentid,CourseId 数学 from StudentCourseScore where courseid=2) 数学 +on 语文.StudentId=数学.StudentId + +--1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) +select studentinfo.Id 学生编号,studentname 学生姓名 ,语文,数学 from StudentInfo +left join (select studentid,CourseId 语文 from StudentCourseScore where courseid=1) 语文 +on StudentInfo.Id =语文.StudentId + join (select studentid,CourseId 数学 from StudentCourseScore where courseid=2) 数学 +on 语文.StudentId=数学.StudentId + + +--1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select studentinfo.Id 学生编号,studentname 学生姓名 ,语文,数学 from StudentInfo + join (select studentid,CourseId 语文 from StudentCourseScore where courseid=1) 语文 +on StudentInfo.Id =语文.StudentId +left join (select studentid,CourseId 数学 from StudentCourseScore where courseid=2) 数学 +on 语文.StudentId=数学.StudentId + + +--2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select studentcode,studentname,avg(score) from StudentCourseScore join StudentInfo on StudentCourseScore.StudentId=StudentInfo.StudentCode +group by StudentInfo.StudentCode,StudentName +having avg(score)>=60 + + +--3.查询在 成绩 表存在成绩的学生信息 +select StudentCode,StudentName,Birthday,Sex,ClassId from StudentCourseScore join StudentInfo on StudentInfo.studentcode=studentcoursescore.StudentId +where StudentCourseScore.Score is not null +--4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) + +select studentinfo.Id ,studentname 学生姓名, 总成绩 from StudentInfo left join +(select studentid 学生编号,count(courseid) 选课总数,sum(score) 总成绩 +from StudentCourseScore group by StudentId) 总成绩 +on StudentInfo.Id=总成绩.学生编号 + +--4.1 查有成绩的学生信息 + +select * from StudentInfo +join (select StudentId 学生编号,CourseId 课程编号,Score 成绩 from StudentCourseScore ) 成绩 +on StudentInfo.Id=成绩.学生编号 + +--5.查询「李」姓老师的数量 + +select count(teacherid) 李姓老师数量 from CourseInfo +join (select * from Teachers where TeacherName like '%李%') tea +on CourseInfo.TeacherId=tea.Id + +--6.查询学过「张三」老师授课的同学的信息 + +select StudentInfo.*,zs,老师编号 from StudentInfo join StudentCourseScore +on StudentInfo.Id=StudentCourseScore.StudentId +join (select Teacherid 老师编号,id 课程编号 from CourseInfo where Id=2) kc +on kc.课程编号=StudentCourseScore.CourseId +join (select id,TeacherName zs from Teachers where TeacherName like '%张三%') zs +on kc.老师编号=zs.Id + +select * from CourseInfo + +--7.查询没有学全所有课程的同学的信息 +select * from StudentInfo where id not in( +select StudentId from StudentCourseScore group by StudentId +having count(*)=(select count(*) from CourseInfo ) +) + +--8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +--方案1: +select distinct StudentInfo.* from StudentInfo +inner join StudentCourseScore on StudentInfo.id=StudentCourseScore.StudentId +where StudentCourseScore.CourseId in (select CourseId from StudentCourseScore where StudentId=1) +and StudentInfo.id!=1 + +--方案二: +select StudentInfo.* from StudentInfo where id in( +select StudentId from StudentCourseScore where CourseId= any(select CourseId from StudentCourseScore where StudentId=1) +and StudentId!=1 +) + +--9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 + +select * from StudentInfo where id in +( +--课程与'01'学生课程完全相同 +select StudentId from +--课程编号与01选修的课程相同的成绩信息结果集 +(select * from StudentCourseScore where CourseId in +( + --学号为01的学生选修的课程编号 + select CourseId from StudentCourseScore where StudentId=1 +)) b +group by StudentId --根据学号分组统计选修的课程数,课程数与01课程数相同 +having count(*)=(select count(*) from StudentCourseScore where StudentId=1) +intersect +--选修课程数等于01编号选修的课程数 +select StudentId from StudentCourseScore group by StudentId +having COUNT(*)=(select COUNT(*) from StudentCourseScore where StudentId=1) +) +and id!=1 + + +--10.查询没学过"张三"老师讲授的任一门课程的学生姓名 +select StudentName from StudentInfo where Id not in +( + select StudentCourseScore.StudentId from StudentCourseScore + inner join CourseInfo on StudentCourseScore.CourseId=CourseInfo.id + inner join Teachers on Teachers.Id=CourseInfo.TeacherId + where Teachers.TeacherName='张三' +) + +--11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 + +select StudentId 学号,StudentName 姓名,avg(Score) 平均分 from StudentInfo +inner join StudentCourseScore +on StudentCourseScore.StudentId=StudentInfo.Id +where Score<60 +group by StudentId,StudentName having count(*)>=2 + +--12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 +select * from StudentInfo +inner join StudentCourseScore on StudentInfo.id= StudentCourseScore.StudentId +inner join CourseInfo on StudentCourseScore.CourseId=CourseInfo.Id +where CourseName='数学' and Score<60 +order by Score + +--13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 +select a.*,avgs 平均成绩 from StudentCourseScore a +inner join (select StudentId,avg(Score) avgs from StudentCourseScore group by StudentId) b +on a.StudentId=b.StudentId +order by b.avgs desc + + +--14.查询各科成绩最高分、最低分和平均分: +select CourseName 课程名称,max(Score) 最高分,min(Score) 最低分,avg(Score) 平均分 from StudentCourseScore +inner join CourseInfo on CourseInfo.id=StudentCourseScore.CourseId +group by CourseName + + +--15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 +--/* +-- 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + +-- 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + +--*/ + select CourseId 课程号,count(*) 选修人数,max(Score) 最高分,min(Score) 最低分,avg(Score) 平均分, + round(sum(及格)/CONVERT(decimal(5,2),count(*)),2) 及格率,round(sum(中等)/CONVERT(decimal(5,2),count(*)),2) 中等率,round(sum(优良)/CONVERT(decimal(5,2),count(*)),2) 优良率,round(sum(优秀)/CONVERT(decimal(5,2),count(*)),2) 优秀率 from ( + select *,(case when Score>=60 then 1 else 0 end ) 及格, + case when Score between 70 and 80 then 1 else 0 end 中等, + case when Score between 80 and 90 then 1 else 0 end 优良, + case when Score>=90 then 1 else 0 end 优秀 + from StudentCourseScore) as a + group by CourseId + +--15.1按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 +select *,rank() over(partition by courseid order by score desc) 排名 from StudentCourseScore + +--15.2 按各科成绩进行排序,并显示排名, Score 重复时合并名次 +select *,dense_rank() over(partition by courseid order by score desc) 排名 from StudentCourseScore + +--16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 +select StudentId,sum(Score) 总分,rank() over(order by sum(score) desc) 排名 from StudentCourseScore group by StudentId + +--16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 +select StudentId,sum(Score) 总分,dense_rank() over(order by sum(score) desc) 排名 from StudentCourseScore group by StudentId +--17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 +select CourseId,CourseName,sum([100-85]) '[100-85]人数',convert(decimal(5,2),sum([100-85])/count(StudentId)) '[100-85]百分比',sum([85-70]) '[85-70]人数',sum([85-70])/count(StudentId) '[85-70]百分比',sum([70-60]) '[70-60]人数',sum([70-60])/count(StudentId) '[70-60]百分比',sum([60-0]) '[60-0]人数',sum([60-0])/count(StudentId) '[60-0]百分比' +from +(select *,case when Score between 85 and 100 then 1 else 0 end [100-85], + case when Score between 70 and 85 then 1 else 0 end [85-70], + case when Score between 60 and 70 then 1 else 0 end [70-60], + case when Score between 0 and 60 then 1 else 0 end [60-0] +from StudentCourseScore) as a +inner join CourseInfo on CourseInfo.Id=a.CourseId +group by a.CourseId,CourseInfo.CourseName + + +--18.查询各科成绩前三名的记录 +select * from +(select *,RANK() over(partition by courseid order by score) as 排名 from StudentCourseScore) a +where a.排名<=3 + +--19.查询每门课程被选修的学生数 +select CourseId,CourseName,count(Score) 选修学生数 from CourseInfo +left join StudentCourseScore on StudentCourseScore.CourseId=CourseInfo.Id +group by CourseId,CourseName +order by CourseId + + +--20.查询出只选修两门课程的学生学号和姓名 +select * from StudentInfo where id in( +select StudentId from StudentCourseScore +group by StudentId +having count(Score)=2 +) +--21.查询男生、女生人数 +select Sex,COUNT(*) 人数 from StudentInfo +group by Sex + +--22.查询名字中含有「风」字的学生信息 +select * from StudentInfo where StudentName like '%风%' + +--23.查询同名同姓学生名单,并统计同名人数 +select StudentName,count(*) 同名人数 from StudentInfo +group by StudentName +having count(*)>=2 + +--24.查询 1990 年出生的学生名单 +select * from StudentInfo where Birthday like '1990%' + +--25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 +select CourseId,CourseName,AVG(Score) 平均成绩 from StudentCourseScore a +inner join CourseInfo on CourseInfo.Id=a.CourseId +group by a.CourseId,CourseName +order by AVG(Score) desc,CourseId + + +--26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 +select StudentId 学号,StudentName 姓名,avg(Score) from StudentInfo a +inner join StudentCourseScore b on b.StudentId=a.Id +group by StudentId,StudentName +having avg(Score)>=85 + +--27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 +select * from StudentInfo +inner join StudentCourseScore on StudentInfo.id=StudentCourseScore.StudentId +inner join CourseInfo on CourseInfo.id=StudentCourseScore.CourseId +where CourseName='数学' and Score<60 +select * from StudentCourseScore where CourseId=2 +select * from CourseInfo + +--28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) +select a.*,CourseName,Score from StudentInfo a +left join StudentCourseScore b on a.Id=b.StudentId +left join CourseInfo c on c.Id=b.CourseId + +--29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 +select StudentName,CourseName,Score from StudentInfo a +left join StudentCourseScore b on a.Id=b.StudentId +left join CourseInfo c on c.Id=b.CourseId +where Score>70 + +--30.查询不及格的课程 +select distinct CourseName from CourseInfo +inner join StudentCourseScore on CourseInfo.Id=StudentCourseScore.CourseId + +--31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 +select * from StudentInfo +inner join StudentCourseScore on StudentInfo.id=StudentCourseScore.StudentId +where CourseId=1 and Score>=80 + +--32.求每门课程的学生人数 +select CourseId,CourseName,count(*) 选修人数 from StudentCourseScore +inner join CourseInfo on CourseInfo.Id=StudentCourseScore.CourseId +group by CourseId,CourseName + +--33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 +select top 1 StudentInfo.*,Score from StudentInfo +inner join StudentCourseScore on StudentInfo.Id=StudentCourseScore.StudentId +inner join CourseInfo on CourseInfo.Id=StudentCourseScore.CourseId +inner join Teachers on CourseInfo.TeacherId=Teachers.Id +where TeacherName='张三' +order by Score desc + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 +select StudentInfo.*,CourseId,Score from StudentInfo +inner join StudentCourseScore on StudentInfo.Id=StudentCourseScore.StudentId +inner join CourseInfo on CourseInfo.Id=StudentCourseScore.CourseId +inner join Teachers on CourseInfo.TeacherId=Teachers.Id +where TeacherName='张三' and Score in( +select max(Score) from StudentCourseScore +inner join CourseInfo on CourseInfo.Id=StudentCourseScore.CourseId +inner join Teachers on CourseInfo.TeacherId=Teachers.Id +where TeacherName='张三' +) + + +--35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 +select distinct a.StudentId 学生编号,a.CourseId 课程编号,a.Score from StudentCourseScore a +inner join StudentCourseScore b on a.StudentId=b.StudentId +where a.Score=b.Score and a.CourseId!=b.CourseId + +--36.查询每门功成绩最好的前两名 +select * from +(select *,rank() over(partition by courseId order by score desc) [rank] from StudentCourseScore) a +where a.[rank]<=2 + + +--37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 +select CourseId,count(StudentId) 选修人数 from StudentCourseScore +group by CourseId +having count(StudentId)>5 + +--38.检索至少选修两门课程的学生学号 +select StudentId from StudentCourseScore +group by StudentId +having count(Score)>=2 + +--39.查询选修了全部课程的学生信息 +select StudentId from StudentCourseScore +group by StudentId +having count(Score)=(select COUNT(*) from CourseInfo) + +--40.查询各学生的年龄,只按年份来算 +select *,datediff(year,Birthday,GETDATE()) age from StudentInfo + +--41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 +select *, +case + when dateadd(year,datediff(year,Birthday,GETDATE()),birthday)80 \ No newline at end of file diff --git "a/10 \347\250\213\346\231\264/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/SQLQuery4.sql" "b/10 \347\250\213\346\231\264/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..78482c6775400655f38e3b3839eef16e3aafcc31 --- /dev/null +++ "b/10 \347\250\213\346\231\264/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/SQLQuery4.sql" @@ -0,0 +1,118 @@ +create database STUDENT ; +go +use STUDENT; +go +create table ORDERS ( +orderId int not null primary key identity , +orderdate nvarchar(50) not null +); + +create table ORDERITEM ( +itemid int not null identity , +orderid int not null , +itemtype nvarchar(50) not null , +itemname nvarchar(50) not null , +thenumber int not null , +themoney int not null ); + +insert into orders(orderdate) +create database LSY +go +use LSY +go +create table stuinfo( +stuNO nvarchar(50) primary key, +stuName nvarchar(50), +stuAge tinyint, +stuAddress nvarchar(50), +stuSeat int identity, +stuSex int +) + +create table stuexam( +examNO int primary key identity, +stuNO nvarchar(50) references stuinfo(stuNO), +writtenExam int, +labExam int +) + +insert into stuinfo(stuNO,stuName,stuAge,stuAddress,stuSex) +values('s2501','张秋利',20,'美国硅谷',1), + ('s2502','李斯文',18,'湖北武汉',0), + ('s2503','马文才',22,'湖南长沙',1), + ('s2504','欧阳俊雄',21,'湖北武汉',0), + ('s2505','梅超风',20,'湖北武汉',1), + ('s2506','陈旋风',19,'美国硅谷',1), + ('s2507','陈风',20,'美国硅谷',0) + +insert into stuexam(stuNO,writtenExam,labExam) +values ('s2501',50,70), + ('s2502',60,65), + ('s2503',86,85), + ('s2504',40,80), + ('s2505',70,90), + ('s2506',85,90) + +---1.查询学生信息表(stuinfo)中所有列信息,给每列取上中文名称 +select stuNO 学号,stuName 姓名,stuAge 年龄,stuAddress 地址,stuSeat 座号,stuSex 性别 from stuinfo +--2.查询学生信息表(stuinfo)中的姓名,年龄和地址三列的信息 +select stuName 姓名,stuAge 年龄,stuAddress 地址 from stuinfo +--3.查询学生分数表(stuexam)中的学号,笔试和机试三列的信息,并为这三列取中文名字 +-- 注意:要用三种方法 +select stuNO 学号,writtenExam 笔试,labExam 机试 from stuexam +select stuNO '学号',writtenExam '笔试',labExam '机试' from stuexam +select stuNO as 学号,writtenExam as 笔试,labExam as 机试 from stuexam +--4.查询学生信息表(stuInfo)中的学号,姓名,地址,以及将:姓名+@+地址 组成新列 “邮箱” +select stuNO 学号,stuName 姓名,stuAddress 地址, stuNO+'@'+stuAddress 邮箱 from stuinfo +--5.查询学生分数表(stuexam)中的学生的学号,笔试,机试以及总分这四列的信息 +select stuNO 学号,writtenExam 笔试,labExam 机试,writtenExam+labExam 总分 from stuexam +--6.查询学生信息表(stuInfo)中学生来自哪几个地方 +select distinct stuAddress 地址 from stuinfo +--7.查询学生信息表(stuInfo)中学生有哪几种年龄,并为该列取对应的中文列名 +select distinct stuAge 年龄 from stuinfo +--8.查询学生信息表(stuInfo)中前3行记录 +select top 3 * from stuinfo +--9.查询学生信息表(stuInfo)中前4个学生的姓名和座位号 +select top 4 stuName 姓名,stuSeat 座位号 from stuinfo +--10.查询学生信息表(stuInfo)中一半学生的信息 +select top 50 percent * from stuinfo +--11.将地址是湖北武汉,年龄是20的学生的所有信息查询出来 +select * from stuinfo where stuAddress like '湖北武汉' and stuAge like 20 +--12.将机试成绩在60-80之间的信息查询出来,并按照机试成绩降序排列(用两种方法实现) +select*from stuexam where labExam between 60 and 80 order by labExam desc +select*from stuexam where labExam>=60 and labExam<=80 order by labExam desc +--13.查询来自湖北武汉或者湖南长沙的学生的所有信息(用两种方法实现) +select*from stuinfo where stuAddress like '湖北武汉' or stuAddress like '湖南长沙' +select*from stuinfo where stuAddress = '湖北武汉' or stuAddress = '湖南长沙' +--14.查询出笔试成绩不在70-90之间的信息,并按照笔试成绩升序排列(用两种方法实现) +select writtenExam 笔试 from stuexam where not(writtenExam>=70 and writtenExam<=90) order by writtenExam asc +select writtenExam 笔试 from stuexam where not(writtenExam between 70 and 90) order by writtenExam asc + +--15.查询年龄没有写的学生所有信息 +select * from stuinfo where stuAge='' +select * from stuinfo where stuAge is null +--16.查询年龄写了的学生所有信息 +select * from stuinfo where stuAge !='' +select * from stuinfo where not(stuAge is null) +--17.查询姓张的学生信息 +select * from stuinfo where stuName like '张%' +--18.查询学生地址中有‘湖’字的信息 +select * from stuinfo where stuAddress like '%湖%' +--19.查询姓张但名为一个字的学生信息 +select * from stuinfo where stuName like '张_' +--20.查询姓名中第三个字为‘俊’的学生的信息,‘俊’后面有多少个字不限制 +select * from stuinfo where stuName like '__俊%' +--21.按学生的年龄降序显示所有学生信息 +select * from stuinfo order by stuAge desc +--22.按学生的年龄降序和座位号升序来显示所有学生的信息 +select * from stuinfo order by stuAge desc , stuSeat asc +--23.显示笔试第一名的学生的考试号,学号,笔试成绩和机试成绩 +select top 1 * from stuexam order by writtenExam desc +--24.显示机试倒数第一名的学生的考试号,学号,笔试成绩和机试成绩 +select top 1 * from stuexam order by labExam asc +--25.查询每个地方的学生的平均年龄 +select stuAddress, avg(stuAge) 平均年龄 from stuinfo group by stuAddress +--26.查询男女生的分别的年龄总和 +select stuSex , sum(stuAge) 年龄总和 from stuinfo group by stuSex +--27.查询每个地方的男女生的平均年龄和年龄的总和 +select stuAddress 地址,stuSex 性别, avg(stuAge) 平均年龄, sum(stuAge)年龄总和 from stuinfo group by stuAddress,stuSex \ No newline at end of file diff --git "a/10 \347\250\213\346\231\264/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/\346\220\234\347\213\227\346\210\252\345\233\27620230302103035.png" "b/10 \347\250\213\346\231\264/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/\346\220\234\347\213\227\346\210\252\345\233\27620230302103035.png" new file mode 100644 index 0000000000000000000000000000000000000000..fd2568185c3aa0bfff2c981e568d597cc2a7fbc1 Binary files /dev/null and "b/10 \347\250\213\346\231\264/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/\346\220\234\347\213\227\346\210\252\345\233\27620230302103035.png" differ diff --git "a/10 \347\250\213\346\231\264/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/\346\220\234\347\213\227\346\210\252\345\233\27620230302103112.png" "b/10 \347\250\213\346\231\264/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/\346\220\234\347\213\227\346\210\252\345\233\27620230302103112.png" new file mode 100644 index 0000000000000000000000000000000000000000..c5a2267ce30efcc986ebd2e1e771021072aee3fd Binary files /dev/null and "b/10 \347\250\213\346\231\264/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/\346\220\234\347\213\227\346\210\252\345\233\27620230302103112.png" differ diff --git "a/10 \347\250\213\346\231\264/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/\346\220\234\347\213\227\346\210\252\345\233\27620230302103122.png" "b/10 \347\250\213\346\231\264/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/\346\220\234\347\213\227\346\210\252\345\233\27620230302103122.png" new file mode 100644 index 0000000000000000000000000000000000000000..f38b4b1908fc00ab7d2eb1888c3c861d6be5184b Binary files /dev/null and "b/10 \347\250\213\346\231\264/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/\346\220\234\347\213\227\346\210\252\345\233\27620230302103122.png" differ diff --git "a/10 \347\250\213\346\231\264/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/SQLQuery3.sql" "b/10 \347\250\213\346\231\264/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d177c3d6260996120c11709db9f8631388c66a67 --- /dev/null +++ "b/10 \347\250\213\346\231\264/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/SQLQuery3.sql" @@ -0,0 +1,76 @@ +create database STUDENT ; +go +use STUDENT; +go +create table ORDERS ( +orderId int not null primary key identity , +orderdate nvarchar(50) not null +); + +create table ORDERITEM ( +itemid int not null identity , +orderid int not null , +itemtype nvarchar(50) not null , +itemname nvarchar(50) not null , +thenumber int not null , +themoney int not null ); + +insert into orders(orderdate) +values('2008-01-12 00:00:00.000'), + ('2008-02-10 00:00:00.000'), + ('2008-02-15 00:00:00.000'), + ('2008-03-10 00:00:00.000') +select * from ORDERS + +insert into orderitem(orderid,itemtype,itemname,thenumber,themoney) +values(1,'文具','笔',72,2), + (1,'文具','尺',10,1), + (1,'体育用品','篮球',1,56), + (2,'文具','笔',36,2), + (2,'文具','固体胶',20,3), + (2,'日常用品','透明胶',2,1), + (3,'体育用品','羽毛球',20,3), + (3,'文具','订书机',20,3), + (3,'文具','订书针',10,3), + (4,'文具','裁纸刀',5,5), + (4,'文具','笔',20,2), + (4,'文具','信纸',50,1), + (4,'日常用品','毛巾',4,5), + (4,'日常用品','透明胶',30,1), + (4,'体育用品','羽毛球',20,3) +select * from ORDERITEM +--1.查询所有订单订购的所有物品数量总和 +select * from ORDERITEM; +select sum(thenumber) as 总数 from ORDERITEM ; + +--2.查询订单编号小于3的,平均单价小于10 每个订单订购的所有物品的数量和以及平均单价 +select sum(thenumber) as 总数 , avg(themoney) as 平均单价 from ORDERITEM +where orderid < 3 +having avg(themoney) <10; + +--3.查询平均单价小于10并且总数量大于 50 每个订单订购的所有物品数量和以及平均单价 +select sum(thenumber) as 总数 , avg(themoney) as 平均单价 from ORDERITEM +where thenumber>50 +having avg(themoney) <10 ; + +--4.查询每种类别的产品分别订购了几次,例如: +-- 文具 9 +-- 体育用品 3 +-- 日常用品 3 +select count(0) as '文具' from ORDERITEM where itemtype='文具'; +select count(0) as '体育用品' from ORDERITEM where itemtype='体育用品'; +select count(0) as '日常用品' from ORDERITEM where itemtype='日常用品'; +select *from ORDERITEM; + +--5.查询每种类别的产品的订购总数量在100以上的订购总数量和平均单价 +select itemtype ,sum(thenumber) as 数量 ,avg(themoney)as 平均单价 from ORDERITEM +group by itemtype +having sum(thenumber)>100 ; + +--6.查询每种产品的订购次数,订购总数量和订购的平均单价,例如: + +-- 产品名称 订购次数 总数量 平均单价 +-- 笔 3 120 2 +select* from ORDERITEM ; +select itemname ,count(0) as次数 ,sum(thenumber) as 总数量 , avg(themoney) as 平均单价 from ORDERITEM +group by itemname ; \ No newline at end of file diff --git "a/10 \347\250\213\346\231\264/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\346\220\234\347\213\227\346\210\252\345\233\27620230306192202.png" "b/10 \347\250\213\346\231\264/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\346\220\234\347\213\227\346\210\252\345\233\27620230306192202.png" new file mode 100644 index 0000000000000000000000000000000000000000..33fd8d47ce9d7d9df30990deeab11e906501ee06 Binary files /dev/null and "b/10 \347\250\213\346\231\264/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\346\220\234\347\213\227\346\210\252\345\233\27620230306192202.png" differ diff --git "a/10 \347\250\213\346\231\264/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/\346\220\234\347\213\227\346\210\252\345\233\27620230302103237.png" "b/10 \347\250\213\346\231\264/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/\346\220\234\347\213\227\346\210\252\345\233\27620230302103237.png" new file mode 100644 index 0000000000000000000000000000000000000000..12124e22f68aad96220e5bdf66984a4b0696ec25 Binary files /dev/null and "b/10 \347\250\213\346\231\264/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/\346\220\234\347\213\227\346\210\252\345\233\27620230302103237.png" differ diff --git "a/10 \347\250\213\346\231\264/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/\346\220\234\347\213\227\346\210\252\345\233\27620230302103303.png" "b/10 \347\250\213\346\231\264/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/\346\220\234\347\213\227\346\210\252\345\233\27620230302103303.png" new file mode 100644 index 0000000000000000000000000000000000000000..9ab5b59412db31c5f3ede58813dddbe0fbe4b3f1 Binary files /dev/null and "b/10 \347\250\213\346\231\264/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/\346\220\234\347\213\227\346\210\252\345\233\27620230302103303.png" differ diff --git "a/10 \347\250\213\346\231\264/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/\346\220\234\347\213\227\346\210\252\345\233\27620230302103310.png" "b/10 \347\250\213\346\231\264/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/\346\220\234\347\213\227\346\210\252\345\233\27620230302103310.png" new file mode 100644 index 0000000000000000000000000000000000000000..563329a8ebe94e12e07f9d5144b0b33a3c9f8d0a Binary files /dev/null and "b/10 \347\250\213\346\231\264/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/\346\220\234\347\213\227\346\210\252\345\233\27620230302103310.png" differ diff --git "a/10 \347\250\213\346\231\264/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/.keep" "b/10 \347\250\213\346\231\264/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/10 \347\250\213\346\231\264/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\347\273\203\344\271\2401.sql" "b/10 \347\250\213\346\231\264/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\347\273\203\344\271\2401.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7a11d39df7c1383c27bbd46b8431810379421393 --- /dev/null +++ "b/10 \347\250\213\346\231\264/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\232/\347\273\203\344\271\2401.sql" @@ -0,0 +1,73 @@ +create database llon +go +use llon +go +create table orders( +orderid int primary key identity, +orderdate nvarchar(50) +) + +create table orderitem( +itemid int primary key identity, +orderid int, +itemtype nvarchar(50), +itemname nvarchar(50), +thenumber int, +themoney int +) + +insert into orders(orderdate) +values('2008-01-12 00:00:00.000'), + ('2008-02-10 00:00:00.000'), + ('2008-02-15 00:00:00.000'), + ('2008-03-10 00:00:00.000') + +insert into orderitem(orderid,itemtype,itemname,thenumber,themoney) +values(1,'文具','笔',72,2), + (1,'文具','尺',10,1), + (1,'体育用品','篮球',1,56), + (2,'文具','笔',36,2), + (2,'文具','固体胶',20,3), + (2,'日常用品','透明胶',2,1), + (3,'体育用品','羽毛球',20,3), + (3,'文具','订书机',20,3), + (3,'文具','订书针',10,3), + (4,'文具','裁纸刀',5,5), + (4,'文具','笔',20,2), + (4,'文具','信纸',50,1), + (4,'日常用品','毛巾',4,5), + (4,'日常用品','透明胶',30,1), + (4,'体育用品','羽毛球',20,3) + +--1.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价 +select orders.orderid 订单编号,orderdate 订单日期,itemtype 类别,itemname 产品名称,thenumber 订购数量,themoney 订购单价 from orders +join orderitem on orders.orderid=orderitem.orderid + +--2.查询订购数量大于50的订单的编号,订单日期,订购产品的类别和订购的产品名称 +select orders.orderid 订单编号,orderdate 订单日期,itemtype 类别,itemname 产品名称,thenumber 订购数量,themoney 订购单价 from orders +join orderitem on orders.orderid=orderitem.orderid +where thenumber>50 + +--3.查询所有的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderid 订单编号,orderdate 订单日期,itemtype 类别,itemname 产品名称,thenumber 订购数量,themoney 订购单价,sum(themoney)订购总价 from orders +join orderitem on orders.orderid=orderitem.orderid +group by orders.orderid ,orderdate ,itemtype ,itemname,thenumber,themoney + +--4.查询单价大于等于5并且数量大于等于50的订单的订单的编号,订单日期,订购产品的类别和订购的产品名称,订购数量和订购单价以及订购总价 +select orders.orderid 订单编号,orderdate 订单日期,itemtype 类别,itemname 产品名称,thenumber 订购数量,themoney 订购单价,sum(themoney)订购总价 from orders +join orderitem on orders.orderid=orderitem.orderid +where themoney>=5 and thenumber>=50 +group by orders.orderid ,orderdate ,itemtype ,itemname,thenumber,themoney + +--5.查询每个订单分别订购了几个产品,例如: +-- 编号 订购产品数 订购日期 +-- 1 3 +-- 2 4 +select orderitem.orderid 编号,count(thenumber)订购产品数 from orderitem +group by orderitem.orderid,thenumber + +--6.查询每个订单里的每个类别的产品分别订购了几次和总数量,订购日期,例如: + +-- 订单编号 产品类别 订购次数 总数量 订购日期 +select orderitem.orderid 订单编号,itemtype 类别,thenumber 订购数量,sum(themoney)订购总价 from orderitem +group by orderitem.orderid,itemtype,thenumber,themoney \ No newline at end of file diff --git "a/10 \347\250\213\346\231\264/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/.keep" "b/10 \347\250\213\346\231\264/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/10 \347\250\213\346\231\264/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\230\266\346\256\265\344\272\214\344\275\234\344\270\2321.sql" "b/10 \347\250\213\346\231\264/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\230\266\346\256\265\344\272\214\344\275\234\344\270\2321.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f13fb487d97819870f24ba404b655f6314d15db8 --- /dev/null +++ "b/10 \347\250\213\346\231\264/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\230\266\346\256\265\344\272\214\344\275\234\344\270\2321.sql" @@ -0,0 +1,77 @@ +create database llon + +use llon + +--学生信息表(stuInfo) +create table stuinfo( +stuID int primary key identity, +stuName nvarchar(30), +stuAge int, +stuSex int, +time date +) +--课程信息表(courseInfo) +create table courseinfo( +courseID int primary key identity, +courseName nvarchar(30), +courseMarks int +) +--分数信息表(scoreInfo) +create table scoreinfo( +scoreID int primary key identity, +stuID int references stuinfo(stuID), +courseID int references courseinfo(courseID), +score int +) + +--插入数据 +insert into stuinfo(stuName,stuAge,stuSex,time) +values('Tom',19,1,null),('Jack',20,0,null),('Rose',21,1,null), + ('Lulu',19,1,null),('Lili',21,0,null),('abc',20,1,'2007-01-07 01:11:36.590') + +insert into courseinfo(courseName,courseMarks) +values('JavaBase',4),('HTML',2),('JavaScript',2),('SqlBase',2) + +insert into scoreinfo(stuID,courseID,score) +values(1,1,80),(1,2,85),(1,4,50),(2,1,75), +(2,3,45),(2,4,75),(3,1,45),(4,1,95), +(4,2,75),(4,3,90),(4,4,45) + +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuinfo.stuID 编号,stuname 姓名,count(courseid)选修的课程数量,avg(score)所选修的课程的考试的平均分 from stuinfo +join scoreinfo on stuinfo.stuID=scoreinfo.scoreID +group by stuinfo.stuID,stuname +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName 课程名称,count(scoreinfo.courseid)每门课程的选修的学生的个数,sum(score)学生考试的总分 from scoreinfo +join courseinfo on courseinfo.courseID=scoreinfo.courseID +group by courseName +--3.查询出性别一样并且年龄一样的学生的信息 +select one.stuid,one.stuname,one.stuage,one.stusex from stuinfo one +inner join stuinfo two on one.stusex=two.stusex and one.stuAge=two.stuage +where one.stuID!=two.stuid +--4.查询出学分一样的课程信息 +select distinct one.courseid,one.coursename,one.coursemarks from courseinfo one +join courseinfo two on one.courseMarks=two.courseMarks +where one.courseid!=two.courseID +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuinfo.stuid,stuinfo.stuName,scoreinfo.courseID,scoreinfo.score from stuinfo +join scoreinfo on stuinfo.stuID=scoreinfo.stuID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuinfo.stuID,stuinfo.stuName ,scoreinfo.courseID,courseinfo.courseMarks, scoreinfo.score from stuinfo +left join scoreinfo on stuinfo.stuID=scoreinfo.stuID +left join courseinfo on courseinfo.courseID=scoreinfo.courseID +where score is not null +--7.查询出没有参加考试的学生的学号和姓名 +select stuinfo.stuID,stuinfo.stuName from stuinfo +left join scoreinfo on stuinfo.stuID=scoreinfo.stuID +where score is null +--8.查询出是周六周天来报到的学生 + +--9.查询出姓名中有字母a的学生的信息 +select * from stuinfo where stuinfo.stuName like'%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuinfo.stuID,avg(score) 考试平均分,count(courseinfo.courseid) 选修课程的数量 from stuinfo +join scoreinfo on scoreinfo.stuID=stuinfo.stuID +join courseinfo on courseinfo.courseID=scoreinfo.courseID +group by stuinfo.stuID +having avg(score)>70 and count(courseinfo.courseid)>2 \ No newline at end of file diff --git "a/10 \347\250\213\346\231\264/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\230\266\346\256\265\344\272\214\344\275\234\344\270\2322.sql" "b/10 \347\250\213\346\231\264/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\230\266\346\256\265\344\272\214\344\275\234\344\270\2322.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8a3c31ac247f8f66c2fa64c3b381660cf3f09501 --- /dev/null +++ "b/10 \347\250\213\346\231\264/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232/\351\230\266\346\256\265\344\272\214\344\275\234\344\270\2322.sql" @@ -0,0 +1,68 @@ +create database gjf; + +use gjf; + +create table tbl_card( +id nvarchar(50) primary key , +password char(50) , +balance bigint , +usename nvarchar(50)); + +create table tbl_computer( +id varchar(10) primary key , +onuse int, +note int ); +create table tbl_record( +id int primary key , +cardid char(50) , +computerid varchar(10), +begintime date, +endtime date, +fee int ); + + + + +/*查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列*/ +select usename,tbl_card.id ,tbl_computer.id,begintime ,endtime ,fee from tbl_card join tbl_computer on tbl_card.id=tbl_computer.id join tbl_record on tbl_card.id=tbl_record.id +group by usename,tbl_card.id ,tbl_computer.id,begintime ,endtime ,fee +order by fee desc + +--2. 查询出每台机器上的上网次数和消费的总金额 +select sum(fee) zongjine ,count(onuse) from tbl_computer join tbl_record on tbl_computer.id=tbl_record.computerid + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select sum(fee) , onuse from tbl_computer join tbl_record on tbl_computer.id=tbl_record.computerid +where onuse=1 +group by onuse + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select sum(fee) , onuse from tbl_computer join tbl_record on tbl_computer.id=tbl_record.computerid +where onuse=0 +group by onuse + +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from tbl_card as a, tbl_card as b where a.password=b.usename; +--6. 查询出使用次数最多的机器号和使用次数 + + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select * from tbl_card,tbl_computer,tbl_record where tbl_card.id like '%ABC%' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tbl_card.usename,tbl_computer.id, sum(fee) as '总费用' +from tbl_card,tbl_computer,tbl_record +where tbl_card.id=tbl_computer.id and tbl_card.id=tbl_record.id +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select * from tbl_card a,上机登记表 b where a.卡号=b.卡号 +and 时间>='00:00:01' and 时间<='23:59:59' + + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select top 3 * from tbl_card,tbl_computer,tbl_record + + + +*/ + + diff --git "a/10 \347\250\213\346\231\264/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/.keep" "b/10 \347\250\213\346\231\264/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/10 \347\250\213\346\231\264/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/\347\273\203\344\271\2401.sql" "b/10 \347\250\213\346\231\264/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/\347\273\203\344\271\2401.sql" new file mode 100644 index 0000000000000000000000000000000000000000..230b1aa0a00fc000322ba95f68adeb49ef8f57f6 --- /dev/null +++ "b/10 \347\250\213\346\231\264/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232/\347\273\203\344\271\2401.sql" @@ -0,0 +1,66 @@ +create database llon +go +use llon +go +create table student ( +stuno nvarchar(10) primary key, +stuname nvarchar(20) not null, +stuage int not null, +stuaddress nvarchar(20) not null, +stuseat int identity, +stusex nvarchar(1) not null +); + +insert into student(stuno, stuname, stuage, stuaddress, stusex) +values('s2501','寮犵鍒',20,'缇庡浗纭呰胺',1), +('s2502','鏉庢柉鏂',18,'婀栧寳姝︽眽',0), +('s2503','椹枃鎵',22,'婀栧崡闀挎矙',1), +('s2504','娆ч槼淇婇泟',21,'婀栧寳姝︽眽',0), +('s2505','姊呰秴椋',20,'婀栧寳姝︽眽',1), +('s2506','闄堟棆椋',19,'缇庡浗纭呰胺',1), +('s2507','闄堥',20,'缇庡浗纭呰胺',0) + +create table exam( +examno int primary key identity, +stuno nvarchar(10) references student (stuno), +writtenexam int not null, +labexam int not null +); + +insert into exam(stuno,writtenexam,labexam) +values('s2501',50,70), +('s2502',60,65), +('s2503',86,85), +('s2504',40,80), +('s2505',70,90), +('s2506',85,90) + +--鏌ヨ瀛︾敓鐨勫鍚嶏紝骞撮緞锛岀瑪璇曟垚缁╁拰鏈鸿瘯鎴愮哗 +select student.stuname 濮撳悕,stuage 骞撮緞,writtenexam 绗旇瘯,labexam 鏈鸿瘯 from student +join exam on student.stuno=exam.stuno + +--鏌ヨ绗旇瘯鍜屾満璇曟垚缁╅兘鍦60鍒嗕互涓婄殑瀛︾敓鐨勫鍙凤紝濮撳悕锛岀瑪璇曟垚缁╁拰鏈鸿瘯鎴愮哗 +select student.stuname 濮撳悕,stuage 骞撮緞,writtenexam 绗旇瘯,labexam 鏈鸿瘯 from student +join exam on student.stuno=exam.stuno +where writtenexam>60 and labexam>60 + +--鏌ヨ鎵鏈夊鐢熺殑瀛﹀彿锛屽鍚嶏紝绗旇瘯鎴愮哗锛屾満璇曟垚缁╋紝娌℃湁鍙傚姞鑰冭瘯鐨勫鐢熺殑鎴愮哗浠ULL鍊煎~鍏 +select student.stuname 濮撳悕,stuage 骞撮緞,writtenexam 绗旇瘯,labexam 鏈鸿瘯 from student +left join exam on student.stuno=exam.stuno +order by exam.writtenexam,exam.labexam desc + +--鏌ヨ骞撮緞鍦20浠ヤ笂锛堝寘鎷20锛夌殑瀛︾敓鐨勫鍚嶏紝骞撮緞锛岀瑪璇曟垚缁╁拰鏈鸿瘯鎴愮哗锛屽苟鎸夌瑪璇曟垚缁╅檷搴忔帓鍒 +select student.stuname 濮撳悕,stuage 骞撮緞,writtenexam 绗旇瘯,labexam 鏈鸿瘯 from student +join exam on student.stuno=exam.stuno +where stuage>=20 +order by exam.writtenexam desc + +--鏌ヨ鐢峰コ鐢熺殑鏈鸿瘯骞冲潎鍒 +select stusex,avg(labexam) from student +inner join exam on student.stuno=exam.stuno +group by stusex + +--鏌ヨ鐢峰コ鐢熺殑绗旇瘯鎬诲垎 +select stusex,sum(writtenexam) from student +inner join exam on student.stuno=exam.stuno +group by stusex ; \ No newline at end of file diff --git "a/10 \347\250\213\346\231\264/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232.png" "b/10 \347\250\213\346\231\264/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232.png" new file mode 100644 index 0000000000000000000000000000000000000000..afa2f5f05b1fa29046c753849b1f9198fea4d225 Binary files /dev/null and "b/10 \347\250\213\346\231\264/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232.png" differ diff --git "a/10\347\250\213\346\231\264/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.sql" "b/10\347\250\213\346\231\264/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391