diff --git "a/48\350\203\241\344\270\207\347\233\233/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232" "b/48\350\203\241\344\270\207\347\233\233/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232" new file mode 100644 index 0000000000000000000000000000000000000000..6de646957d293dcf472363bc2c4ec273dd9ccb55 --- /dev/null +++ "b/48\350\203\241\344\270\207\347\233\233/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232" @@ -0,0 +1,218 @@ +create database 阶段二作业一; +go +use 阶段二作业一; +go + +--创建学生信息表 +create table stuInfo( +stuID int identity primary key, +stuName nvarchar(10), +stuAge int, +stuSex tinyint, +time datetime, +); + +--创建课程信息表 +create table courseInfo( +courseID int identity primary key, +courseName nvarchar(10), +courseMarks int ); + +--创建分数信息表 +create table scoreInfo( +scoreID int 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); +select * from stuInfo +select * from scoreInfo +select * from courseInfo +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuID,count(courseName)选修的课程的数量, avg(score)平均分 +from courseInfo inner join scoreInfo +on courseInfo.courseID=scoreInfo.courseID +group by stuID +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName,count(stuName)学生个数,sum(score) 考试总分 +from stuInfo join scoreInfo +on stuInfo.stuID=scoreInfo.stuID join courseInfo +on courseInfo.courseID=scoreInfo.courseID +group by courseName +--3.查询出性别一样并且年龄一样的学生的信息 +select son.* from stuInfo son join stuInfo father +on son.stuAge=father.stuAge and son.stuSex=father.stuSex +where son.stuID!=father.stuID ; +--4.查询出学分一样的课程信息 +select distinct son.* from courseInfo son join courseInfo father +on son.courseMarks=father.courseMarks +where son.courseID!=father.courseID +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select scoreInfo.stuID,StuName,scoreInfo.courseID,score +from stuInfo s join scoreInfo +on s.stuID=scoreInfo.stuID join courseInfo +on courseInfo.courseID=scoreInfo.courseID +where exists(select 1 from scoreInfo r where r.stuID=s.stuID) +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select scoreInfo.stuID,StuName,scoreInfo.courseID,courseName,courseMarks,score +from stuInfo s join scoreInfo +on s.stuID=scoreInfo.stuID join courseInfo +on courseInfo.courseID=scoreInfo.courseID +where exists(select 1 from scoreInfo r where r.stuID=s.stuID) +--7.查询出没有参加考试的学生的学号和姓名 +select stuID,StuName from stuInfo s where not exists(select 1 from scoreInfo r where r.stuID=s.stuID) +--8.查询出是周六周天来报到的学生 + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like'%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select StuInfo.StuId,avg(scoreInfo.Score)as 平均成绩,Count(scoreInfo.Score) as 选课数量 from stuInfo +join scoreInfo on StuInfo.StuId = scoreInfo.StuId +join CourseInfo on scoreInfo.CourseId = CourseInfo.CourseId +group by StuInfo.StuId +Having avg(scoreInfo.Score)>70 and Count(scoreInfo.Score)>2; + +阶段2create database 阶段二作业二 +go +use 阶段二作业二 +go +create table tbl_card( +id nvarchar(15) primary key, +passWord nvarchar(15), +balance money, +userName nvarchar(20) +); +create table tbl_computer( +id nvarchar(15) primary key, +onUse tinyint check(onUse=0 or onUse=1), +Note int +); +create table tbl_record( +id int primary key, +cardId nvarchar(15) references tbl_card(id), +ComputerId nvarchar(15) references tbl_computer(id), +beginTime datetime, +endTime datetime, +fee money +); +insert into tbl_card (id, passWord, balance, userName) +values +('0023_ABC','555',98,'张军'), +('0025_bbd','abe',300,'朱俊'), +('0036_CCD','何柳',100,'何柳'), +('0045_YGR','0045_YGR',58,'证验'), +('0078_RJV','55885fg',600,'校庆'), +('0089_EDE','zhang',134,'张峻') +insert into tbl_computer(id, onUse, Note) +values +('02',0,25555), +('03',1,55555), +('04',0,66666), +('05',1,88888), +('06',0,688878), +('B01',0,558558) +insert into tbl_record(id, cardId, ComputerId, beginTime, endTime, fee) +values +(23,'0078_RJV','B01','2007-7-15 19:00:00','2007-7-15 21:00:00',20), +(34,'0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00',23), +(45,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50), +(46,'0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00',6), +(47,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50), +(48,'0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00',6), +(55,'0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00',50), +(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',300), +(65,'0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00',23), +(98,'0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00',23) + +select * from tbl_card +select * from tbl_computer +select * from tbl_record + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 + +select cardId,userName,tbl_computer.id,beginTime,endTime,fee +from tbl_record join tbl_computer on tbl_computer.id=tbl_record.ComputerId +join tbl_card on tbl_card.id =tbl_record.cardId +where userName like '张军' +order by fee desc + +--2. 查询出每台机器上的上网次数和消费的总金额 + +select ComputerId,count(cardId)上网次数,sum(fee)总金额 from tbl_record group by ComputerId + +--3. 查询出所有已经使用过的上网卡的消费总金额 + +select cardid,sum(fee) from tbl_record group by cardid + +--4. 查询出从未消费过的上网卡的卡号和用户名 + +select id,userName from tbl_card c +where not exists( select 1 from tbl_record r where r.cardid=c.id) + +select id,userName from tbl_card +where id not in (select distinct cardId from tbl_record) + +select * from tbl_card left join tbl_record +on tbl_card.id=tbl_record.cardId +where fee is null + +--5. 将密码与用户名一样的上网卡信息查询出来 + +select * from tbl_card where passWord=userName + +--6. 查询出使用次数最多的机器号和使用次数 + +select top 1 ComputerId,count(cardId)使用次数 from tbl_record group by ComputerId order by count(cardId) desc + +select * from +(select computerId,(count(*))使用次数 from tbl_record group by computerId) 上网次数 +where 使用次数= (select max(使用次数) from (select computerId,count(*) 使用次数 from tbl_record group by computerId) 上网次数) + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 + +select cardId,userName,computerId,fee from tbl_record join tbl_card on tbl_record.cardId=tbl_card.id where cardId like'%abc' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + + select tbl_card.id,tbl_card.userName,tbl_computer.id,beginTime,endTime,tbl_record.fee + from tbl_record join tbl_card on tbl_card.id=tbl_record.cardId join tbl_computer on tbl_computer.id=tbl_record.ComputerId + where datepart(weekday,cast(tbl_record.begintime as date)) in (1,7) + --获取时间的星期数 + select DATEPART(weekday,'2023-03-19') + + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select cardId,userName,computerId,beginTime,endTime,fee from tbl_record join tbl_card on tbl_card.id=tbl_record.cardId where (endTime-beginTime)> '12:00:00' + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select cardId,userName,ComputerId,beginTime,endTime,fee +from tbl_record join tbl_card on tbl_card.id=tbl_record.cardId +where fee not in (select top 3 fee from tbl_record order by fee desc) \ No newline at end of file