diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/.keep" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\215\242\345\256\201/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\215\242\345\256\201/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..202304afe4ca2e7a9f4c31aebc746e4460396b74 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\215\242\345\256\201/SQLQuery1.sql" @@ -0,0 +1,99 @@ +use master +go + +create database Students +on +( + name='Students', + filename='F:\Students.mdf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +log on +( + name='Students_log', + filename='F:\Students_log.ldf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +go + +use Students +go + +create table stuInfo +( + stuID int primary key not null identity(1,1), + stuName varchar(10) not null, + stuAge int not null, + stuSex varchar(2) not null check(stuSex='1'or stuSex='0'), + stuTime datetime null +) + +create table courseInfo +( + courseID int primary key not null identity(1,1), + courseName varchar(10) not null, + courseMake int not null +) + +create table scoreInfo +( + scoreID int primary key not null identity(1,1), + stuID int references stuInfo(stuID), + courseID int references courseInfo(courseID), + score int not null +) + +insert into stuInfo(stuName,stuAge,stuSex,stuTime) +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,courseMake) +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','95') + +select * from stuInfo +select * from courseInfo +select * from scoreInfo + +--1.查询出每个学生 所选修的课程的数量 和 所选修的课程的考试的平均分 +select ST.stuName 学生,COUNT(distinct C.courseID) 课程数,AVG(score) 平均分 from scoreInfo SC +inner join stuInfo ST on ST.stuID=SC.stuID +INNER JOIN courseInfo C on C.courseID=SC.courseID +group by ST.stuName + +--2.查询出 每门课程的 选修的学生的个数 和 学生考试的总分 +select A.courseID,COUNT(distinct B.stuID)学生个数,SUM(score)考试总分 from courseInfo A inner join stuInfo B on A.courseID = B.stuID +inner join scoreInfo C on A.courseID = C.courseID GROUP BY A.courseID,courseMake + +--3.查询出性别一样并且年龄一样的学生的信息 +select A.stuName, A.stuSex,A.stuAge from stuInfo A inner join stuInfo B on (A.stuID <> B.stuID) and +(A.stuSex = B.stuSex) and (A.stuAge = B.stuAge) + +--4.查询出学分一样的课程信息 +select distinct A.courseName,A.courseMake from courseInfo A inner join courseInfo B on (A.courseID <> B.courseID) and +(A.courseMake = B.courseMake) + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select A.stuID 学号,A.stuName 姓名,C.courseID 课程号,C.score 分数 from stuInfo A inner join scoreInfo C on A.stuID = C.stuID + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select C.stuID 学号,C.courseID 课程号,A.courseName 课程名,A.courseMake 课程学分, C.score 分数 from courseInfo A inner join scoreInfo C on A.courseID = C.courseID + +--7.查询出没有参加考试的 学生的学号 和 姓名 +select A.stuID 学号,A.stuName 姓名 from stuInfo A inner join scoreInfo B on A.stuID = B.stuID where B.score is null + +--8.查询出是周六周天来报到的学生 +select * from stuInfo where datename(weekday,stuTime)='星期六' or datename(weekday,stuTime)='星期日' + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' + +--10.查询出选修了 2门课程以上的 并且 考试平均分在70以上 的学生的学号 和考试平均分 以及选修课程的数量 +select A.stuID 学号,COUNT(*)课程数量,AVG(score)平均分 from stuInfo A inner join scoreInfo B on A.stuID = B.stuID group by A.stuID having COUNT(*)>2 and AVG(score)>70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\345\220\257\350\210\252/SQLQuery15.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\345\220\257\350\210\252/SQLQuery15.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d4f7b1ce11ca3ba008e225eef58d1eb0340a1712 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\345\220\257\350\210\252/SQLQuery15.sql" @@ -0,0 +1,89 @@ +use master +go +create database students +on +( + name='students', + filename='D:\students.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log on +( + name='students_log', + filename='D:\students_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +go +use students +go +create table stuInfo +( + stuID int primary key identity(1,1), + stuName varchar(10), + stuAge int, + stuSex char(2) check(stuSex =0 or stuSex =1), + time datetime +) +create table courseInfo +( + courseID int primary key identity(1,1), + courseName varchar(10), + courseMarks int +) +create table scoreInfo +( + scoreID int primary key identity(1,1), + stuID int references stuInfo(stuID) , + courseID int references courseInfo(courseID), + score int +) + +insert into stuInfo 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 values ('JavaBase',4), +('HTML',2),('JavaScript',2),('SqlBase',2) + +insert into scoreInfo 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 stuName 学生姓名,count(*)课程数量,avg(score) 平均分 from scoreInfo s inner join stuInfo i on i.stuID = s.stuID inner join courseInfo c on c.courseID = s.courseID + group by stuName +--2.查询出每门课程的选修的学生的个数和学生考试的总分 + select courseName 课程名称,COUNT(*) 选修学生个数,sum(score)考试总分 from scoreInfo s inner join stuInfo i on i.stuID = s.stuID inner join courseInfo c on c.courseID = s.courseID + group by courseName +--3.查询出性别一样并且年龄一样的学生的信息 + select A.* from stuInfo A inner join stuInfo B on A.stuID<>B.stuID and A.stuSex=B.stuSex and A.stuAge=B.stuAge + +--4.查询出学分一样的课程信息 + select distinct A.* from courseInfo A inner join courseInfo B on A.courseID<>B.courseID and A.courseMarks=B.courseMarks + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 + select s.StuID,StuName,courseID,score from Stuinfo s right join scoreInfo i on s.stuID=i.stuID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 + select s.stuID,c.courseID,courseName,courseMarks,score from courseInfo c left join scoreInfo s on s.courseID= c.courseID +--7.查询出没有参加考试的学生的学号和姓名 + select s.StuID,StuName from Stuinfo s left join scoreInfo i on s.StuID = i.stuID + where score is NULL or score=' ' + +--8.查询出是周六周天来报到的学生 + select * from stuInfo where datename(weekday,time)='星期六' or datename(weekday,time)='星期天' +--9.查询出姓名中有字母a的学生的信息 + select * from stuInfo where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 + select s.stuID,avg(score),count(*) from scoreInfo s inner join stuInfo i on i.stuID = s.stuID inner join courseInfo c on c.courseID = s.courseID + group by s.stuID + having count(*)>2 and avg(score)>70 + +select * from stuInfo --学生信息表 +select * from courseInfo --课程信息表 +select * from scoreInfo --分数信息表 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\345\220\257\350\210\252/SQLQuery16.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\345\220\257\350\210\252/SQLQuery16.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8b48be50e68d6e970025eb1349211cc973b394ac --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\345\220\257\350\210\252/SQLQuery16.sql" @@ -0,0 +1,99 @@ +use master +go +create database chargesystem +on +( + name='chargesystem', + filename='D:\chargesystem.mdf', + size=10, + maxsize=50, + filegrowth=5% +) +log on +( + name='chargesystem_log', + filename='D:\chargesystem_log.mdf', + size=10, + maxsize=50, + filegrowth=5% +) +go +use chargesystem +go +create table tbl_card +( + id varchar(10) primary key, + passWord nvarchar(10), + balance int, + userName nvarchar(10) +) +create table tbl_computer +( + id varchar(10) primary key, + onUse char(2) check(onUse=0 or onUse=1), + note varchar(20) +) +create table tbl_record +( + id int primary key, + cardid varchar(10) references tbl_card(id), + Computerid varchar(10) references tbl_computer(id), + beginTime datetime, + endTime datetime, + fee int +) + +insert into tbl_card values ('0023_ABC','555',98,'张军'), +('0025_bbd','abc',300,'朱俊'),('0036_CCD','何柳',100,'何柳'), +('0045_YGR','0045_YGR',58,'证验'),('0078_RJV','55885fg',600,'校庆'), +('0089_EDE','zhang',134,'张峻') + +insert into tbl_computer values ('02',0,25555),('03',0,55555), +('04',0,66666),('05',1,88888),('06',0,688878),('B01',0,558558) + +insert into tbl_record values (23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-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',3), +(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) + + + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 + select c.id 卡号,userName 用户名,Computerid 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_card c inner join tbl_record r on c.id=r.cardid + where userName='张军'order by fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 + select c.id 机器编号,COUNT(*) 上网次数,sum(fee) 消费总金额 from tbl_computer c inner join tbl_record r on c.id = r.Computerid + group by c.id +--3. 查询出所有已经使用过的上网卡的消费总金额 + select SUM(fee)消费总金额 from tbl_card c right join tbl_record r on c.id = r.cardid +--4. 查询出从未消费过的上网卡的卡号和用户名 + select c.id , userName from tbl_card c left join tbl_record r on c.id = r.cardid + where fee is NULL or fee=' ' +--5. 将密码与用户名一样的上网卡信息查询出来 + select * from tbl_card A inner join tbl_card B on A.id=B.passWord +--6. 查询出使用次数最多的机器号和使用次数 + select top 1 cardid ,COUNT(*) from tbl_record group by cardid order by COUNT(*) desc + select * from tbl_record +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 + select userName,Computerid,fee from tbl_card c inner join tbl_record r on c.id = r.cardid where c.id like '%ABC' +--8. 查询出是周六、周天上网记的录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + select c.id,userName,Computerid,beginTime,endTime,fee from tbl_record r inner join tbl_card c on c.id=r.cardid + where datename(weekday,beginTime)='星期六' or datename(weekday,beginTime)='星期天' +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + select c.id,userName,Computerid,beginTime,endTime,fee from tbl_record r inner join tbl_card c on c.id=r.cardid + where DATEDIFF(HH,beginTime,endTime)>12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + select c.id,userName,Computerid,beginTime,endTime,fee from tbl_record r inner join tbl_card c on c.id=r.cardid + where fee !=50 + select top 3 fee from tbl_record order by fee desc + + +select * from tbl_card +select * from tbl_computer +select * from tbl_record \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\345\237\216/\344\275\234\344\270\2321.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\345\237\216/\344\275\234\344\270\2321.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9f7898e53a72774aced57b69ce5b4f0fc34f0e6b --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\345\237\216/\344\275\234\344\270\2321.sql" @@ -0,0 +1,78 @@ +use master +go +create database Student +use Student +create table stuInfo +( + stuID int primary key identity(1,1), + stuName nvarchar(10) not null, + stuAge nvarchar(30) not null, + stuSex nvarchar(2) not null, + [time] datetime +) +insert into stuInfo values('Tom',19,'鐢',null),('Jack',20,'濂',null), +('Rose',21,'鐢',null),('LUlu',19,'鐢',null),('Lili',21,'濂',null),('abc',20,'鐢','2007-01-07 01:11:36.590') + +create table Course +( + courseID int primary key identity(1,1), + courseName nvarchar(10) not null, + courseMarks int not null +) + +insert into Course values('JavaBase',4),('HTML',2),('JavaScript',2),('SqlBase',2) + + +create table Score +( + scoreID int primary key identity(1,1), + stuID int references stuInfo(stuID) not null, + courseID int references Course(courseID) not null, + score int not null +) + +insert into 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 Course +select * from Score + +--1.鏌ヨ鍑烘瘡涓鐢熸墍閫変慨鐨勮绋嬬殑鏁伴噺鍜屾墍閫変慨鐨勮绋嬬殑鑰冭瘯鐨勫钩鍧囧垎 + select st.stuID ,stuName,COUNT(courseID) 璇剧▼鏁伴噺, AVG(score) 鑰冭瘯骞冲潎鍒 from stuInfo st + inner join Score s on st.stuID=s.stuID + group by st.stuID,stuName +--2.鏌ヨ鍑烘瘡闂ㄨ绋嬬殑閫変慨鐨勫鐢熺殑涓暟鍜屽鐢熻冭瘯鐨勬诲垎 + select st.stuID ,stuName,COUNT(courseID) 璇剧▼鏁伴噺, sum(score) 鑰冭瘯鎬诲垎 from stuInfo st + inner join Score s on st.stuID=s.stuID + group by st.stuID,stuName +--3.鏌ヨ鍑烘у埆涓鏍峰苟涓斿勾榫勪竴鏍风殑瀛︾敓鐨勪俊鎭 + select s1.* from stuInfo s1 + inner join stuInfo s2 + on (s1.stuSex=s2.stuSex) and (s1.stuAge=s2.stuSex) +--4.鏌ヨ鍑哄鍒嗕竴鏍风殑璇剧▼淇℃伅 + select distinct c1.* from Course c1 + inner join Course c2 + on (c1.courseID<>c2.courseID) and (c1.courseMarks=c2.courseMarks) +--5.鏌ヨ鍑哄弬鍔犱簡鑰冭瘯鐨勫鐢熺殑瀛﹀彿锛屽鍚嶏紝璇剧▼鍙峰拰鍒嗘暟 + select st.stuID 瀛﹀彿,st.stuName 濮撳悕, sc.courseID 璇剧▼鍙,sc. score 鍒嗘暟 from stuInfo st + inner join Score sc + on st.stuID=sc.stuID +--6.鏌ヨ鍑哄弬鍔犱簡鑰冭瘯鐨勫鐢熺殑瀛﹀彿锛岃绋嬪彿锛岃绋嬪悕锛岃绋嬪鍒嗗拰鍒嗘暟 + select sc.stuID,sc.courseID,courseName,courseMarks, score from Score sc + inner join Course c + on sc.courseID=c.courseID +--7.鏌ヨ鍑烘病鏈夊弬鍔犺冭瘯鐨勫鐢熺殑瀛﹀彿鍜屽鍚 + select st.stuID,stuName from stuInfo st + left join Score sc + on st.stuID=sc.stuID + where sc.scoreID is null +--8.鏌ヨ鍑烘槸鍛ㄥ叚鍛ㄥぉ鏉ユ姤鍒扮殑瀛︾敓 + select * from stuInfo where DATENAME(DW,time)='鏄熸湡鍏'or DATENAME(DW,time)='鏄熸湡鏃' +--9.鏌ヨ鍑哄鍚嶄腑鏈夊瓧姣峚鐨勫鐢熺殑淇℃伅 + select * from stuInfo where stuName like '%a%' +--10.鏌ヨ鍑洪変慨浜2闂ㄨ绋嬩互涓婄殑骞朵笖鑰冭瘯骞冲潎鍒嗗湪70浠ヤ笂鐨 瀛︾敓鐨勫鍙峰拰鑰冭瘯骞冲潎鍒嗕互鍙婇変慨璇剧▼鐨勬暟閲 + select stuID '瀛﹀彿', avg(score) 骞冲潎鍒,COUNT(*) 閫夎鏁伴噺 from score + group by stuID + having COUNT(*)>2 and avg(score)>70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\345\255\235\346\266\265/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\345\255\235\346\266\265/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c74b406291a3da29f70286b36e41e9a113e7faef --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\345\255\235\346\266\265/SQLQuery1.sql" @@ -0,0 +1,136 @@ +use master +go + +create database Student +on +( + name=Student, + filename='E:\SQL\330\Student.mdf', + size=5MB, + maxsize=10MB, + filegrowth=10% +) +log on +( + name=Student_log, + filename='E:\SQL\330\Student_log.ldf', + size=5MB, + maxsize=10MB, + filegrowth=10% +) +go + +use Student +go + +create table stuInfo +( + stuID int identity(1,1), + stuName varchar(30), + stuAge varchar(5), + stuSex varchar(6), + time datetime +) +go + +insert into stuInfo(stuName,stuAge,stuSex) +select 'Tom',19,1 union +select 'Jack',20,0 union +select 'Rose',21,1 union +select 'Lulu',19,1 union +select 'Lili',21,0 union +select 'abc',20,1 +go + +update stuInfo set time='2007-01-07 01:11:36.590' where stuID=6 +go + +create table courseInfo +( + courseID int identity(1,1), + courseName varchar(10), + courseMarks varchar(6) +) +go + +insert into courseInfo(courseName,courseMarks)values('JavaBase',4),('HTML',2),('JavaScript',2),('SqlBase',2) +go + +create table scoreInfo +( + scoreID int identity(1,1), + stuID int, + courseID int, + score int, +) +go + +insert into scoreInfo(stuID,courseID,score) +select 1,1,80 union +select 1,2,85 union +select 1,4,50 union +select 2,1,75 union +select 2,3,45 union +select 2,4,75 union +select 3,1,45 union +select 4,1,95 union +select 4,2,75 union +select 4,3,90 union +select 4,4,45 +go + +--棰樼洰锛 +--1.鏌ヨ鍑烘瘡涓鐢 鎵閫変慨鐨勮绋嬬殑鏁伴噺 鍜屾墍閫変慨鐨勮绋嬬殑鑰冭瘯鐨勫钩鍧囧垎 + +select stuName 瀛︾敓濮撳悕,count(s.stuID)璇剧▼鏁伴噺,AVG(score)鑰冭瘯骞冲潎鍒 from scoreInfo s +inner join stuInfo i on i.stuID=s.stuID +group by s.stuID,stuName + +--2.鏌ヨ鍑烘瘡闂ㄨ绋嬬殑閫変慨鐨勫鐢熺殑涓暟鍜屽鐢熻冭瘯鐨勬诲垎 + +select courseName 璇剧▼鍚嶇О,COUNT(s.courseID)瀛︾敓涓暟,SUM(score)鑰冭瘯鎬诲垎 from scoreInfo s +inner join courseInfo c on c.courseID=s.courseID +group by s.courseID,courseName + + +--3.鏌ヨ鍑烘у埆涓鏍峰苟涓斿勾榫勪竴鏍风殑瀛︾敓鐨勪俊鎭 + +select * from stuInfo A +left join stuInfo B on B.stuID = A.stuID +where B.stuAge=A.stuAge and A.stuSex=B.stuSex + +--4.鏌ヨ鍑哄鍒嗕竴鏍风殑璇剧▼淇℃伅 + +select * from courseInfo A +right join courseInfo B on B.courseMarks=A.courseMarks + +--5.鏌ヨ鍑哄弬鍔犱簡鑰冭瘯鐨勫鐢熺殑瀛﹀彿锛屽鍚嶏紝璇剧▼鍙峰拰鍒嗘暟 +select A.stuID,stuName,courseID,score from stuInfo A +right join scoreInfo B on A.stuID=B.stuID + +--6.鏌ヨ鍑哄弬鍔犱簡鑰冭瘯鐨勫鐢熺殑瀛﹀彿锛岃绋嬪彿锛岃绋嬪悕锛岃绋嬪鍒嗗拰鍒嗘暟 + +select A.stuID 瀛︾敓瀛﹀彿,C.courseID 璇剧▼鍙,C.courseName,C.courseMarks,score from stuInfo A +right join scoreInfo B on A.stuID=B.stuID +left join courseInfo C on C.courseID=B.courseID + +--7.鏌ヨ鍑烘病鏈夊弬鍔犺冭瘯鐨勫鐢熺殑瀛﹀彿鍜屽鍚 +select B.stuID,B.stuName from scoreInfo A +right join stuInfo B on A.stuID=B.stuID +except +select A.stuID,stuName from stuInfo A +right join scoreInfo B on A.stuID=B.stuID +left join courseInfo C on C.courseID=B.courseID + +--8.鏌ヨ鍑烘槸鍛ㄥ叚鍛ㄥぉ鏉ユ姤鍒扮殑瀛︾敓 + + +--9.鏌ヨ鍑哄鍚嶄腑鏈夊瓧姣峚鐨勫鐢熺殑淇℃伅 + +select * from stuInfo where stuName like '%a%' + +--10.鏌ヨ鍑洪変慨浜2闂ㄨ绋嬩互涓婄殑骞朵笖鑰冭瘯骞冲潎鍒嗗湪70浠ヤ笂鐨勫鐢熺殑 瀛﹀彿 鍜 鑰冭瘯骞冲潎鍒嗕互鍙 閫変慨璇剧▼鐨勬暟閲 + +select stuID 瀛︾敓瀛﹀彿,AVG(score)鑰冭瘯骞冲潎鍒,COUNT(stuID)璇剧▼鏁伴噺 from scoreInfo +group by stuID +having COUNT(stuID)>2 and AVG(score)>70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\231\346\226\207\350\243\225/.keep" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\231\346\226\207\350\243\225/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\231\346\226\207\350\243\225/3.30.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\231\346\226\207\350\243\225/3.30.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4c8307650b3af4f01cc35c442d618c7e73a18755 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\231\346\226\207\350\243\225/3.30.sql" @@ -0,0 +1,86 @@ +use master +go +create database Student +on( + name = 'Student.mdf', + filename='D:\Student.mdf', + size=5, + maxsize=10, + filegrowth=15% +) +log on( + name = 'Student_log.ldf', + filename='D:\Student_log.ldf', + size=5, + maxsize=10, + filegrowth=15% +) +go +use Student +go +create table stuInfo +( + stuID int primary key identity(1,1), + stuName nvarchar(7) not null, + stuAge int not null, + stuSex nvarchar(1) not null, + time datetime +) +create table courseInfo +( + courseID int primary key identity(1,1), + courseName nvarchar(10) not null, + courseMarks nvarchar(5) +) +create table scoreInfo +( + scoreID int primary key identity(1,1), + stuID int references stuInfo(stuID), + courseID int references courseInfo(courseID), + score int not null +) +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','95') +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) +--题目: +select * from stuInfo +select * from courseInfo +select * from scoreInfo + +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuName 学生 ,count(courseInfo.courseID) 课程数量,AVG(score) 平均分 from stuInfo inner join scoreInfo +on stuInfo.stuID=scoreInfo.stuID inner join courseInfo on courseInfo.courseID=scoreInfo.courseID group by + stuName +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName 课程名称, COUNT(scoreInfo.courseID) 学生个数 ,sum(score) 总分 from stuInfo inner join scoreInfo +on stuInfo.stuID=scoreInfo.stuID inner join courseInfo on courseInfo.courseID=scoreInfo.courseID group by + scoreInfo.courseID,courseName +--3.查询出性别一样并且年龄一样的学生的信息 +select * from stuInfo a left join stuInfo b on a.stuSex=b.stuSex and a.stuAge=b.stuAge +--4.查询出学分一样的课程信息 +select * from courseInfo A inner join courseInfo B on A.courseMarks = B.courseMarks and A.courseID<>B.courseID +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuInfo.stuID 学号,stuInfo.stuName 姓名,courseInfo.courseID 课程号, scoreInfo.score 分数 from stuInfo inner join scoreInfo +on stuInfo.stuID=scoreInfo.stuID inner join courseInfo on courseInfo.courseID=scoreInfo.courseID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuInfo.stuID 学号,courseInfo.courseID 课程号,courseInfo.courseMarks 课程学分, scoreInfo.score 分数 from stuInfo inner join scoreInfo +on stuInfo.stuID=scoreInfo.stuID inner join courseInfo on courseInfo.courseID=scoreInfo.courseID +--7.查询出没有参加考试的学生的学号和姓名 +select stuInfo.stuID 学号,stuInfo.stuName 姓名 from stuInfo inner join scoreInfo +on stuInfo.stuID=scoreInfo.stuID inner join courseInfo on courseInfo.courseID=scoreInfo.courseID where +stuInfo.stuID is null and stuInfo.stuName is null +--8.查询出是周六周天来报到的学生 +select * from StuInfo where datename(weekday,time)='星期六' or datename(weekday,time)='星期日' +--9.查询出姓名中有字母a的学生的信息 +select * from StuInfo where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuInfo.stuID 学号,avg(scoreInfo.score) 考试平均分,COUNT(courseInfo.courseID) 选修课程的数量 from stuInfo inner join scoreInfo +on stuInfo.stuID=scoreInfo.stuID inner join courseInfo on courseInfo.courseID=scoreInfo.courseID +GROUP BY stuInfo.stuID HAVING COUNT(courseInfo.courseID)>=2 +AND avg(scoreInfo.score)>=70 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\256\213\345\230\211\347\202\234/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\256\213\345\230\211\347\202\234/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..16471a5b79db477c32eb67c94fd65a5f958b19b8 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\256\213\345\230\211\347\202\234/SQLQuery1.sql" @@ -0,0 +1,115 @@ +use master +go + +create database Student +on +( + name = 'Student', + filename = 'D:\sql\Student.mlf', + size = 5mb, + maxsize=50mb, + filegrowth = 10% +) +log on +( + name = 'Student_log', + filename = 'D:\sql\Student_log.mlf', + size = 5mb, + maxsize=50mb, + filegrowth = 10% +) +go +use Student +go + +create table StuInfo +( + stuId int primary key identity(1,1), + stuName nvarchar(10), + stuAge int, + stuSex int, + time datetime +) +create table CourseInfo +( + courseId int primary key identity(1,1), + courseName varchar(10), + courseMarks int +) + +create table ScoreInfo +( + scoreId int identity, + stuID int references StuInfo(stuid), + courseID int references CourseInfo(courseId), + score int +) + +insert into Stuinfo 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' ) +select * from Stuinfo + +drop table ScoreInfo +drop table CourseInfo +insert into CourseInfo values +('JavaBase',4), +('HTML',2), +('JavaScript',2), +('SqlBase',2) + +insert into ScoreInfo 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 CourseInfo +select * from scoreInfo +--题目: +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select StuName,count(courseid)选修的课程的数量,avg(score)选修的课程的考试的平均分 from Stuinfo S +left join scoreinfo SC on S.StuID = SC.stuid group by SC.StuID,StuName + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select count(coursename)选修的学生的个数,sum(score)学生考试的总分 from courseinfo C +left join scoreinfo SC on C.courseid=SC.courseid group by SC.courseid + +--3.查询出性别一样并且年龄一样的学生的信息 +select A.*from StuInfo A , StuInfo B WHERE A.stuAge=B.stuAge and A.stuSex = B.stuSex AND A.stuId != B.stuId + +--4.查询出学分一样的课程信息 +select A.courseID ,A.coursemarks, B.coursemarks from courseinfo A inner join courseinfo B on A.coursemarks = B.coursemarks + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select S.stuId,stuName,SC.courseid,SCORE from scoreinfo SC +inner join courseinfo C on SC.courseid=C.courseid +inner join StuInfo S on SC.stuid = S.stuId + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select S.stuId,stuName,SC.courseid,C.coursename,SCORE from scoreinfo SC +inner join courseinfo C on SC.courseid=C.courseid +inner join StuInfo S on SC.stuid = S.stuId + +--7.查询出没有参加考试的学生的学号和姓名 +select * from StuInfo S +left join scoreinfo SC on SC.stuid= S.stuId where score is NULL + +--9.查询出姓名中有字母a的学生的信息 +select * from StuInfo WHERE stuName like '%a%' + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select SC.stuid, StuName,count(courseid)选修的课程的数量,avg(score)选修的课程的考试的平均分 from Stuinfo S +left join scoreinfo SC on S.StuID = SC.stuid group by SC.StuID,StuName having count(courseid)>=2 and avg(score)>=70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\262\263\345\220\257\345\215\216/ss.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\262\263\345\220\257\345\215\216/ss.sql" new file mode 100644 index 0000000000000000000000000000000000000000..40bf1f306e966919d4a3a23a0f50a0595e06c1c0 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\262\263\345\220\257\345\215\216/ss.sql" @@ -0,0 +1,183 @@ +use master +go + +create database Utest +go + +use UTest +go + +create table StuInfo +( + StuID int primary key identity(1,1), + StuName varchar(10) not null, + StuAge int, + StuSex char(1) default(0) check(StuSex in(0, 1)), + Time datetime +) + +insert into + StuInfo(StuName, StuAge, StuSex, Time) +values + ('Tom',19,1,NUll), + ('Jeck',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') +go + +-- courseInfo scoreInfo +create table CourseInfo +( + CourseID int primary key identity(1,1), + CourseName varchar(15) not null, + CourseMarks int +) + +insert into + CourseInfo(CourseName, CourseMarks) +values + ('JavaBase',4), + ('HTML',2), + ('JavaScript',2), + ('SqlBase',2) +go + +create table ScoreInfo +( + ScoreId int primary key identity(1,1), + StuId int foreign key references StuInfo(StuID), + CourseID int foreign key references CourseInfo(CourseID) not null, + Score int +) + +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) +go + +select * from StuInfo +select * from CourseInfo +select * from ScoreInfo + +-- 1.鏌ヨ鍑烘瘡涓鐢熸墍閫変慨鐨勮绋嬬殑鏁伴噺鍜屾墍閫変慨鐨勮绋嬬殑鑰冭瘯鐨勫钩鍧囧垎 +select + Sti.StuName,count(Sci.CourseID),avg(Sci.Score) +from + StuInfo Sti +join + ScoreInfo Sci +on + Sti.StuID = Sci.StuID +group by + Sci.StuID + +-- 2.鏌ヨ鍑烘瘡闂ㄨ绋嬬殑閫変慨鐨勫鐢熺殑涓暟鍜屽鐢熻冭瘯鐨勬诲垎 +select + Ci.CoureName, count(Sci.CourseId), Sti.StuName, sum(Sci.Score) +from + CourseInfo Ci +join + ScoreInfo Sci +on + Sci.CourseID = Ci.CourseID +join + StuInfo Sti +on + Sti.StuID = Sci.StuID +group by + Sci.StuID +order by + Sci.Score desc + +-- 3.鏌ヨ鍑烘у埆涓鏍峰苟涓斿勾榫勪竴鏍风殑瀛︾敓鐨勪俊鎭 +select + S1.* +from + StuInfo s1 +join + StuInfo s2 +on + S1.stuID <> S2.stuID and S1.stuSex = S2.stuSex and S1.stuAge = S2.stuAge + +-- 4.鏌ヨ鍑哄鍒嗕竴鏍风殑璇剧▼淇℃伅 +select + * +from + CourseInfo Ci +join + CourseInfo Ci2 +on + Ci.CourseMarks = Ci2.CourseMarks + + +-- 5.鏌ヨ鍑哄弬鍔犱簡鑰冭瘯鐨勫鐢熺殑瀛﹀彿锛屽鍚嶏紝璇剧▼鍙峰拰鍒嗘暟 +select + Sti.StuID,Sti.StuName,Sci.CourseID,Sci.Score +from + StuInfo Sti +join + ScoreInfo Sci +on + Sti.stuID = Sci.StuID + +-- 6.鏌ヨ鍑哄弬鍔犱簡鑰冭瘯鐨勫鐢熺殑瀛﹀彿锛岃绋嬪彿锛岃绋嬪悕锛岃绋嬪鍒嗗拰鍒嗘暟 +select + Sti.StuID,Sti.StuName,Ci.*,Sci.Score +from + StuInfo Sti +join + ScoreInfo Sci +on + Sti.stuID = Sci.StuID +join + CourseInfo Ci +on + Sci.CourseID = Ci.CourseID + +-- 7.鏌ヨ鍑烘病鏈夊弬鍔犺冭瘯鐨勫鐢熺殑瀛﹀彿鍜屽鍚 +select + Sti.StuID,Sti.StuName +from + StuInfo Sti +join + ScoreInfo Sci +on + Sti.StuID not in (Sci.StuID ) + +-- 8.鏌ヨ鍑烘槸鍛ㄥ叚鍛ㄥぉ鏉ユ姤鍒扮殑瀛︾敓 +select + Sti.StuName +from + StuInfo Sti +where + datepart(weekday,getdate()) > 5 + +-- 9.鏌ヨ鍑哄鍚嶄腑鏈夊瓧姣峚鐨勫鐢熺殑淇℃伅 +select + * +from + StuInfo +where + StuName like ('%a%') +-- 10.鏌ヨ鍑洪変慨浜2闂ㄨ绋嬩互涓婄殑骞朵笖鑰冭瘯骞冲潎鍒嗗湪70浠ヤ笂鐨勫鐢熺殑瀛﹀彿鍜岃冭瘯骞冲潎鍒嗕互鍙婇変慨璇剧▼鐨勬暟閲 +select + StuID,count(Sci.CoureID),avg(Sci.Score) +from + ScoreInfo Sci +group by + Sci.StuID +having + count(Sci.CoureID) > 2 and avg(Sci.Score) > 70 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\346\230\214\351\276\231/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\346\230\214\351\276\231/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0553bd54176318855c53cac45a5b1554b1ca693c --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\346\230\214\351\276\231/SQLQuery1.sql" @@ -0,0 +1,71 @@ +use master +go +create database Students +on +( + name='Students', + filename='D:\SQLwork\Students.mdf', + Size=5MB, + maxsize=50MB, + filegrowth=10% + ) + log on +( +name='Students_log', + filename='D:\SQLwork\Students_log.ldf', + Size=5MB, + maxsize=50MB, + filegrowth=10% +) +go +use Students +create table stuInfo +( +stuID int primary key identity(1,1), +stuName nvarchar(10) , +stuAge int , +stuSex nvarchar(2) , +stuTime datetime , +) +create table courseInfo +( +courseID int primary key identity(1,1), +courseName nvarchar(10) not null, +courseMarks int not null, +) +create table scoreInfo +( +scoreID int identity(1,1), +stuID int references stuInfo(stuID), +courseID int references courseInfo(courseID), +score int not null, +) + +insert into stuInfo(stuName,stuAge,stuSex,stuTime) 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 courseInfo +select * from scoreInfo +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) +--题目: +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select F.stuID,stuName, count (courseMarks)课程的数量,avg (score)课程的考试的平均分 from stuInfo F,courseInfo C,scoreInfo O where F.stuID=O.stuID AND C.courseID=O.courseID group by stuName ,F.stuID +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select C.courseID, count (F.stuID)选修的学生的个数,SUM (score)课程的考试的总分 from stuInfo F,courseInfo C,scoreInfo O where F.stuID=O.stuID AND C.courseID=O.courseID group by C.courseID +--3.查询出性别一样并且年龄一样的学生的信息 +select A.stuID,A.stuName,A.stuSex,B.stuAge FROM stuInfo A inner join stuInfo B on A.stuID<>B.stuID and A.stuSex=B.stuSex and A.stuAge=B.stuAge +--4.查询出学分一样的课程信息 +select distinct B.courseID,B.courseName,B.courseMarks from courseInfo A inner join courseInfo B on A.courseID<>B.courseID and A.courseMarks=B.courseMarks +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select F.stuID 学号,stuName 姓名,C.courseID 课程号,score 分数 from stuInfo F,courseInfo C,scoreInfo O where F.stuID=O.stuID AND C.courseID=O.courseID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select F.stuID 学号,stuName 姓名,C.courseID 课程号,courseName 课程名,courseMarks 课程学分,score 分数 from stuInfo F,courseInfo C,scoreInfo O where F.stuID=O.stuID AND C.courseID=O.courseID +--7.查询出没有参加考试的学生的学号和姓名 +select S.StuID,StuName from Stuinfo S left join scoreInfo I on S.StuID = I.stuID where score is NULL +--8 .查询出是周六周天来报到的学生 +select * from stuInfo where stuTime='星期六' or stuTime='星期天' +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select S.stuID,avg(score),count(*) from scoreInfo S inner join stuInfo I on I.stuID = S.stuID inner join courseInfo C on C.courseID = S.courseID group by S.stuID having count(*)>2 and avg(score)>70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\351\223\255\350\275\251/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\351\223\255\350\275\251/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d4a6965ad26efb860fdd78e289f005b427ba5ea6 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\351\223\255\350\275\251/SQLQuery1.sql" @@ -0,0 +1,108 @@ +use master +go +create database Student + +on( +name = 'Student', +filename='D:\Student01.mdf', +size=5, +maxsize=10, +filegrowth=10% + +) +log on( +name = 'Student_log', +filename='D:\Student01_log.ldf', +size=5, +maxsize=10, +filegrowth=10% + +) +go + + + use Student + + go + + create table stuInfo + ( + stuId int primary key identity (1,1) not null, + stuname varchar(10) not null, + stuAge int not null, + stuSex char(1) check(stuSex=0 or stuSex=1) not null, + time date + ) + + create table courseInfo + ( + courseId int primary key identity (1,1) not null, + courseName varchar(10) not null, + courseMaxs int not null, + + ) + create table scoreInfo + ( + scoreLd int primary key identity (1,1) not null, + stuId int, + courseId int, + score int + + ) + go + insert into stuInfo values('Tom',19,1,null), + ('Jack',20,0,null), + ('Rose',21,1,null), + ('Luli',19,1,null), + ('Lili',21,0,null), + ('abc',20,1,'2007-01-07 01:11:36.590') + + insert into courseInfo values + ('JayaBase',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 * from courseInfo +select * from scoreInfo +select * from stuInfo +select stuname 学生, courseName 选修课程,courseMaxs 课程的数量,avg (score)考试平均分 from courseInfo s inner join scoreInfo r on s.courseId=r.courseId inner join stuInfo I on r.courseId=I.stuId group by stuname, courseName ,courseMaxs ,score +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName 课程名称,COUNT(*) 选修学生个数,sum(score)考试总分 from scoreInfo s inner join stuInfo i on i.stuID = s.stuID inner join courseInfo c on c.courseID = s.courseID group by courseName +--3.查询出性别一样并且年龄一样的学生的信息 +select A.stuName 学生, A.stuSex 性别,A.stuAge 年龄 from stuInfo A inner join stuInfo B on (A.stuID <> B.stuID) and(A.stuSex = B.stuSex) and (A.stuAge = B.stuAge) +--4.查询出学分一样的课程信息 +select distinct A.courseName,A.courseMaxs from courseInfo A inner join courseInfo B on (A.courseID <> B.courseID) and +(A.courseMaxs = B.courseMaxs) + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select A.stuID 学号,A.stuName 姓名,C.courseID 课程号,C.score 分数 from stuInfo A inner join scoreInfo C on A.stuID = C.stuID + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select C.stuID 学号,C.courseID 课程号,A.courseName 课程名,A.courseMaxs 课程学分, C.score 分数 from courseInfo A inner join scoreInfo C on A.courseID = C.courseID + +--7.查询出没有参加考试的 学生的学号 和 姓名 +select A.stuID 学号,A.stuName 姓名 from stuInfo A inner join scoreInfo B on A.stuID = B.stuID where B.score is null + +--8.查询出是周六周天来报到的学生 +select * from stuInfo where datename(weekday,Time)='星期六' or datename(weekday,Time)='星期日' + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' + +--10.查询出选修了 2门课程以上的 并且 考试平均分在70以上 的学生的学号 和考试平均分 以及选修课程的数量 +select A.stuID 学号,COUNT(*)课程数量,AVG(score)平均分 from stuInfo A inner join scoreInfo B on A.stuID = B.stuID group by A.stuID having COUNT(*)>2 and AVG(score)>70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\345\211\221\351\230\263/.keep" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\345\211\221\351\230\263/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\345\211\221\351\230\263/work1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\345\211\221\351\230\263/work1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8fdcea01b7aae1df12b4f20a76a9b4090c70ba6b --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\345\211\221\351\230\263/work1.sql" @@ -0,0 +1,92 @@ +create database Students +on +( +name= 'students', +filename = 'D:\SQL.mdf', +size = 5, +maxsize = 10, +filegrowth = 10% +) +log on +( +name= 'students_log', +filename = 'D:\SQL_log.ldf', +size = 5, +maxsize = 10, +filegrowth = 10% +) +use Students +go +create table stuinfo +( +StuId int primary key identity, +StuName nvarchar(10) not null, +StuAge int, +StuSex int check(StuSex in(1,0)), +time datetime +) +create table courseinfo +( +CourseID int primary key identity, +CourseName nvarchar(10), +CourseMarks int +) +create table scoreinfo +( +ScoreID int primary key identity, +StuId int references stuinfo(StuId), +CourseID int references courseinfo(CourseID), +Score int +) +insert into stuinfo values('Tom',19,1,''), +('Jack',20,0,''), +('Rose',21,1,''), +('Lulu',19,1,''), +('Lili',21,0,''), +('abc',20,1,'2007-01-07 01:11:36.590') +insert into courseinfo values('JavaBase',4), +('HTML',2), +('JavaScript',2), +('SqlBase',2) +insert into scoreinfo 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 courseinfo +select * from scoreinfo +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select s.StuId,stuName,count(*) 选修课程数量,AVG(Score) 考试平均分 from stuinfo s +inner join scoreinfo sc on s.StuId=sc.StuId +inner join courseinfo c on sc.CourseID=c.CourseID group by s.StuId,stuName +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select c.CourseID,CourseName,COUNT(*) 选修学生个数,SUM(Score) 考试总分 from courseinfo c +inner join scoreinfo sc on c.CourseID=sc.CourseID +group by c.CourseID,CourseName +--3.查询出性别一样并且年龄一样的学生的信息 +select a.StuId,a.StuName,a.StuAge,a.StuSex from stuinfo a +inner join stuinfo b on a.StuSex=b.StuSex and b.StuAge=a.StuAge and a.StuName!=b.StuName +--4.查询出学分一样的课程信息 +select * from courseinfo a inner join courseinfo b on a.CourseMarks=b.CourseMarks and a.CourseID!=b.CourseID +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select s.stuId,StuName,c.CourseID,Score from StuInfo s right join scoreinfo sc on s.stuId=sc.StuId +inner join courseinfo c on sc.CourseID= c.CourseID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select s.stuId,c.CourseID,CourseName,CourseMarks,Score from StuInfo s right join scoreinfo sc on s.stuId=sc.StuId +inner join courseinfo c on sc.CourseID= c.CourseID +--7.查询出没有参加考试的学生的学号和姓名 +select s.stuId,StuName from stuinfo s left join scoreinfo sc on s.stuId=sc.StuId where score is null or score='' +--8.查询出是周六周天来报到的学生 +select * from StuInfo where datename(weekday,Time) ='星期六' or datename(weekday,Time) = '星期天' +--9.查询出姓名中有字母a的学生的信息 +select * from StuInfo where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select s.stuId,AVG(Score) 平均分,COUNT(*)选修课程数量 from StuInfo s inner join scoreinfo sc on s.stuId=SC.StuId +inner join courseinfo c on sc.CourseID=c.CourseID group by s.stuId having COUNT(*)>2 and AVG(Score)>70 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\233\271\346\226\207\346\226\214/SQLQuery11.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\233\271\346\226\207\346\226\214/SQLQuery11.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3ae09b966e6d3252416561195374e200808b1208 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\233\271\346\226\207\346\226\214/SQLQuery11.sql" @@ -0,0 +1,92 @@ +use master +go + +create database student +go + +use student +go + +create table stuInfo +( + stuID int primary key identity(1,1) not null, + stuName char(4)unique not null, + stuAge char(2)not null, + stuSex char(1) default(1) check(stuSex='1' or stuSex='0'), + time date +) + +create table courseInfo +( + courseID int primary key identity(1,1) not null, + courseName char(10) not null, + courseMarks char(1) not null +) + +create table scoreInfo +( + scoreID int primary key identity(1,1) not null, + stuID int references stuInfo(stuID), + courseID int references courseInfo(courseID), + score int not null +) + +insert into stuInfo(stuName,stuAge,stuSex,time) values +('Tom','19','1',''), +('Jack','20','0',''), +('Rose','21','1',''), +('Lulu','19','1',''), +('Lili','21','0',''), +('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 courseInfo +select * from scoreInfo +select * from stuInfo +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) + +--题目: +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分scoreInfo +select stuName,count(*),avg(score) from courseInfo inner join scoreInfo on +courseInfo.courseID=scoreInfo.courseID inner join stuInfo on scoreInfo.stuID=stuInfo.stuID group by stuName +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName,count(*)个数,sum(score)总分 from courseInfo inner join scoreInfo on courseInfo.courseID=scoreInfo.courseID +inner join stuInfo on scoreInfo.stuID=stuInfo.stuID group by courseName +--3.查询出性别一样并且年龄一样的学生的信息 +select * from stuInfo A left join stuInfo B on A.stuID<>B.stuID and A.stuSex=B.stuSex and A.stuAge=B.stuAge +--4.查询出学分一样的课程信息 +select * from courseInfo A inner join courseInfo B on A.courseMarks = B.courseMarks and A.courseID<>B.courseID +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select scoreInfo.stuID,stuName,scoreInfo.courseID,score from courseInfo inner join scoreInfo on +courseInfo.courseID=scoreInfo.courseID inner join stuInfo on scoreInfo.stuID=stuInfo.stuID group by scoreInfo.stuID,stuName,scoreInfo.courseID,score +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select scoreInfo.stuID,scoreInfo.courseID,courseName,courseMarks,score from courseInfo inner join scoreInfo on +courseInfo.courseID=scoreInfo.courseID inner join stuInfo on scoreInfo.stuID=stuInfo.stuID group by +scoreInfo.stuID,scoreInfo.courseID,courseName,courseMarks,score +--7.查询出没有参加考试的学生的学号和姓名 +select stuInfo.stuID,stuName from stuInfo left join scoreInfo on stuInfo.stuID=scoreInfo.stuID group by stuInfo.stuID,stuName +--8.查询出是周六周天来报到的学生 +select * from StuInfo where datename(weekday,Time)='星期六' or datename(weekday,Time)='星期日' +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuInfo.stuID,avg(score),count(scoreInfo.courseID) from stuInfo +inner join scoreInfo on stuInfo.stuID=scoreInfo.stuID inner join +courseInfo on courseInfo.courseID=scoreInfo.courseID group by stuInfo.stuID having count(scoreInfo.courseID)>2 and avg(score)>70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\233\276\344\273\216\346\235\234/SQLQueryZ1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\233\276\344\273\216\346\235\234/SQLQueryZ1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9475cf4ab5a6936ec996b06d9fb52072d03baa81 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\233\276\344\273\216\346\235\234/SQLQueryZ1.sql" @@ -0,0 +1,68 @@ +use master +go +create database Srudents +go +create table stuinfo +( +stuID int primary key identity, +stuName varchar(10), +stuAge int , +StuSex int check(stuSex=1 or stuSex=0), +time date +) +create table courseInfo +( +courseID int primary key identity, +courseName varchar(10), +courseMarks int +) +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) +values +('Tom',19,1), +('Jack',20,0), +('Rose',21,1), +('Lulu',19,1), +('Lili',21,0), +('abc',20,1) +update stuinfo set time='2007-01-07 01:11:36.590'where stuID=6 +insert into +courseInfo(courseName,courseMarks) +values +('JavaBase',4), +('HTML',2), +('JavaScript',2), +('SqkBase',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 stuID,count(courseID) 所修课程数量,avg(score) 所修课程平均分 from scoreInfo group by stuID +select courseID 课程,count(stuID) 选修课程人数,sum(score) 总分 from scoreInfo group by courseID +select * from stuinfo a where EXISTS (select stuAge,StuSex,count(stuAge),count(StuSex) from stuinfo b where a.stuAge=b.stuAge and a.StuSex=b.StuSex group by stuAge,StuSex having count(stuAge)>1 and count(StuSex)>1) +select * from courseInfo where courseMarks in ( select courseMarks from courseInfo group by courseMarks having count(courseMarks)>1) +select scoreInfo.stuID,stuinfo.stuName, courseID,score from scoreInfo inner join stuinfo on stuinfo.stuID=scoreInfo.stuID +select scoreInfo.stuID,courseInfo.courseID,courseName,courseMarks,score from scoreInfo inner join courseInfo on courseInfo.courseID=scoreInfo.courseID +--9到10题 +select * from stuinfo where stuName like '%a%' +select stuID, avg(score) 平均分,count(courseID) 课程数 from scoreInfo group by stuID having avg(score)>70 and count(courseID)>2 +select * from stuinfo +select * from courseInfo +select * from scoreInfo \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\344\274\237/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\344\274\237/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..2d50e5855d84640f8fb676a1b47175f726c95f7d --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\344\274\237/SQLQuery1.sql" @@ -0,0 +1,97 @@ +use master +go +create database ccc +on +( +name='ccc', +filename='D:\ccc\text.mdf', +size=5mb, +maxsize=50mb, +filegrowth=10% +) +log on +( +name='ccc_log', +filename='D:\ccc\text_log.ldf', +size=5mb, +maxsize=50mb, +filegrowth=10% +) +use ccc +go +create table StuInfo +( +StuId int primary key identity(1,1) not null, +StuName char(10) not null, +StuAge int not null, +StuSex nvarchar(2) not null, +time datetime +) +create table CourseInfo +( +CourseId int primary key identity(1,1) not null, +CourseName char(10) not null, +Coursemarks int not null +) +create table ScoreInfo +( +ScoreId int primary key identity(1,1) not null, +StuId int not null references StuInfo(StuId), +CourseId int not null references CourseInfo(CourseId), +Score int not null +) +insert into StuInfo 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') +select * from StuInfo +insert into CourseInfo values +('JaveBase',4), +('HTML',2), +('IavaScript',2), +('SqlBase',2) +select * from CourseInfo +insert into ScoreInfo 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 ScoreInfo +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuName,count(*),avg(score) from courseInfo inner join scoreInfo on +courseInfo.courseID=scoreInfo.courseID inner join stuInfo on scoreInfo.stuID=stuInfo.stuID group by stuName +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName 课程名称,COUNT(scoreInfo.courseID) 学生个数,SUM(score) 学生考试的总分 from courseInfo inner join scoreInfo on courseInfo.courseID = scoreInfo.courseID GROUP BY scoreInfo.courseID,courseName +--3.查询出性别一样并且年龄一样的学生的信息 + select * from stuInfo A join stuInfo B on A.stuName<>B.stuName and A.stuAge=B.stuAge and A.stusex=B.stusex +--4.查询出学分一样的课程信息 +select * from courseInfo A inner join courseInfo B on A.courseMarks = B.courseMarks and A.courseID<>B.courseID +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuInfo.stuID 学生学号,stuName 学生姓名,courseID 课程号,score 分数 +from stuInfo inner join scoreInfo on stuInfo.stuID = scoreInfo.stuID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuInfo.stuID 学生学号,stuName 学生姓名,scoreInfo.courseID 课程号,courseMarks 课程学分,score 分数 +from scoreInfo inner join stuInfo on scoreInfo.stuID = stuInfo.stuID inner join courseInfo +on scoreInfo.courseID = courseInfo.courseID +--7.查询出没有参加考试的学生的学号和姓名 +select stuInfo.stuID,stuName +from stuInfo left join scoreInfo +on stuInfo.stuID=scoreInfo.stuID group by stuInfo.stuID,stuName +--8.查询出是周六周天来报到的学生 +select * from StuInfo where datename(weekday,Time)='星期六' or datename(weekday,Time)='星期日' +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuInfo.stuID,avg(score),count(scoreInfo.courseID) from stuInfo +inner join scoreInfo on stuInfo.stuID=scoreInfo.stuID inner join +courseInfo on courseInfo.courseID=scoreInfo.courseID group by stuInfo.stuID having count(scoreInfo.courseID)>2 and avg(score)>70 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\350\213\261\347\276\244/.keep" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\350\213\261\347\276\244/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\350\213\261\347\276\244/\344\275\234\344\270\23211.txt" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\350\213\261\347\276\244/\344\275\234\344\270\23211.txt" new file mode 100644 index 0000000000000000000000000000000000000000..c4e035b604f64165d133813c0b9c1bbbfc491479 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\350\213\261\347\276\244/\344\275\234\344\270\23211.txt" @@ -0,0 +1,74 @@ + +create database aji +go +use aji +go + +create table stuInfo +( + stuID int primary key identity(1,1), + stuName nvarchar(10) not null, + stuAge char(3) not null, + stusex char(2) not null, + time date +) +create table courseInfo +( + courseID int primary key identity(1,1), + courseName nvarchar(10) not null, + courseMarks int not null +) +create table scoreInfo +( + scoreID int primary key identity(1,1), + stuID int references stuInfo(stuID) not null, + courseID int references courseInfo(courseID) not null, + score int , +) +go + +insert into stuInfo 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 values('JavaBase',4),('HTML',2),('JavaScript',2),('SqlBase',2) + +insert into scoreInfo 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) +go +--鏈夊鍥炬墍绀虹殑涓夊紶琛ㄧ粨鏋勶紝瀛︾敓淇℃伅琛紙stuInfo锛夛紝璇剧▼淇℃伅琛紙courseInfo锛,鍒嗘暟淇℃伅琛紙scoreInfo锛 +select * from stuInfo +select * from courseInfo +select * from scoreInfo +--棰樼洰锛 +--1.鏌ヨ鍑烘瘡涓鐢熸墍閫変慨鐨勮绋嬬殑鏁伴噺鍜屾墍閫変慨鐨勮绋嬬殑鑰冭瘯鐨勫钩鍧囧垎 +select s.stuID,stuName, count(courseName) 璇剧▼鏁伴噺 ,avg(score) 骞冲潎鍒嗘暟 from scoreInfo s +inner join stuInfo stu on s.stuID = stu.stuID +inner join courseInfo c on s.courseID=c.courseID group by s.stuID ,stuName +--2.鏌ヨ鍑 姣忛棬璇剧▼鐨勯変慨鐨勫鐢熺殑涓暟 鍜 瀛︾敓鑰冭瘯鐨勬诲垎 +select c.courseID,courseName,count(s.stuID) 瀛︾敓鐨勪釜鏁,sum(score) 鎬诲垎 from scoreInfo s +inner join stuInfo stu on s.stuID=stu.stuID +inner join courseInfo c on s.courseID=c.courseID group by c.courseID,courseName +--3.鏌ヨ鍑烘у埆涓鏍峰苟涓斿勾榫勪竴鏍风殑瀛︾敓鐨勪俊鎭 +select * from stuInfo s1 join stuInfo s2 on s1.stuName<>s2.stuName and s1.stuAge=s2.stuAge and s1.stusex=s2.stusex +--鍙︿竴绉嶅仛娉晄elect * from stuInfo t where (select COUNT(*) from stuInfo where stuAge=t.stuAge and stuSex=t.stuSex)>1 +--4.鏌ヨ鍑哄鍒嗕竴鏍风殑璇剧▼淇℃伅 +select * from courseInfo c1 join courseInfo c2 on c1.courseID!=c2.courseID and c1.courseMarks=c2.courseMarks +--5.鏌ヨ鍑哄弬鍔犱簡鑰冭瘯鐨 瀛︾敓鐨勫鍙凤紝濮撳悕锛岃绋嬪彿 鍜 鍒嗘暟 +select s.stuID,stuName,c.courseID,score from scoreInfo s +inner join stuInfo stu on s.stuID=stu.stuID +inner join courseInfo c on s.courseID=c.courseID +--6.鏌ヨ鍑哄弬鍔犱簡鑰冭瘯鐨勫鐢熺殑瀛﹀彿锛岃绋嬪彿锛岃绋嬪悕锛岃绋嬪鍒嗗拰鍒嗘暟 +select s.stuID,stuName,c.courseID,courseMarks,score from scoreInfo s +inner join stuInfo stu on s.stuID=stu.stuID +inner join courseInfo c on s.courseID=c.courseID +--7.鏌ヨ鍑烘病鏈夊弬鍔犺冭瘯鐨勫鐢熺殑瀛﹀彿鍜屽鍚 + select * from stuInfo where stuID not in (select stuID from scoreInfo) + --鍙︿竴绉嶅仛娉晄elect * from stuInfo left join scoreInfo on stuInfo.stuID = scoreInfo.stuID where score is null +--8.鏌ヨ鍑烘槸鍛ㄥ叚鍛ㄥぉ鏉ユ姤鍒扮殑瀛︾敓 + +--9.鏌ヨ鍑哄鍚嶄腑鏈夊瓧姣峚鐨勫鐢熺殑淇℃伅 +select * from stuInfo where stuName like '%a%' +--10.鏌ヨ鍑洪変慨浜2闂ㄨ绋嬩互涓婄殑骞朵笖鑰冭瘯骞冲潎鍒嗗湪70浠ヤ笂鐨 瀛︾敓鐨勫鍙 鍜 鑰冭瘯骞冲潎鍒 浠ュ強 閫変慨璇剧▼鐨勬暟閲 +select s.stuID,stuName,avg(score)骞冲潎鍒,count(c.courseID)璇剧▼鏁 from scoreInfo s +inner join stuInfo stu on s.stuID=stu.stuID +inner join courseInfo c on s.courseID=c.courseID group by s.stuID,stuName having avg(score)>70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\344\270\226\347\221\236/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\2321.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\344\270\226\347\221\236/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\2321.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d591c725773e90f214f256729e710073d5b9bb25 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\344\270\226\347\221\236/\347\254\254\345\215\201\344\270\200\346\254\241\344\275\234\344\270\2321.sql" @@ -0,0 +1,131 @@ +use master +go + +create database Student +on +( + name=Student, + filename='E:\数据库文件\Student.mdf', + size=5MB, + maxsize=10MB, + filegrowth=10% +) +log on +( + name=Student_log, + filename='E:\数据库文件\Student_log.ldf', + size=5MB, + maxsize=10MB, + filegrowth=10% +) +go + +use Student +go + +create table stuInfo +( + stuID int identity(1,1), + stuName varchar(30), + stuAge varchar(5), + stuSex varchar(6), + time datetime +) +go + +insert into stuInfo(stuName,stuAge,stuSex) + select 'Tom',19,1 union + select 'Jack',20,0 union + select 'Rose',21,1 union + select 'Lulu',19,1 union + select 'Lili',21,0 union + select 'abc',20,1 +go + +update stuInfo set time='2007-01-07 01:11:36.590' where stuID=6 +go + +create table courseInfo +( + courseID int identity(1,1), + courseName varchar(10), + courseMarks varchar(6) +) +go + +insert into courseInfo(courseName,courseMarks)values('JavaBase',4),('HTML',2),('JavaScript',2),('SqlBase',2) +go + +create table scoreInfo +( + scoreID int identity(1,1), + stuID int, + courseID int, + score int, +) +go + +insert into scoreInfo(stuID,courseID,score) +select 1,1,80 union +select 1,2,85 union +select 1,4,50 union +select 2,1,75 union +select 2,3,45 union +select 2,4,75 union +select 3,1,45 union +select 4,1,95 union +select 4,2,75 union +select 4,3,90 union +select 4,4,45 +go + +--题目: +--1.查询出每个学生 所选修的课程的数量 和所选修的课程的考试的平均分 + +select stuName 学生姓名,count(s.stuID)课程数量,AVG(score)考试平均分 from scoreInfo s +inner join stuInfo i on i.stuID=s.stuID +group by s.stuID,stuName + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 + +select C.courseName 课程名称,count(S.courseID) 学生个数,sum(S.score) 考试总分 from courseInfo C +inner join scoreInfo S on C.courseID=S.courseID +group by C.courseID,courseName + +--3.查询出性别一样并且年龄一样的学生的信息 +select * from stuInfo A +left join stuInfo B on A.stuAge=B.stuAge and A.stuSex=B.stuSex and A.stuID!=B.stuID + + +--4.查询出学分一样的课程信息 +select * from courseInfo A +left join courseInfo B on B.courseMarks=A.courseMarks where A.courseID!=B.courseID + + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select A.stuID 学号,stuName 姓名,courseID 课程号,score 分数 from stuInfo A +right join scoreInfo B on A.stuID=B.stuID + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 + +select A.stuID 学生学号,C.courseID 课程号,C.courseName,C.courseMarks,score from stuInfo A +right join scoreInfo B on A.stuID=B.stuID +left join courseInfo C on C.courseID=B.courseID + +--7.查询出没有参加考试的学生的学号和姓名 + +select B.stuID 学号,B.stuName 姓名 from scoreInfo A +right join stuInfo B on A.stuID=B.stuID where score is NULL + +--8.查询出是周六周天来报到的学生 + + +--9.查询出姓名中有字母a的学生的信息 + +select * from stuInfo where stuName like '%a%' + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的 学号 和 考试平均分以及 选修课程的数量 + +select stuID 学生学号,AVG(score)考试平均分,COUNT(stuID)课程数量 from scoreInfo +group by stuID +having COUNT(stuID)>=2 and AVG(score)>70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\200\235\345\260\271/.keep" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\200\235\345\260\271/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\200\235\345\260\271/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\200\235\345\260\271/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..61ee4b680251647f65aec0304163fc14b481e479 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\200\235\345\260\271/SQLQuery2.sql" @@ -0,0 +1,104 @@ +Use Master +go +Create Database student +on +(name='student', + filename='D:\student.mdf', + size=10mb, + maxsize=100mb, + filegrowth=10% + ) + log on + (name='student_log', + filename='D:\student_log.ldf', + size=10mb, + maxsize=100mb, + filegrowth=10% + ) + go +Use student +go +CREATE table stuInfo +( + stuID int primary key identity, + stuName varchar(20) not null, + stuAge int not null, + stuSex int check(stuSex in (0,1)), + time datetime + ) +go +create table courseInfo +(courseID int primary key identity, + courseName varchar(20) not null, + courseMarks int not null + ) + go + Create table scoreInfo + (scoreID int primary key identity, + stuID int references stuInfo(stuID) not null, + courseID int references courseInfo(courseID) not null, + score int not null + ) + go + insert into stuInfo 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') + go + insert into courseInfo values + ('JavaBase',4), + ('HTML',2), + ('JavaScript',2), + ('SqlBase',2) + go + insert into scoreInfo 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) +go +select* from stuInfo +select* from courseInfo +select* from scoreInfo +--题目: +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 + +select i.stuName ,count(*)数量 ,avg(score)平均分 from scoreInfo s JOIN stuInfo i on i. stuID= s .stuID group by i.stuName + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select c.courseName, count(*) '选修的学生个数', sum(score) '总分'from courseInfo c join scoreInfo s on c.courseID = s.courseID group by c.courseName +--3.查询出性别一样并且年龄一样的学生的信息 +select s1.* from stuInfo s1 join stuInfo s2 on s1.stuName<>s2.stuName and s1. stuSex=s2.stuSex and s1.stuAge =s2.stuAge + + +--4.查询出学分一样的课程信息 +select distinct c1.* from courseInfo c1 join courseInfo c2 on (c1.courseID <> c2.courseID) and (c1.courseMarks = c2.courseMarks ) + + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select st.stuID, st.stuName, s.courseID, s.score from scoreInfo s join stuInfo st on s.stuID = st.stuID + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select s.stuID, s.courseID, c.courseName, c.courseMarks , s.score from scoreInfo s join courseInfo c on c.courseID = s.courseID +--7.查询出没有参加考试的学生的学号和姓名 +select st.stuID, st.stuName from stuInfo st left join scoreInfo sc on sc.stuID = st.stuID where sc.scoreID is null + +--8.查询出是周六周天来报到的学生 +select * from stuInfo where DATENAME(dw, time) = '星期六' or DATENAME(dw, time) = '星期日' + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuID '学号', round(avg(cast(score as float)), 1) '平均成绩', count(*) '选课数量' from scoreInfo group by stuID having count(*) > 2 and round(avg(cast(score as float)), 1) > 70 + \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\345\263\260/.keep" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\345\263\260/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\345\263\260/3.30\344\275\234\344\270\232.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\345\263\260/3.30\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5baed16f0f0a78f6a6745632d9030a704ec7d82f --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\345\263\260/3.30\344\275\234\344\270\232.sql" @@ -0,0 +1,75 @@ +use master +go +create database student +on +( + name='student', + filename='E:\sql\student.mdf', + size=10MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name='student_log', + filename='E:\sql\student_log.ldf', + size=10MB, + maxsize=50MB, + filegrowth=10% +) +go +use student +go +create table stuInfo +( + stuID int primary key, + stuName varchar(20), + stuAge varchar(2), + stuSex varchar(1) check(stuSex=1 or stuSex=0), + time varchar(50) +) +create table courseInfo +( + courseID int primary key, + courseName varchar(20), + courseMarks char(1) +) +create table scoreInfo +( + scoreID int primary key, + stuID int references stuInfo(stuID), + courseID int references courseInfo(courseID), + score int +) +insert into stuInfo (stuID,stuName,stuAge,stuSex,time) +values(1,'Tom',19,1,null),(2,'Jack',20,0,null),(3,'Rose',21,1,null),(4,'Lulu',19,1,null),(5,'Lili',21,0,null),(6,'abc',20,1,'2007-01-07 01:11:36.590') + +insert into courseInfo(courseID,courseName,courseMarks) +values (1,'JavaBase',4),(2,'HTML',2),(3,'JavaScri',2),(4,'SqlBase',2) + +insert into scoreInfo(scoreID,stuID,courseID,score) +values (1,1,1,80),(2,1,2,85),(3,1,4,50),(4,2,1,75),(5,2,3,45),(6,2,4,75),(7,3,1,45),(8,4,2,75),(9,4,2,75),(10,4,3,90),(11,4,4,45) + +select * from stuInfo +select * from courseInfo +select * from scoreInfo +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuName,count(*)课程数量,round (AVG(cast(score as float)),1)平均分 from scoreInfo join stuInfo on stuInfo.stuID = stuInfo.stuID group by stuName +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName count(*),sum(score) from courseInfo join scoreInfo on courseInfo.courseID = scoreInfo.courseID group by courseName +--3.查询出性别一样并且年龄一样的学生的信息 +select * from stuInfo s1 join stuInfo s2 on s1.stuID = s2.stuID and s1.stuSex= s2.stuSex and s1.stuAge= s2.stuAge +--4.查询出学分一样的课程信息 +select * from courseInfo c1 join courseInfo c2 on c1.courseMarks=c2.courseMarks +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuInfo.stuID,stuName,courseID,score from stuInfo join scoreInfo on stuInfo.stuID=stuInfo.stuID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuInfo.stuID,stuName,score from stuInfo join scoreInfo on stuInfo.stuID=stuInfo.stuID join courseInfo on courseInfo.courseID=courseInfo.courseID +--7.查询出没有参加考试的学生的学号和姓名 +select stuInfo.stuID,stuName from stuInfo join scoreInfo on stuInfo.stuID not in (stuInfo.stuID) +--8.查询出是周六周天来报到的学生 +select stuInfo.stuName from stuInfo where datepart (weekday,getdate())>5 +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like ('%a%') +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuID,count(courseID)选修课程数量,AVG(score)平均分 from scoreInfo group by stuID having COUNT(courseID) >2 and AVG(score) >70 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\345\263\260/3.31\344\275\234\344\270\232.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\345\263\260/3.31\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3f66cdeabbf13754d1c7ccd4e7355d7469eee492 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\345\263\260/3.31\344\275\234\344\270\232.sql" @@ -0,0 +1,170 @@ +use master +go +create database wb +on +( + name='wb', + filename='D:\sql\wb.mdf', + size=10MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name='wb_log', + filename='D:\sql\wb_log.ldf', + size=10MB, + maxsize=50MB, + filegrowth=10% +) +go +use wb +go +create table tbl_card +( + cardid varchar(10) primary key, + passWord nvarchar(20), + balance money , + userName nvarchar(2) +) +create table tbl_computer +( + computerid varchar(4) primary key, + onUse int check(onUse=0 or onUse=1), + note text +) +create table tbl_record +( + id int primary key, + cardId varchar(10) references tbl_card(cardid), + ComputerId varchar(4) references tbl_computer(computerid), + beginTime time, + endTime time, + fee money +) +go +insert into tbl_card(cardid,passWord,balance,userName) +select '0023_ABC','555','98','张军'union +select '0025_bbd','abe','300','朱俊'union +select '0036_CCD','何柳','100','何柳'union +select '0045_YGR','0045_YGR','58','证验'union +select '0078_RJV','55885fg','600','校庆'union +select '0089_EDE','zhang','134','张峻' + +insert into tbl_computer(computerid,onUse,note) +select'02','0','25555'union +select'03','1','55555'union +select'04','0','66666'union +select'05','1','88888'union +select'06','0','688878'union +select'B01','0','558558' + +insert into tbl_record(id,cardId,ComputerId,beginTime,endTime,fee) +select'23','0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20'union +select'34','0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00','23'union +select'45','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00','50'union +select'46','0023_ABC','03','2006-12-22 15:26:00','2006-12-22 22:55:00','6'union +select'47','0023_ABC','03','2006-12-23 15:26:00','2006-12-23 15:55:00','50'union +select'48','0023_ABC','03','2007-01-06 15:26:00','2007-01-06 22:55:00','6'union +select'55','0023_ABC','03','2006-07-21 15:26:00','2006-07-21 22:55:00','50'union +select'64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','36'union +select'65','0025_bbd','02','2006-12-28 18:00:00','2006-12-28 22:00:00','23'union +select'98','0025_bbd','02','2006-12-26 18:00:00','2006-12-26 22:00:00','23' + +select * from tbl_record +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select Tc.userName ,Tr.* from tbl_record Tr inner join tbl_card Tc on userName='张军' order by Tr.fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 +select tbl_computer.computerid,count(id)上网次数,sum(fee)消费总金额 from tbl_computer inner join tbl_record on tbl_computer.computerid=tbl_record.ComputerId group by tbl_computer.computerid +--3. 查询出所有已经使用过的上网卡的消费总金额 +select tbl_card.cardid,sum(fee)消费总金额 from tbl_card inner join tbl_record on tbl_card.cardid=tbl_record.cardId group by tbl_card.cardid +--4. 查询出从未消费过的上网卡的卡号和用户名 +select + tc.id, tc.userName +from + tbl_card tc +left join + tbl_record tr +on + tr.cardId = tc.id +where + tr.id is null + +--5. 将密码与用户名一样的上网卡信息查询出来 +select + * +from + tbl_card t1 +join + tbl_card t2 +on + (t1.id = t2.id) and (t1.[passWord] = t2.userName) + +--6. 查询出使用次数最多的机器号和使用次数 +select + ComputerId, count(*) '使用次数' +from + tbl_record +group by + ComputerId +having + count(*) = (select max(t.使用次数) from (select ComputerId, count(*) '使用次数' from tbl_record group by ComputerId) t) + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select + tc.id, tc.userName, tr.ComputerId, tr.fee +from + tbl_record tr +join + tbl_card tc +on + tr.cardId = tc.id +where + tr.cardId like '%ABC' + + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select + tc.id, tc.userName, tr.ComputerId, tr.beginTime, tr.endTime, tr.fee +from + tbl_record tr +join + tbl_card tc +on + tr.cardId = tc.id +where + DATENAME(dw, tr.beginTime) in ('星期六','星期日') +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select + tc.id, tc.userName, tr.ComputerId, tr.beginTime, tr.endTime, tr.fee +from + tbl_record tr +join + tbl_card tc +on + tr.cardId = tc.id +where + DATEDIFF(HH, tr.beginTime, tr.endTime) > 12 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select + tc.id, tc.userName, tr.ComputerId, tr.beginTime, tr.endTime, tr.fee +from + tbl_record tr +join + tbl_card tc +on + tr.cardId = tc.id +where + tr.fee in ( + select + distinct top 3 fee + from + tbl_record + order by + fee desc + ) +order by + tr.fee desc + diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\242\201\344\270\226\350\261\252/3.30.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\242\201\344\270\226\350\261\252/3.30.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a08aa6df88c126084966ab74d0286a03bfe4ffc2 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\242\201\344\270\226\350\261\252/3.30.sql" @@ -0,0 +1,86 @@ +use master +go +create database Student +on( + name = 'Student.mdf', + filename='D:\Student.mdf', + size=5, + maxsize=10, + filegrowth=15% +) +log on( + name = 'Student_log.ldf', + filename='D:\Student_log.ldf', + size=5, + maxsize=10, + filegrowth=15% +) +go +use Student +go +create table stuInfo +( + stuID int primary key identity(1,1), + stuName nvarchar(7) not null, + stuAge int not null, + stuSex nvarchar(1) not null, + time datetime +) +create table courseInfo +( + courseID int primary key identity(1,1), + courseName nvarchar(10) not null, + courseMarks nvarchar(5) +) +create table scoreInfo +( + scoreID int primary key identity(1,1), + stuID int references stuInfo(stuID), + courseID int references courseInfo(courseID), + score int not null +) +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','95') +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) +--题目: +select * from stuInfo +select * from courseInfo +select * from scoreInfo + +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuName 学生 ,count(courseInfo.courseID) 课程数量,AVG(score) 平均分 from stuInfo inner join scoreInfo +on stuInfo.stuID=scoreInfo.stuID inner join courseInfo on courseInfo.courseID=scoreInfo.courseID group by + stuName +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName 课程名称, COUNT(scoreInfo.courseID) 学生个数 ,sum(score) 总分 from stuInfo inner join scoreInfo +on stuInfo.stuID=scoreInfo.stuID inner join courseInfo on courseInfo.courseID=scoreInfo.courseID group by + scoreInfo.courseID,courseName +--3.查询出性别一样并且年龄一样的学生的信息 +select * from stuInfo a left join stuInfo b on a.stuSex=b.stuSex and a.stuAge=b.stuAge +--4.查询出学分一样的课程信息 +select * from courseInfo A inner join courseInfo B on A.courseMarks = B.courseMarks and A.courseID<>B.courseID +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuInfo.stuID 学号,stuInfo.stuName 姓名,courseInfo.courseID 课程号, scoreInfo.score 分数 from stuInfo inner join scoreInfo +on stuInfo.stuID=scoreInfo.stuID inner join courseInfo on courseInfo.courseID=scoreInfo.courseID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuInfo.stuID 学号,courseInfo.courseID 课程号,courseInfo.courseMarks 课程学分, scoreInfo.score 分数 from stuInfo inner join scoreInfo +on stuInfo.stuID=scoreInfo.stuID inner join courseInfo on courseInfo.courseID=scoreInfo.courseID +--7.查询出没有参加考试的学生的学号和姓名 +select stuInfo.stuID 学号,stuInfo.stuName 姓名 from stuInfo inner join scoreInfo +on stuInfo.stuID=scoreInfo.stuID inner join courseInfo on courseInfo.courseID=scoreInfo.courseID where +stuInfo.stuID is null and stuInfo.stuName is null +--8.查询出是周六周天来报到的学生 +select * from StuInfo where datename(weekday,time)='星期六' or datename(weekday,time)='星期日' +--9.查询出姓名中有字母a的学生的信息 +select * from StuInfo where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuInfo.stuID 学号,avg(scoreInfo.score) 考试平均分,COUNT(courseInfo.courseID) 选修课程的数量 from stuInfo inner join scoreInfo +on stuInfo.stuID=scoreInfo.stuID inner join courseInfo on courseInfo.courseID=scoreInfo.courseID +GROUP BY stuInfo.stuID HAVING COUNT(courseInfo.courseID)>=2 +AND avg(scoreInfo.score)>=70 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\242\201\351\223\266\350\212\235/.keep" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\242\201\351\223\266\350\212\235/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\242\201\351\223\266\350\212\235/\346\242\201\351\223\266\350\212\235/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\242\201\351\223\266\350\212\235/\346\242\201\351\223\266\350\212\235/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8a8ddc03d456d0cab1cb8c59f9e997ad6f720fe1 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\242\201\351\223\266\350\212\235/\346\242\201\351\223\266\350\212\235/SQLQuery1.sql" @@ -0,0 +1,112 @@ +use master +go + +create database Student +on +( +name='Student', + filename='D:\text\Student.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +log on +( +name='Students_log', + filename='D:\text\Student_log.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +go + +use Student +go + +create table stuInfo +( +stuID int primary key identity, +stuName varchar(10), +stuAge varchar(5), +stuSex varchar(1), +[time] datetime +) +go + +create table courseInfo +( +courseID int primary key identity, +courseName varchar(10), +courseMarks int +) +go + +create table scoreInfo +( +scoreID int primary key identity, +stuID int references stuInfo(stuID), +courseID int references courseInfo(courseID), +score int +) +go + +insert into stuInfo 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') +go + +insert into courseInfo values('JavaBase',4),('HTML',2),('JavaScript',2),('SqlBase',2) +go + +insert into scoreInfo 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) +go + +select * from stuInfo +select * from courseInfo +select * from scoreInfo +go + +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuName 学生姓名,COUNT(scoreInfo.stuID) 所选修的课程的数量,AVG(score) 所选修的课程的考试的平均分 + from stuInfo left join scoreInfo on stuInfo.stuID = scoreInfo.stuID GROUP BY stuName + go + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName 课程名称,COUNT(scoreInfo.courseID) 学生个数,SUM(score) 学生考试的总分 from courseInfo inner join scoreInfo on courseInfo.courseID = scoreInfo.courseID GROUP BY scoreInfo.courseID,courseName + go + + --3.查询出性别一样并且年龄一样的学生的信息 + --方法一 + select * from stuInfo t where (select COUNT(*) from stuInfo where stuAge=t.stuAge and stuSex=t.stuSex)>1 + --方法二 + select * from stuInfo s1 join stuInfo s2 on s1.stuName<>s2.stuName and s1.stuAge=s2.stuAge and s1.stusex=s2.stusex + go + +--4.查询出学分一样的课程信息 + select * from courseInfo c where (select COUNT(*) from courseInfo where courseMarks=c.courseMarks)>1 + go + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuInfo.stuID 学号,stuName 姓名,courseID 课程号,score 分数 from stuInfo inner join scoreInfo on stuInfo.stuID = scoreInfo.stuID +go + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuInfo.stuID 学号,stuName 姓名,scoreInfo.courseID 课程号,courseMarks 课程学分,score 分数 from scoreInfo inner join stuInfo on scoreInfo.stuID = stuInfo.stuID inner join courseInfo on scoreInfo.courseID = courseInfo.courseID +go + +--7.查询出没有参加考试的学生的学号和姓名 +--方法一 +select stuInfo.stuID 学号,stuName 姓名 from stuInfo left join scoreInfo on stuInfo.stuID = scoreInfo.stuID where score is null +--方法二 +select * from stuInfo where stuID not in (select stuID from scoreInfo) +go + +--8.查询出是周六周天来报到的学生 + + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +go + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的 学号 和 考试平均分以及 选修课程的数量 +select stuInfo.stuID 学号,AVG(score) 考试平均分,COUNT(courseID) 选修课程的数量 from stuInfo join scoreInfo on stuInfo.stuID = scoreInfo.stuID group by stuInfo.stuID having AVG(score)>70 and COUNT(courseID)>2 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\267\346\231\250\346\227\255/1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\267\346\231\250\346\227\255/1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5c3161a3abd5eb8e03762f27ab0a442a3ef5c0b7 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\267\346\231\250\346\227\255/1.sql" @@ -0,0 +1,23 @@ +select * from StuInfo +select * from CourseInfo +select * from ScoreInfo +--1.查询出 每个学生 所选修的课程的 数量和 所选修的课程的考试的 平均分 +select S.StuID 学生ID , COUNT(*) 数量 , AVG(Score) 平均分 from StuInfo S inner join ScoreInfo I on S.StuID=I.StuID group by S.StuID +--2.查询出 每门课程 的选修的学生的 个数 和学生考试的 总分 +select C.CourseID 课程ID , COUNT(*) 个数 , SUM(Score) 总分 from CourseInfo C inner join ScoreInfo I on C.CourseID=I.CourseID group by C.CourseID +--3.查询出 性别一样 并且 年龄一样 的学生的信息 +select * from StuInfo S,StuInfo I where S.StuAge=I.StuAge AND S.StuSex=I.StuSex and S.StuID<>I.StuID +--4.查询出 学分一样 的 课程信息 +select * from CourseInfo C,CourseInfo I where C.CourseMarks=I.CourseMarks and C.CourseID<>I.CourseID +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select S.StuID 学生学号 , StuName 姓名 , C.CourseID 课程号 , Score 分数 from ScoreInfo S inner join StuInfo I on S.StuID=I.StuID inner join CourseInfo C on S.CourseID=C.CourseID group by S.StuID,StuName,C.CourseID,Score +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select S.StuID 学生学号 , C.CourseID 课程号 , CourseName 课程名, CourseMarks 课程学分 , Score 分数 from ScoreInfo S inner join StuInfo I on S.StuID=I.StuID inner join CourseInfo C on S.CourseID=C.CourseID group by S.StuID,C.CourseID,CourseName,CourseMarks,Score +--7.查询出没有参加考试的学生的学号和姓名 +select S.StuID 学号 , StuName 姓名 from ScoreInfo I right join StuInfo S on I.StuID=S.StuID where Score is null or Score='' +--8.查询出是周六周天来报到的学生 +select * from StuInfo where datename(weekday,StuTime)='星期六' or datename(weekday,StuTime)='星期日' +--9.查询出姓名中有字母a的学生的信息 +select * from StuInfo where StuName like '%a%' or StuName like '%A%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select S.StuID 学号 , COUNT(*) 课程数量 , AVG(Score) 平均分 from ScoreInfo I inner join StuInfo S on I.StuID=S.StuID group by S.StuID having COUNT(*)>2 and AVG(Score)>70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\267\346\231\250\346\227\255/2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\267\346\231\250\346\227\255/2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ae9f2a1089b2c8b425f7c1d24cdae55236338973 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\267\346\231\250\346\227\255/2.sql" @@ -0,0 +1,101 @@ +use master +go +create database TBL +on +( + name='TBL', + filename='D:\SQL\TBL.mdf', + size=5MB, + maxsize=20MB, + filegrowth=10% +) +log on +( + name='TBL_log', + filename='D:\SQL\TBL_log.ldf', + size=5MB, + maxsize=20MB, + filegrowth=10% +) +go + +use TBL +go +create table TBL_Card +( + CardID char(30) primary key not null, + PassWord varchar(50) not null, + Balance money , + UserName nvarchar(20) +) + +create table TBL_Computer +( + CID char(30) primary key , + OnUse char(1) check(OnUse in(1,0)), + note text +) + +create table TBL_Record +( + RID int primary key , + CardID char(30) references TBL_Card(CardID), + CID char(30) references TBL_Computer(CID), + BeginTime datetime, + Endtime datetime, + fee money +) +go + +insert into TBL_Card values +('0023_ABC','555',98,'张军'), +('0025_bbd','abe',300,'朱俊'), +('0036_CCD','何柳',100,'何柳'), +('0045_YGR','00445_YGR',58,'证验'), +('0078_RJV','55885fg',600,'校庆'), +('0089_EDE','zhang',134,'张俊') + +insert into TBL_Computer values +('02','0','25555'), +('03','1','55555'), +('04','0','66666'), +('05','1','88888'), +('06','0','688878'), +('B01','0','558558') + +insert into TBL_Record values +(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-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:25:00',50), +(46,'0023_ABC','03','2006-12-23 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',20), +(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',3), +(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) +go + +select * from TBL_Card +select * from TBL_Computer +select * from TBL_Record +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select C.CardID 卡号 , UserName 用户名 , P.CID 机器编号 , BeginTime 开始时间 , Endtime 结束时间 , fee 消费金额 from TBL_Card C inner join TBL_Record R on C.CardID=R.CardID inner join TBL_Computer P on R.CID=P.CID where UserName='张军' order by fee DESC +--2. 查询出每台机器上的上网次数和消费的总金额 +select P.CID 机器编号 , COUNT(*) 次数 , SUM(fee) 总金额 from TBL_Computer P inner join TBL_Record R on P.CID=R.CID group by P.CID +--3. 查询出所有已经使用过上网卡的的消费总金额 +select C.CardID 卡号 , SUM(fee) 消费总金额 from TBL_Card C inner join TBL_Record R on C.CardID=R.CardID group by C.CardID +--4. 查询出从未消费过的上网卡的卡号和用户名 +select C.CardID 卡号 , UserName 用户名 from TBL_Card C left join TBL_Record R on C.CardID=R.CardID where fee is NULL or fee='' +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from TBL_Card C inner join TBL_Card A on C.PassWord=A.UserName +--6. 查询出使用次数最多的机器号和使用次数 +select C.CID 机器编号 , COUNT(*) 使用次数 from TBL_Record R inner join TBL_Computer C on R.CID=C.CID group by C.CID having C.CID=(select top 1 CID from TBL_Record group by CID order by COUNT(*) DESC) +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select C.CardID 卡号 , UserName 用户名 , CID 机器编号 , SUM(fee) 消费金额 from TBL_Card C inner join TBL_Record R on C.CardID=R.CardID where C.CardID like '%ABC' group by C.CardID,UserName,CID +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select C.CardID 卡号 , UserName 用户名 , CID 机器编号 , BeginTime 开始时间 , Endtime 结束时间 from TBL_Record R inner join TBL_Card C on R.CardID=C.CardID where datename(weekday,BeginTime)='星期六' or datename(weekday,BeginTime)='星期日' +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select C.CardID 卡号 , UserName 用户名 , CID 机器编号 , Begintime 开始时间 , Endtime 结束时间 , fee 消费金额 from TBL_Record R inner join TBL_Card C on R.CardID=C.CardID where datediff(hh,Endtime,BeginTime)>12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select C.CardID 卡号 , UserName 用户名 ,CID 机器编号 , BeginTime 开始时间, EndTime 结束时间 , fee 消费金额 from TBL_Record R join TBL_Card C on C.cardId = R.CardID where fee not in (select top 3 fee from TBL_Record order by fee DESC)order by fee desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\346\231\266\345\251\267/.keep" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\346\231\266\345\251\267/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\346\231\266\345\251\267/\344\275\234\344\270\232\344\270\200\344\273\243\347\240\201.txt" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\346\231\266\345\251\267/\344\275\234\344\270\232\344\270\200\344\273\243\347\240\201.txt" new file mode 100644 index 0000000000000000000000000000000000000000..bad5956540202f7b1c547e0ff9c91431643b8b71 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\346\231\266\345\251\267/\344\275\234\344\270\232\344\270\200\344\273\243\347\240\201.txt" @@ -0,0 +1,121 @@ +--浣滀笟涓 + +use master +go + +create database SQL11 +on +( + name='SQL11', + filename='D:\SQL11.mdf', + size=5MB, + maxsize=5MB, + filegrowth=10% +) +log on +( + name='SQL11_log', + filename='D:\SQL11_log.ldf', + size=5MB, + maxsize=5MB, + filegrowth=10% +) +go + +use SQL11 +go + +create table Stuinfo +( + StuID int primary key identity(1,1), + StuName nvarchar(10), + StuAge int, + StuSex int, + STime datetime default(cast('2007-01-07 01:11:36.590' as datetime)) +) + +insert into Stuinfo values ('Tom',19,1,null), +('Jack',20,0,null), +('Rose',21,1,null), +('Lulu',19,1,null), +('Lili',21,0,null), +('abc',20,1 ,default) + +select * from Stuinfo + +create table CourseInfo +( + CourseID int primary key identity(1,1), + CourseName nvarchar(10), + CourseMarks int +) + +insert into CourseInfo values('JavaBase',4), +('HTML',2), +('JavaScript',2), +('SqlBase',2) + +select * from CourseInfo + +create table ScoreInfo +( + ScoreID int primary key identity(1,1), + StuID int references Stuinfo(StuID), + CourseID int references CourseInfo(CourseID), + Score int +) + +insert into ScoreInfo 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 ScoreInfo + +select * from Stuinfo +select * from CourseInfo +select * from ScoreInfo + +--鏈夊鍥炬墍绀虹殑涓夊紶琛ㄧ粨鏋勶紝瀛︾敓淇℃伅琛紙stuInfo锛夛紝璇剧▼淇℃伅琛紙courseInfo锛,鍒嗘暟淇℃伅琛紙scoreInfo锛 + +--棰樼洰锛 +--1.鏌ヨ鍑烘瘡涓鐢 鎵閫変慨鐨勮绋嬬殑鏁伴噺 鍜屾墍閫変慨鐨勮绋嬬殑鑰冭瘯鐨勫钩鍧囧垎 +select st.StuID 瀛︾敓鍙,StuName 瀛︾敓濮撳悕,count(co.CourseID) 鎵閫夎绋嬫暟閲,AVG(Score) 骞冲潎鍒 from ScoreInfo sc +inner join CourseInfo co on sc.CourseID=co.CourseID +inner join Stuinfo st on st.StuID=sc.StuID group by st.StuID,StuName + +--2.鏌ヨ鍑烘瘡闂ㄨ绋 鐨勯変慨鐨勫鐢熺殑涓暟 鍜屽鐢熻冭瘯鐨勬诲垎 +select co.CourseID 璇剧▼鍙 ,CourseName 璇剧▼鍚嶇О,count(st.StuID) 瀛︾敓涓暟,sum(Score) 鑰冭瘯鎬诲垎 from ScoreInfo sc +inner join Stuinfo st on st.StuID=sc.StuID +inner join CourseInfo co on sc.CourseID=co.CourseID group by co.CourseID,CourseName + + +--3.鏌ヨ鍑烘у埆涓鏍峰苟涓斿勾榫勪竴鏍风殑瀛︾敓鐨勪俊鎭 +select * from Stuinfo A join Stuinfo B on A.StuSex=B.StuSex and A.StuAge=B.StuAge and A.StuName<>B.StuName + +--4.鏌ヨ鍑哄鍒嗕竴鏍风殑璇剧▼淇℃伅 +select distinct A.* from CourseInfo A join CourseInfo B on A.CourseMarks=B.CourseMarks and A.CourseName<>B.CourseName + +--5.鏌ヨ鍑哄弬鍔犱簡鑰冭瘯鐨勫鐢熺殑瀛﹀彿锛屽鍚嶏紝璇剧▼鍙峰拰鍒嗘暟 +select st.StuID 瀛︾敓鍙,StuName 瀛︾敓濮撳悕,sc.CourseID 璇剧▼鍙,Score 鍒嗘暟 from ScoreInfo sc +inner join Stuinfo st on st.StuID=sc.StuID +inner join courseInfo co on sc.courseID=co.courseID + +--6.鏌ヨ鍑哄弬鍔犱簡鑰冭瘯鐨勫鐢熺殑瀛﹀彿锛岃绋嬪彿锛岃绋嬪悕锛岃绋嬪鍒嗗拰鍒嗘暟 +select st.StuID 瀛︾敓鍙,sc.CourseID 璇剧▼鍙,CourseName 璇剧▼鍚嶅瓧,courseMarks 璇剧▼瀛﹀垎,Score 鍒嗘暟 from ScoreInfo sc +inner join Stuinfo st on st.StuID=sc.StuID +inner join courseInfo co on sc.courseID=co.courseID + +--7.鏌ヨ鍑烘病鏈夊弬鍔犺冭瘯鐨勫鐢熺殑瀛﹀彿鍜屽鍚 +select * from stuInfo left join scoreInfo on stuInfo.stuID = scoreInfo.stuID where score is null + + +--8.鏌ヨ鍑烘槸鍛ㄥ叚鍛ㄥぉ鏉ユ姤鍒扮殑瀛︾敓 + + +--9.鏌ヨ鍑哄鍚嶄腑鏈夊瓧姣峚鐨勫鐢熺殑淇℃伅 +select StuName from Stuinfo where StuName like '%a%' + +--10.鏌ヨ鍑洪変慨浜2闂ㄨ绋嬩互涓婄殑骞朵笖鑰冭瘯骞冲潎鍒嗗湪70浠ヤ笂鐨勫鐢熺殑瀛﹀彿鍜岃冭瘯骞冲潎鍒嗕互鍙婇変慨璇剧▼鐨勬暟閲 +select sc.stuID 瀛﹀彿,stuName 濮撳悕,avg(score)骞冲潎鍒,count(co.courseID)璇剧▼鏁 from scoreInfo sc +inner join stuInfo st on sc.stuID=st.stuID +inner join courseInfo co on sc.courseID=co.courseID group by sc.stuID,stuName having avg(score)>70 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\347\204\246\346\230\216\345\220\233/3.30.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\347\204\246\346\230\216\345\220\233/3.30.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4c8307650b3af4f01cc35c442d618c7e73a18755 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\347\204\246\346\230\216\345\220\233/3.30.sql" @@ -0,0 +1,86 @@ +use master +go +create database Student +on( + name = 'Student.mdf', + filename='D:\Student.mdf', + size=5, + maxsize=10, + filegrowth=15% +) +log on( + name = 'Student_log.ldf', + filename='D:\Student_log.ldf', + size=5, + maxsize=10, + filegrowth=15% +) +go +use Student +go +create table stuInfo +( + stuID int primary key identity(1,1), + stuName nvarchar(7) not null, + stuAge int not null, + stuSex nvarchar(1) not null, + time datetime +) +create table courseInfo +( + courseID int primary key identity(1,1), + courseName nvarchar(10) not null, + courseMarks nvarchar(5) +) +create table scoreInfo +( + scoreID int primary key identity(1,1), + stuID int references stuInfo(stuID), + courseID int references courseInfo(courseID), + score int not null +) +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','95') +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) +--题目: +select * from stuInfo +select * from courseInfo +select * from scoreInfo + +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuName 学生 ,count(courseInfo.courseID) 课程数量,AVG(score) 平均分 from stuInfo inner join scoreInfo +on stuInfo.stuID=scoreInfo.stuID inner join courseInfo on courseInfo.courseID=scoreInfo.courseID group by + stuName +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName 课程名称, COUNT(scoreInfo.courseID) 学生个数 ,sum(score) 总分 from stuInfo inner join scoreInfo +on stuInfo.stuID=scoreInfo.stuID inner join courseInfo on courseInfo.courseID=scoreInfo.courseID group by + scoreInfo.courseID,courseName +--3.查询出性别一样并且年龄一样的学生的信息 +select * from stuInfo a left join stuInfo b on a.stuSex=b.stuSex and a.stuAge=b.stuAge +--4.查询出学分一样的课程信息 +select * from courseInfo A inner join courseInfo B on A.courseMarks = B.courseMarks and A.courseID<>B.courseID +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuInfo.stuID 学号,stuInfo.stuName 姓名,courseInfo.courseID 课程号, scoreInfo.score 分数 from stuInfo inner join scoreInfo +on stuInfo.stuID=scoreInfo.stuID inner join courseInfo on courseInfo.courseID=scoreInfo.courseID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuInfo.stuID 学号,courseInfo.courseID 课程号,courseInfo.courseMarks 课程学分, scoreInfo.score 分数 from stuInfo inner join scoreInfo +on stuInfo.stuID=scoreInfo.stuID inner join courseInfo on courseInfo.courseID=scoreInfo.courseID +--7.查询出没有参加考试的学生的学号和姓名 +select stuInfo.stuID 学号,stuInfo.stuName 姓名 from stuInfo inner join scoreInfo +on stuInfo.stuID=scoreInfo.stuID inner join courseInfo on courseInfo.courseID=scoreInfo.courseID where +stuInfo.stuID is null and stuInfo.stuName is null +--8.查询出是周六周天来报到的学生 +select * from StuInfo where datename(weekday,time)='星期六' or datename(weekday,time)='星期日' +--9.查询出姓名中有字母a的学生的信息 +select * from StuInfo where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuInfo.stuID 学号,avg(scoreInfo.score) 考试平均分,COUNT(courseInfo.courseID) 选修课程的数量 from stuInfo inner join scoreInfo +on stuInfo.stuID=scoreInfo.stuID inner join courseInfo on courseInfo.courseID=scoreInfo.courseID +GROUP BY stuInfo.stuID HAVING COUNT(courseInfo.courseID)>=2 +AND avg(scoreInfo.score)>=70 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\345\206\240\346\235\260/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\345\206\240\346\235\260/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..24319cda223b6d145f792c01dc194ef6c5aa16ca --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\345\206\240\346\235\260/SQLQuery1.sql" @@ -0,0 +1,109 @@ +use master +go + +create database bbs +go + +use bbs +go + +create table stulnfo +( + stuID int primary key identity(1,1), + stuName varchar(5) not null, + stuAge int , + stuSex varchar(2) default(0) check(stuSex=0 or stuSex=1), + time date null +) +go + +create table course +( + courseID int primary key identity(1,1), + courseName varchar(10) not null, + courseMarks int +) +go + +create table score +( + scoreID int primary key identity(1,1), + stuID int references stulnfo(stuID), + courseID int references course(courseID), + score int +) + +insert into stulnfo(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') + +insert into course(courseName,courseMarks) values +('JavaBase',4), +('HTML',2), +('JavaScipt',2), +('SqlBase',2) + +insert into score(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 stulnfo +select * from course +select * from score + +select * from stulnfo s inner join score o on s.stuID=o.stuID +inner join course c on c.courseID=o.courseID +--题目: +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select s.stuID,stuName, count(courseName) 所选课程数量,avg(score) 平均分 from stulnfo s +inner join score o on s.stuID=o.stuID +inner join course c on c.courseID=o.courseID +group by s.stuID,stuName +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName,count(courseMarks) 选修学生个数,sum(score) 考试总分 from stulnfo s +inner join score o on s.stuID=o.stuID +inner join course c on c.courseID=o.courseID +group by courseName +--3.查询出性别一样并且年龄一样的学生的信息 +select * from stulnfo s1 join stulnfo s2 on s1.stuName<>s2.stuName and s1.stuSex=s2.stuSex and s1.stuAge=s2.stuAge + +--4.查询出学分一样的课程信息 + select * from course c1 join course c2 on c1.courseName<>c2.courseName and c1.courseMarks=c2.courseMarks + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select s.stuID,stuName,c.courseID,score from stulnfo s inner join score o on s.stuID=o.stuID +inner join course c on c.courseID=o.courseID + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select s.stuID,c.courseID,courseName,courseMarks,score from stulnfo s inner join score o on s.stuID=o.stuID +inner join course c on c.courseID=o.courseID + +--7.查询出没有参加考试的学生的学号和姓名 +select * from stulnfo s left join score o on s.stuID=o.stuID + +--8.查询出是周六周天来报到的学生 +select * from stulnfo where time>='2007-01-07' + +--9.查询出姓名中有字母a的学生的信息 +select * from stulnfo where stuName like '%a%' + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select s.stuID ,count(courseName) 选修课程数量,avg(score) 平均分 from stulnfo s inner join score o on s.stuID=o.stuID +inner join course c on c.courseID=o.courseID group by s.stuID having count(courseName)>=2 and avg(score)>=70 + + + + diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\350\277\234\346\226\271/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\350\277\234\346\226\271/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..74e2bac0c635f2b9e31db690854aac330737a79d --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\350\277\234\346\226\271/SQLQuery3.sql" @@ -0,0 +1,100 @@ +use master +go +create database bmp +on +( + name='bmp', + filename='D:\bmp.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='bmp_log', + filename='D:\bmp_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +use bmp +go +create table stuInfo +( + stuID int primary key identity(1,1) not null, + stuName char(10) not null, + stuAge int not null, + stuSex int not null, + time datetime +) +create table courseInfo +( + courseID int primary key identity(1,1), + courseName char(10) not null, + courseMarks int not null +) +create table scoreInfo +( + scoreID int primary key identity(1,1), + stuID int not null references stuInfo(stuID), + courseID int not null references courseInfo(courseID), + score int not null +) +select * from stuInfo +insert into stuInfo 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 values +('JavaBase',4), +('HTML',2), +('JavaScript',2), +('SqlBase',2) +select * from courseInfo +insert into scoreInfo 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 scoreInfo +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuName 学生,COUNT(courseName) 选修的课程的数量,AVG(score) 考试的平均分 from stuInfo SI,courseInfo CI, scoreInfo S +where SI.stuID = S.stuID and CI.courseID = S.courseID group by stuName +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName 各门课程,COUNT(stuName) 学生的个数,sum(score) 学生考试的总分 from stuInfo SI,courseInfo CI, scoreInfo S +where SI.stuID = S.stuID and CI.courseID = S.courseID group by courseName +--3.查询出性别一样并且年龄一样的学生的信息 +--方法一 + select * from stuInfo A join stuInfo B on A.stuName<>B.stuName and A.stuAge=B.stuAge and A.stusex=B.stusex + --方法二 + select * from stuInfo B where (select COUNT(*) from stuInfo where stuAge=B.stuAge and stuSex=B.stuSex)>1 +--4.查询出学分一样的课程信息 +select * from courseInfo A inner join courseInfo B on A.courseMarks = B.courseMarks and A.courseID<>B.courseID +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuInfo.stuID 学生学号,stuName 学生姓名,courseID 课程号,score 分数 +from stuInfo inner join scoreInfo on stuInfo.stuID = scoreInfo.stuID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuInfo.stuID 学生学号,stuName 学生姓名,scoreInfo.courseID 课程号,courseMarks 课程学分,score 分数 +from scoreInfo inner join stuInfo on scoreInfo.stuID = stuInfo.stuID inner join courseInfo +on scoreInfo.courseID = courseInfo.courseID +--7.查询出没有参加考试的学生的学号和姓名 +select stuInfo.stuID,stuName +from stuInfo left join scoreInfo +on stuInfo.stuID=scoreInfo.stuID group by stuInfo.stuID,stuName +--8.查询出是周六周天来报到的学生 +select * from StuInfo where datename(weekday,Time)='星期六' or datename(weekday,Time)='星期日' +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuInfo.stuID 学号,COUNT(courseID) 选修课程的数量, AVG(score) 考试平均分 +from stuInfo join scoreInfo on stuInfo.stuID = scoreInfo.stuID group by stuInfo.stuID having COUNT(courseID)>2 and AVG(score)>70 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\347\250\213\346\226\207\345\220\233/.keep" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\347\250\213\346\226\207\345\220\233/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\347\250\213\346\226\207\345\220\233/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\347\250\213\346\226\207\345\220\233/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0a39b3bdf63817ef88460e7fefd6dc7bfafe2df0 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\347\250\213\346\226\207\345\220\233/SQLQuery2.sql" @@ -0,0 +1,79 @@ +create database stu +go +use stu +go + +create table stuInfo +( + stuID int primary key identity(1,1), + stuName nvarchar(10) not null, + stuAge char(3) not null, + stusex char(2) not null, + time date +) +create table courseInfo +( + courseID int primary key identity(1,1), + courseName nvarchar(10) not null, + courseMarks int not null +) +create table scoreInfo +( + scoreID int primary key identity(1,1), + stuID int references stuInfo(stuID) not null, + courseID int references courseInfo(courseID) not null, + score int , +) +go + +insert into stuInfo 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 values +('JavaBase',4),('HTML',2), +('JavaScript',2),('SqlBase',2) + +insert into scoreInfo 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) +go +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) +select * from stuInfo +select * from courseInfo +select * from scoreInfo +--题目: +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select s.stuID,stuName, count(courseName) 课程数量 ,avg(score) 平均分数 from scoreInfo s +inner join stuInfo stu on s.stuID = stu.stuID +inner join courseInfo c on s.courseID=c.courseID group by s.stuID ,stuName +--2.查询出 每门课程的选修的学生的个数 和 学生考试的总分 +select c.courseID,courseName,count(s.stuID) 学生的个数,sum(score) 总分 from scoreInfo s +inner join stuInfo stu on s.stuID=stu.stuID +inner join courseInfo c on s.courseID=c.courseID group by c.courseID,courseName +--3.查询出性别一样并且年龄一样的学生的信息 +select * from stuInfo s1 join stuInfo s2 on s1.stuName<>s2.stuName and s1.stuAge=s2.stuAge and s1.stusex=s2.stusex +--另一种做法select * from stuInfo t where (select COUNT(*) from stuInfo where stuAge=t.stuAge and stuSex=t.stuSex)>1 +--4.查询出学分一样的课程信息 +select * from courseInfo c1 join courseInfo c2 on c1.courseID!=c2.courseID and c1.courseMarks=c2.courseMarks +--5.查询出参加了考试的 学生的学号,姓名,课程号 和 分数 +select s.stuID,stuName,c.courseID,score from scoreInfo s +inner join stuInfo stu on s.stuID=stu.stuID +inner join courseInfo c on s.courseID=c.courseID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select s.stuID,stuName,c.courseID,courseMarks,score from scoreInfo s +inner join stuInfo stu on s.stuID=stu.stuID +inner join courseInfo c on s.courseID=c.courseID +--7.查询出没有参加考试的学生的学号和姓名 + select * from stuInfo where stuID not in (select stuID from scoreInfo) + --另一种做法select * from stuInfo left join scoreInfo on stuInfo.stuID = scoreInfo.stuID where score is null +--8.查询出是周六周天来报到的学生 + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的 学生的学号 和 考试平均分 以及 选修课程的数量 +select s.stuID,stuName,avg(score)平均分,count(c.courseID)课程数 from scoreInfo s +inner join stuInfo stu on s.stuID=stu.stuID +inner join courseInfo c on s.courseID=c.courseID group by s.stuID,stuName having avg(score)>70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\347\273\203\346\226\207\346\265\251/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\347\273\203\346\226\207\346\265\251/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..440f9d46e146c7f9441f589ab22c4fcc0ede77d6 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\347\273\203\346\226\207\346\265\251/SQLQuery2.sql" @@ -0,0 +1,103 @@ +use master +go + +create database Student +on +( +name ='Student', +filename='D:\Student.mdf', +size=5MB, +maxsize=50MB, +filegrowth=50% +) + +log on +( +name='Student_log', +filename='D:\Student_log.ldf', +size=5MB, +maxsize=50MB, +filegrowth=50% +) +go + +use Student +go + +create table StuInfo +( + StuID int primary key identity(1,1), + StuName nvarchar(10), + StuAge int , + StuSex varchar(2) check (StuSex in (1,0) ), + Time datetime , + +) + +create table CourseInfo +( + CourseID int primary key identity(1,1), + CourseName nvarchar(20) , + 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) + +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 CourseInfo +Select * from ScoreInfo +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 + +Select T.StuID,T.StuName,count(CourseName) 选课数量, avg(Score) 平均分 from ScoreInfo S inner join StuInfo T on S.StuID=T.StuID +inner join CourseInfo C on C.CourseID=S.CourseID group by T.StuID,T.StuName + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +Select C.CourseName,C.CourseID,count(*) 选修的个数,sum(Score) 考试总分 from ScoreInfo S inner join CourseInfo C on C.CourseID=S.CourseID +group by C.CourseName,C.CourseID + +--3.查询出性别一样并且年龄一样的学生的信息 +Select S.StuID,S.StuName,S.StuAge,S.StuSex from StuInfo S inner join + StuInfo T on S.StuAge=T.StuAge and S.StuSex=T.StuSex and S.StuName!=T.StuName + +--4.查询出学分一样的课程信息 +Select * from CourseInfo C,CourseInfo R where C.CourseMarks=R.CourseMarks and C.CourseID<>R.CourseID +group by C.CourseName,C.CourseID,C.CourseMarks + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +Select S.StuID 学生学号 , StuName 姓名 , C.CourseID 课程号 , Score 分数 from ScoreInfo S inner join StuInfo I on S.StuID=I.StuID inner join +CourseInfo C on S.CourseID=C.CourseID group by S.StuID,StuName,C.CourseID,Score +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select S.StuID 学生学号 , C.CourseID 课程号 , CourseName 课程名, CourseMarks 课程学分 , Score 分数 from ScoreInfo S inner join StuInfo I on S.StuID=I.StuID inner join +CourseInfo C on S.CourseID=C.CourseID group by S.StuID,C.CourseID,CourseName,CourseMarks,Score +--7.查询出没有参加考试的学生的学号和姓名 +select S.StuID 学号 , StuName 姓名 from ScoreInfo I right join StuInfo S on I.StuID=S.StuID where Score is null or Score='' +--8.查询出是周六周天来报到的学生 +select * from StuInfo where datename(weekday,StuTime)='星期六' or datename(weekday,StuTime)='星期日' +--9.查询出姓名中有字母a的学生的信息 +select * from StuInfo where StuName like '%a%' or StuName like '%A%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select S.StuID 学号 , COUNT(*) 课程数量 , AVG(Score) 平均分 from ScoreInfo I inner join +StuInfo S on I.StuID=S.StuID group by S.StuID having COUNT(*)>2 and AVG(Score)>70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\347\277\201\346\231\266\350\276\211/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\347\277\201\346\231\266\350\276\211/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f77e575838e0a68f643b345162607e8f0f881632 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\347\277\201\346\231\266\350\276\211/SQLQuery1.sql" @@ -0,0 +1,110 @@ +use master +go + +create database bbs +go + +use bbs +go + +create table stulnfo +( + stuID int primary key identity(1,1), + stuName varchar(5) not null, + stuAge int , + stuSex varchar(2) default(0) check(stuSex=0 or stuSex=1), + time date null +) +go + +create table course +( + courseID int primary key identity(1,1), + courseName varchar(10) not null, + courseMarks int +) +go + +create table score +( + scoreID int primary key identity(1,1), + stuID int references stulnfo(stuID), + courseID int references course(courseID), + score int +) + +insert into stulnfo(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') + +insert into course(courseName,courseMarks) values +('JavaBase',4), +('HTML',2), +('JavaScipt',2), +('SqlBase',2) + +insert into score(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 stulnfo +select * from course +select * from score + +select * from stulnfo s inner join score o on s.stuID=o.stuID +inner join course c on c.courseID=o.courseID +--题目: +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select s.stuID,stuName, count(courseName) 所选课程数量,avg(score) 平均分 from stulnfo s +inner join score o on s.stuID=o.stuID +inner join course c on c.courseID=o.courseID +group by s.stuID,stuName +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName,count(courseMarks) 选修学生个数,sum(score) 考试总分 from stulnfo s +inner join score o on s.stuID=o.stuID +inner join course c on c.courseID=o.courseID +group by courseName +--3.查询出性别一样并且年龄一样的学生的信息 +select s1.* from stulnfo s1 join stulnfo s2 on s1.stuID<>s2.stuID and s1.stuSex=s2.stuSex and s1.stuAge=s2.stuAge + +--4.查询出学分一样的课程信息 + select distinct c1.* from course c1 join course c2 on c1.courseName<>c2.courseName and c1.courseMarks=c2.courseMarks + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select s.stuID,stuName,c.courseID,score from stulnfo s inner join score o on s.stuID=o.stuID +inner join course c on c.courseID=o.courseID + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select s.stuID,c.courseID,courseName,courseMarks,score from stulnfo s inner join score o on s.stuID=o.stuID +inner join course c on c.courseID=o.courseID + +--7.查询出没有参加考试的学生的学号和姓名 +select * from stulnfo s left join score o on s.stuID=o.stuID + +--8.查询出是周六周天来报到的学生 + +select * from stulnfo where datename(dw,time)='星期日' or datename(dw,time)=7 + +--9.查询出姓名中有字母a的学生的信息 +select * from stulnfo where stuName like '%a%' + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select s.stuID ,count(courseName) 选修课程数量,avg(score) 平均分 from stulnfo s inner join score o on s.stuID=o.stuID +inner join course c on c.courseID=o.courseID group by s.stuID having count(courseName)>=2 and avg(score)>=70 + + + + diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\202\226\346\265\267\346\231\237/.keep" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\202\226\346\265\267\346\231\237/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\202\226\346\265\267\346\231\237/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\202\226\346\265\267\346\231\237/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8138dd178ef91365836671302bd6ef3121ae6f55 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\202\226\346\265\267\346\231\237/SQLQuery1.sql" @@ -0,0 +1,101 @@ +use master +go +create database school +on +( + name = 'school', + filename = 'C:\sql\school.mdf', + size = 5MB , + maxsize = 500MB , + filegrowth = 5MB +) +log on +( + name = 'school_log', + filename = 'C:\sql\school_log.ldf', + size = 5MB , + maxsize = 500MB , + filegrowth = 5MB +) +go +use school +go +create table StuInfo +( + StuID int primary key identity (1,1) not null, + StuName nvarchar(20) not null, + StuAge int not null, + StuSex char(1) default(1) check(StuSex='男' or StuSex='女'), + Time datetime +) + +create table courseInfo +( + courseID int primary key identity (1,1) not null, + courseName nvarchar(20) not null, + courseMarks int not null +) + +create table scoreInfo +( + scoreID int primary key identity (1,1) not null, + StuID int references StuInfo(StuID), + courseID int references courseInfo(courseID), + score int check(score>=0 and score<=100) +) + +go +truncate table StuInfo +alter table StuInfo drop constraint CK__StuInfo__StuSex__117F9D94 +alter table scoreInfo drop constraint FK__scoreInfo__StuID__164452B1 +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 courseInfo +select * from scoreInfo + +--1.查询出每个学生 所选修的课程的数量 和 所选修的课程的考试的平均分 +select S.StuID , count(*) 所选课程的数量 , AVG(score) 所选修课程的考试平均分 from StuInfo S inner join scoreInfo C on S.StuID=C.StuID inner join courseInfo U on U.courseID = C.courseID group by S.StuID +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName , count(*) , sum(score) from StuInfo S inner join scoreInfo C on S.StuID=C.StuID inner join courseInfo U on U.courseID = C.courseID group by courseName +--3.查询出性别一样并且年龄一样的学生的信息 +select * from StuInfo S,StuInfo I where S.StuAge=I.StuAge AND S.StuSex=I.StuSex and S.StuID<>I.StuID +--4.查询出学分一样的课程信息fo B on A.StuSex=B.StuAge +select * from CourseInfo C,CourseInfo I where C.CourseMarks=I.CourseMarks and C.CourseID<>I.CourseID +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select S.StuID 学号 , StuName 姓名 , U.courseID 课程号 ,score 分数 from StuInfo S inner join scoreInfo C on S.StuID=C.StuID inner join courseInfo U on U.courseID = C.courseID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select S.StuID 学号 , U.courseID 课程号 , courseName 课程名 , courseMarks 课程学分 , score 分数 from StuInfo S inner join scoreInfo C on S.StuID=C.StuID inner join courseInfo U on U.courseID = C.courseID +--7.查询出没有参加考试的学生的学号和姓名 +select S.StuID 学号 , StuName 姓名 from ScoreInfo I right join StuInfo S on I.StuID=S.StuID where Score is null or Score='' +--8.查询出是周六周天来报到的学 +select * from StuInfo where datename(weekday,Time)='星期六' or datename(weekday,Time)='星期日' +--9.查询出姓名中有字母a的学生的信息 +select * from StuInfo where StuName like '%a%' or StuName like '%A%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select S.StuID 学号 , COUNT(*) 课程数量 , AVG(Score) 平均分 from ScoreInfo I inner join StuInfo S on I.StuID=S.StuID group by S.StuID having COUNT(*)>2 and AVG(Score)>70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\202\226\346\265\267\346\231\237/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\202\226\346\265\267\346\231\237/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5d30e49c6a96fef7f5365e5048ba26de9cabbbd6 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\202\226\346\265\267\346\231\237/SQLQuery2.sql" @@ -0,0 +1,125 @@ +use master +go +create database internetbar +on +( + name = 'internetbar', + filename='D:\sql\internetbar.mdf', + size = 5MB, + maxsize = 500MB, + filegrowth = 5MB +) +log on +( + name = 'internetbar_log', + filename='D:\sql\internetbar_log.ldf', + size = 5MB, + maxsize = 500MB, + filegrowth = 5MB +) +go +use internetbar +go + +create table tbl_card +( + cardID varchar(20) primary key not null, + passWord nvarchar(20) not null, + balance nvarchar(20) not null, + userName nvarchar(20) not null, +) +--卡号 +--密码 +--卡上余额 +--用户名 + +create table tbl_computer +( + computerID varchar(20) primary key not null, + onUse char(1) default(0) check(onUse=1 or onUse=0) not null, + note nvarchar(50) not null +) +--机器编号 +--机器使用情况 +--说明 + +create table tbl_record +( + recordID varchar(20) primary key not null, + cardID varchar(20) references tbl_card(cardID), + computerID varchar(20) references tbl_computer(computerID), + beginTime datetime, + endTime datetime, + fee int +) +--记录编号 +--卡号 +--机器编号 +--开始时间 +--结束时间 +--费用 + +insert into tbl_card values +('0023_ABC','555',98,'张军'), +('0025_bbd','abe',300,'朱俊'), +('0036_CCD','何柳',100,'何柳'), +('0045_YGR','0045_YGR',58,'证验'), +('0078_RJV','5585fg',600,'校庆'), +('0089_EDE','zhang',134,'张俊') + +insert into tbl_computer values +('02',0,25555), +('03',1,55555), +('04',0,66666), +('05',1,88888), +('06',0,688878), +('B01',0,558558) + +insert into tbl_record values +('23','0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00',20), +('34','0025_bbd','02','2006-12-25 16: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-23 15:26:00','2006-12-23 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',3), +('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 C.cardID 卡号, userName 用户名, computerID 机器编号, beginTime 开始时间, endTime 结束时间,fee 消费金额 from +tbl_record R inner join tbl_card C on R.cardID=C.cardID +where userName='张军' order by fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 +select computerID 机器编号, count(*) 机器上网次数, sum(fee) 总金额 from tbl_record group by computerID +--3. 查询出所有已经使用过的 上网卡的 消费总金额 +select sum(fee) 消费总金额 from tbl_record +--4. 查询出从未消费过的上网卡的卡号和用户名 +select C.cardID 卡号, userName 用户名 from +tbl_card C left join tbl_record R on C.cardID=R.cardID where fee is null or fee=' ' +--5. 将密码与用户名一样的上网卡信息查询出来 +select distinct A. * from +tbl_card A , tbl_card B where A.passWord=B.userName +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 computerID 机器编号, count(*) 使用次数 from +tbl_record group by computerID order by count(*) desc +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select R.cardID 卡号, userName 用户名, computerID 机器编号, fee 消费金额 from +tbl_record R inner join tbl_card C on R.cardID=C.cardID where R.cardID like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select R.cardID 卡号, userName 用户名, computerID 机器编号,beginTime 开始时间,endTime 结束时间, fee 消费金额 from +tbl_record R inner join tbl_card C on R.cardID=C.cardID +where datepart(DW,beginTime)=7 or DATENAME(DW,beginTime)='星期日' +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select R.cardID 卡号, userName 用户名, computerID 机器编号,beginTime 开始时间,endTime 结束时间, fee 消费金额 from +tbl_record R inner join tbl_card C on R.cardID=C.cardID +where (endtime-beginTime)>12 +--10. 查询出消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 R.cardID 卡号, userName 用户名, computerID 机器编号,beginTime 开始时间,endTime 结束时间, fee 消费金额 from +tbl_record R inner join tbl_card C on R.cardID=C.cardID order by fee desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\346\264\245\345\215\216/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\346\264\245\345\215\216/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0553bd54176318855c53cac45a5b1554b1ca693c --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\346\264\245\345\215\216/SQLQuery1.sql" @@ -0,0 +1,71 @@ +use master +go +create database Students +on +( + name='Students', + filename='D:\SQLwork\Students.mdf', + Size=5MB, + maxsize=50MB, + filegrowth=10% + ) + log on +( +name='Students_log', + filename='D:\SQLwork\Students_log.ldf', + Size=5MB, + maxsize=50MB, + filegrowth=10% +) +go +use Students +create table stuInfo +( +stuID int primary key identity(1,1), +stuName nvarchar(10) , +stuAge int , +stuSex nvarchar(2) , +stuTime datetime , +) +create table courseInfo +( +courseID int primary key identity(1,1), +courseName nvarchar(10) not null, +courseMarks int not null, +) +create table scoreInfo +( +scoreID int identity(1,1), +stuID int references stuInfo(stuID), +courseID int references courseInfo(courseID), +score int not null, +) + +insert into stuInfo(stuName,stuAge,stuSex,stuTime) 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 courseInfo +select * from scoreInfo +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) +--题目: +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select F.stuID,stuName, count (courseMarks)课程的数量,avg (score)课程的考试的平均分 from stuInfo F,courseInfo C,scoreInfo O where F.stuID=O.stuID AND C.courseID=O.courseID group by stuName ,F.stuID +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select C.courseID, count (F.stuID)选修的学生的个数,SUM (score)课程的考试的总分 from stuInfo F,courseInfo C,scoreInfo O where F.stuID=O.stuID AND C.courseID=O.courseID group by C.courseID +--3.查询出性别一样并且年龄一样的学生的信息 +select A.stuID,A.stuName,A.stuSex,B.stuAge FROM stuInfo A inner join stuInfo B on A.stuID<>B.stuID and A.stuSex=B.stuSex and A.stuAge=B.stuAge +--4.查询出学分一样的课程信息 +select distinct B.courseID,B.courseName,B.courseMarks from courseInfo A inner join courseInfo B on A.courseID<>B.courseID and A.courseMarks=B.courseMarks +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select F.stuID 学号,stuName 姓名,C.courseID 课程号,score 分数 from stuInfo F,courseInfo C,scoreInfo O where F.stuID=O.stuID AND C.courseID=O.courseID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select F.stuID 学号,stuName 姓名,C.courseID 课程号,courseName 课程名,courseMarks 课程学分,score 分数 from stuInfo F,courseInfo C,scoreInfo O where F.stuID=O.stuID AND C.courseID=O.courseID +--7.查询出没有参加考试的学生的学号和姓名 +select S.StuID,StuName from Stuinfo S left join scoreInfo I on S.StuID = I.stuID where score is NULL +--8 .查询出是周六周天来报到的学生 +select * from stuInfo where stuTime='星期六' or stuTime='星期天' +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select S.stuID,avg(score),count(*) from scoreInfo S inner join stuInfo I on I.stuID = S.stuID inner join courseInfo C on C.courseID = S.courseID group by S.stuID having count(*)>2 and avg(score)>70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\213\217\346\231\272\351\276\231/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\213\217\346\231\272\351\276\231/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fc6a44963cfd403e998ad9d2666d428dab6e00f3 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\213\217\346\231\272\351\276\231/SQLQuery2.sql" @@ -0,0 +1,59 @@ +use master + +create database Srudents +go +create table stuinfo +( +stuID int primary key identity, +stuName varchar(10), +stuAge int , +StuSex int check(stuSex=1 or stuSex=0), +time date +) +create table courseInfo +( +courseID int primary key identity, +courseName varchar(10), +courseMarks int +) +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) +values +('Tom',19,1), +('Jack',20,0), +('Rose',21,1), +('Lulu',19,1), +('Lili',21,0), +('abc',20,1) +update stuinfo set time='2007-01-07 01:11:36.590'where stuID=6 +insert into +courseInfo(courseName,courseMarks) +values +('JavaBase',4), +('HTML',2), +('JavaScript',2), +('SqkBase',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 stuID,count(courseID) 所修课程数量,avg(score) 所修课程平均分 from scoreInfo group by stuID +select courseID 课程,count(stuID) 选修课程人数,sum(score) 总分 from scoreInfo group by courseID +select * from stuinfo a where EXISTS (select stuAge,StuSex,count(stuAge),count(StuSex) from stuinfo b where a.stuAge=b.stuAge and a.StuSex=b.StuSex group by stuAge,StuSex having count(stuAge)>1 and count(StuSex)>1) +select * from courseInfo where courseMarks in ( select courseMarks from courseInfo group by courseMarks having count(courseMarks)>1) +select scoreInfo.stuID,stuinfo.stuName, courseID,score from scoreInfo inner join stuinfo on stuinfo.stuID=scoreInfo.stuID +select scoreInfo.stuID,courseInfo.courseID,courseName,courseMarks,score from scoreInfo inner join courseInfo on courseInfo.courseID=scoreInfo.courseID +select * from stuinfo where stuName like '%a%' +select stuID, avg(score) 平均分,count(courseID) 课程数 from scoreInfo group by stuID having avg(score)>70 and count(courseID)>2 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\213\217\346\235\234\345\206\233/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\213\217\346\235\234\345\206\233/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6905f6c1ba83b7dc83743b28b1d4840535fd715f --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\213\217\346\235\234\345\206\233/SQLQuery1.sql" @@ -0,0 +1,113 @@ +use master +go + +create database Student +on +( +name='Student', + filename='D:\text\Student.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +log on +( +name='Students_log', + filename='D:\text\Student_log.mdf', + size=5MB, + maxsize=50MB, + filegrowth=10% +) +go + +use Student +go + +create table stuInfo +( +stuID int primary key identity, +stuName varchar(10), +stuAge varchar(5), +stuSex varchar(1), +[time] datetime +) +go + +create table courseInfo +( +courseID int primary key identity, +courseName varchar(10), +courseMarks int +) +go + +create table scoreInfo +( +scoreID int primary key identity, +stuID int references stuInfo(stuID), +courseID int references courseInfo(courseID), +score int +) +go + +insert into stuInfo 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') +go + +insert into courseInfo values('JavaBase',4),('HTML',2),('JavaScript',2),('SqlBase',2) +go + +insert into scoreInfo 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) +go + +select * from stuInfo +select * from courseInfo +select * from scoreInfo +go + +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuName 学生姓名,COUNT(scoreInfo.stuID) 所选修的课程的数量,AVG(score) 所选修的课程的考试的平均分 + from stuInfo left join scoreInfo on stuInfo.stuID = scoreInfo.stuID GROUP BY stuName + go + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName 课程名称,COUNT(scoreInfo.courseID) 学生个数,SUM(score) 学生考试的总分 from courseInfo inner join scoreInfo on courseInfo.courseID = scoreInfo.courseID GROUP BY scoreInfo.courseID,courseName + go + + --3.查询出性别一样并且年龄一样的学生的信息 + --方法一 + select * from stuInfo t where (select COUNT(*) from stuInfo where stuAge=t.stuAge and stuSex=t.stuSex)>1 + --方法二 + select * from stuInfo s1 join stuInfo s2 on s1.stuName<>s2.stuName and s1.stuAge=s2.stuAge and s1.stusex=s2.stusex + go + +--4.查询出学分一样的课程信息 + select * from courseInfo c where (select COUNT(*) from courseInfo where courseMarks=c.courseMarks)>1 + go + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuInfo.stuID 学号,stuName 姓名,courseID 课程号,score 分数 from stuInfo inner join scoreInfo on stuInfo.stuID = scoreInfo.stuID +go + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuInfo.stuID 学号,stuName 姓名,scoreInfo.courseID 课程号,courseMarks 课程学分,score 分数 from scoreInfo inner join stuInfo on scoreInfo.stuID = stuInfo.stuID inner join courseInfo on scoreInfo.courseID = courseInfo.courseID +go + +--7.查询出没有参加考试的学生的学号和姓名 +--方法一 +select stuInfo.stuID 学号,stuName 姓名 from stuInfo left join scoreInfo on stuInfo.stuID = scoreInfo.stuID where score is null +--方法二 +select * from stuInfo where stuID not in (select stuID from scoreInfo) +go + +--8.查询出是周六周天来报到的学生 +select * from StuInfo where datename(weekday,[time])='星期六' or datename(weekday,[time])='星期日' +go + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +go + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的 学号 和 考试平均分以及 选修课程的数量 +select stuInfo.stuID 学号,AVG(score) 考试平均分,COUNT(courseID) 选修课程的数量 from stuInfo join scoreInfo on stuInfo.stuID = scoreInfo.stuID group by stuInfo.stuID having AVG(score)>70 and COUNT(courseID)>2 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\350\245\277\346\266\233/.keep" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\350\245\277\346\266\233/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\350\245\277\346\266\233/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\350\245\277\346\266\233/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0f213b55947764e60459fa7216074719d098ed53 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\350\245\277\346\266\233/SQLQuery1.sql" @@ -0,0 +1,86 @@ +use master + +create database LAN +on +( + name='LAN', + filename='D:\LAN.mdf', + size=5, + maxsize=15, + filegrowth=15 +) +log on +( + name='LAN_log', + filename='D:\LAN_log.ldf', + size=5, + maxsize=15, + filegrowth=15 +) +go +use LAN +go +create table stuinfo +( + stuid int primary key, + stuname nvarchar(10) not null, + stuage nvarchar(10) not null, + stusex nvarchar(1) check(stusex='0'or stusex='1'), + stutime date +) +create table courseinfo +( + courseid int primary key not null, + coursename nvarchar(10) not null, + coursemarks nvarchar(10) not null, +) +create table scoreinfo +( + scoreid int primary key identity(1,1) not null, + stuid int references stuinfo(stuid), + courseid int references courseinfo(courseid), + score int check(score>=0 and score<=100) +) +insert into stuinfo values (1,'Tom',19,1,''),(2,'Jack',20,0,''), + (3,'Rose',21,1,''),(4,'Lulu',19,1,''),(5,'Lili',21,0,''),(6,'abc',20,1,'2007-01-07') +insert into courseinfo values (1,'JavaBase',4),(2,'HTML',2), + (3,'JavaScript',2),(4,'SqlBase',2) +insert into scoreinfo 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 courseinfo +select *from scoreinfo +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 + select S.StuID , count(*) 所选课程的数量 , AVG(score) 所选修课程的考试平均分 from StuInfo S inner join scoreInfo C on S.StuID=C.StuID inner join + courseInfo U on U.courseID = C.courseID group by S.StuID +--2.查询出每门课程的选修的学生的个数和学生考试的总分 + select courseName , count(*) , sum(score) from StuInfo S inner join scoreInfo C on S.StuID=C.StuID inner join courseInfo U on U.courseID = C.courseID group by courseName +--3.查询出性别一样并且年龄一样的学生的信息 + select * from StuInfo S,StuInfo I where S.StuAge=I.StuAge AND S.StuSex=I.StuSex and S.StuID<>I.StuID +--4.查询出学分一样的课程信息 + select * from CourseInfo C,CourseInfo I where C.CourseMarks=I.CourseMarks and C.CourseID<>I.CourseID +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 + select S.StuID 学号 , StuName 姓名 , U.courseID 课程号 ,score 分数 from StuInfo S inner join scoreInfo C on S.StuID=C.StuID inner join courseInfo U on U.courseID = C.courseID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 + select S.StuID 学号 , U.courseID 课程号 , courseName 课程名 , courseMarks 课程学分 , score 分数 from StuInfo S inner join + scoreInfo C on S.StuID=C.StuID inner join courseInfo U on U.courseID = C.courseID +--7.查询出没有参加考试的学生的学号和姓名 + select S.StuID 学号 , StuName 姓名 from ScoreInfo I right join StuInfo S on I.StuID=S.StuID where Score is null or Score='' +--8.查询出是周六周天来报到的学生 + select * from StuInfo where datename(weekday,stutime)='星期六' or datename(weekday,stutime)='星期日' +--9.查询出姓名中有字母a的学生的信息 + select * from StuInfo where StuName like '%a%' or StuName like '%A%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和 +--考试平均分以及选修课程的数量 + select S.StuID 学号 , COUNT(*) 课程数量 , AVG(Score) 平均分 from ScoreInfo I inner join StuInfo S on I.StuID=S.StuID group by + S.StuID having COUNT(*)>2 and AVG(Score)>70 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\226\233\351\207\221\345\201\245/\347\254\254\345\215\201\344\270\200\344\275\234\344\270\232.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\226\233\351\207\221\345\201\245/\347\254\254\345\215\201\344\270\200\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7472e6b2dd2ce510b14c659a486c391461758f64 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\226\233\351\207\221\345\201\245/\347\254\254\345\215\201\344\270\200\344\275\234\344\270\232.sql" @@ -0,0 +1,85 @@ +use master +go +create database students +on +( +name='students', +filename='D:\students.mdf', +size=10mb, +maxsize=50mb, +filegrowth=10% +) +log +on +( +name='students_log', +filename='D:\students_log.ldf', +size=10mb, +maxsize=50mb, +filegrowth=10% +) +go + +use students +go + +create table studentinfo +( +stuid int primary key identity, +stuname nvarchar(10) not null, +stuage int not null , +stusex int check(stusex=1 or stusex=0), +time datetime, +) +create table courseinfo +( +courseid int primary key identity , +coursename nvarchar(20), +coursemarks int , +) +create table scoreinfo +( +scoreid int identity , +stuid int references studentinfo(stuid), +courseid int references courseinfo(courseid), +score int +) +insert into studentinfo(stuname,stuage,stusex) values +('Tom',19,1),('Jack',20,0),('Rose',21,1), +('Lulu',19,1),('Lili',21,0),('abc',20,1) + +update studentinfo set time='2007-01-07 01:11:36.590'where stuid=6 + +insert into courseinfo 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 studentinfo +select * from courseinfo +select * from scoreinfo + +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) + +--题目: +--1.查询出 每个学生所选修的 课程的数量和 所选修的课程的考试的平均分 +select ST.stuname,count(*) 课程的数量,avg(score) 平均分 from scoreinfo S inner join studentinfo ST on S.stuid=ST.stuid group by ST.stuname +--2.查询出 每门课程的选修的 学生的个数和 学生考试的总分 +select C.coursename,count(*) 学生的个数,sum(score)学生考试的总分 from scoreinfo S inner join courseinfo C on S.courseid=C.courseid group by C.coursename +--3.查询出性别一样并且年龄一样的学生的信息 +select A.* from studentinfo A inner join studentinfo B on(A.stuID <> B.stuID) and (A.stuSex = B.stuSex) and (A.stuAge = B.stuAge) +--4.查询出学分一样的课程信息 +select distinct A.* from courseinfo A inner join courseinfo B on(A.courseid<>B.courseid) and (A.coursemarks=B.coursemarks) +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select st.stuID 学号, st.stuName 姓名, s.courseID 课程号, s.score 分数 from scoreinfo S inner join studentinfo ST on S.stuid=ST.stuid +--6.查询出参加了 考试的学生 的学号,课程号,课程名,课程学分和分数 +select s.stuID 学号, s.courseID 课程号, c.courseName 课程名, C.coursemarks 课程学分, s.score 分数 from courseinfo C inner join scoreinfo S on C.courseid=S.courseid +--7.查询出没有参加考试的学生的学号和姓名 +select S.stuID, S.stuName from studentinfo S left join scoreinfo SC on S.stuid=SC.stuid where SC.scoreid is null +--8.查询出是周六周天来报到的学生 +select * from studentinfo where DATENAME(dw, time) = '星期六' or DATENAME(dw, time) = '星期日' +--9.查询出姓名中有字母a的学生的信息 +select * from studentinfo where stuname like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuID 学号,avg(score) 平均成绩, count(*) 选课数量 from scoreinfo group by stuid having count(*) > 2 and round(avg(cast(score as float)), 1) > 70 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\242\201\345\235\232/.keep" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\242\201\345\235\232/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\242\201\345\235\232/SQLQueryq1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\242\201\345\235\232/SQLQueryq1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..52c54118eaf40e6b55a93c95711648ada1c3236e --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\242\201\345\235\232/SQLQueryq1.sql" @@ -0,0 +1,75 @@ +use master +go +create database stuinfo +on +( + name='stuinfo', + filename='D:\sql\stuinfo.mbf', + size=5mb, + maxsize=100mb, + filegrowth=5mb +) +log on +( + name='stuinfo_log', + filename='D:\sql\stuinfo_log.lbf', + size=5mb, + maxsize=100mb, + filegrowth=5mb +) +go +use stuinfo +go +create table stu +( + stuID int primary key identity(1,1), + stuName char(12) not null, + stuAge int not null, + stuSex char(1) check(stuSex='1' or stuSex='0'), + time char(23) +) +create table course +( + courseID int primary key identity(1,1), + courseName char(20) not null, + courseMarks int not null +) +create table score +( + scoreID int primary key identity(1,1), + stuID int references stu(stuID), + courseID int references course(courseID), + score int not null +) +insert into stu 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 course values +('JavaBase',4),('HTML',2),('JavaScript',2),('SqlBase',2) +insert into score values +(1,1,80),(1,2,85),(1,4,50),(2,1,75),(3,1,45),(2,4,75) +,(3,1,45),(4,1,95),(4,2,75),(4,3,90),(4,4,45) +select*from stu +select*from course +select*from score +--1.查询出 每个学生 所选修的课程的 数量和 所选修的课程的考试的 平均分 +select S.StuID 学生ID , COUNT(*) 数量 , AVG(Score) 平均分 from StuInfo S inner join ScoreInfo I on S.StuID=I.StuID group by S.StuID +--2.查询出 每门课程 的选修的学生的 个数 和学生考试的 总分 +select C.CourseID 课程ID , COUNT(*) 个数 , SUM(Score) 总分 from CourseInfo C inner join ScoreInfo I on C.CourseID=I.CourseID group by C.CourseID +--3.查询出 性别一样 并且 年龄一样 的学生的信息 +select * from StuInfo S,StuInfo I where S.StuAge=I.StuAge AND S.StuSex=I.StuSex and S.StuID<>I.StuID +--4.查询出 学分一样 的 课程信息 +select * from CourseInfo C,CourseInfo I where C.CourseMarks=I.CourseMarks and C.CourseID<>I.CourseID +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select S.StuID 学生学号 , StuName 姓名 , C.CourseID 课程号 , Score 分数 from ScoreInfo S inner join StuInfo I on S.StuID=I.StuID inner join CourseInfo C on S.CourseID=C.CourseID group by S.StuID,StuName,C.CourseID,Score +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select S.StuID 学生学号 , C.CourseID 课程号 , CourseName 课程名, CourseMarks 课程学分 , Score 分数 from ScoreInfo S inner join StuInfo I on S.StuID=I.StuID inner join CourseInfo C on S.CourseID=C.CourseID group by S.StuID,C.CourseID,CourseName,CourseMarks,Score +--7.查询出没有参加考试的学生的学号和姓名 +select S.StuID 学号 , StuName 姓名 from ScoreInfo I right join StuInfo S on I.StuID=S.StuID where Score is null or Score='' +--8.查询出是周六周天来报到的学生 +select * from StuInfo where datename(weekday,StuTime)='星期六' or datename(weekday,StuTime)='星期日' +--9.查询出姓名中有字母a的学生的信息 +select * from StuInfo where StuName like '%a%' or StuName like '%A%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select S.StuID 学号 , COUNT(*) 课程数量 , AVG(Score) 平均分 from ScoreInfo I inner join StuInfo S on I.StuID=S.StuID group by S.StuID having COUNT(*)>2 and AVG(Score)>70 + diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\246\203\347\245\232\345\233\275/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\246\203\347\245\232\345\233\275/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..4ae12c2c02810281b4af0084da5095cdc61bd4a3 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\246\203\347\245\232\345\233\275/SQLQuery1.sql" @@ -0,0 +1,84 @@ +use master +go +create database class +on +( +name='class', +filename='D:\class.mdf', +size=5, +maxsize=50, +filegrowth=10% +) +log on +( +name='class_log', +filename='D:\class_log.ldf', +size=5, +maxsize=50, +filegrowth=10% +) +go + +use class +go +create table stuInfo--学生信息表 +( +stuid int primary key identity ,--学生编号 +stuname varchar(20) not null,--学生姓名 +stuage int not null,--学生年龄 +stusex char(2) not null,--学生性别 +time datetime--时间 +) +create table courseInfo--课程信息表 +( +courseid int primary key identity,--课程编号 +coursename varchar(20) not null,--课程名称 +coursemarks int not null--课程学分 +) +create table scoreInfo--分数信息表 +( +scoreid int primary key identity,--分数编号 +stuid int references stuInfo(stuid),--学生课程 +courseid int references courseInfo(courseid),--课程编号 +score int not null--课程分数 +) +go +insert into stuInfo(stuname,stuage,stusex) values +('Tom','19','1'), +('Jack','20','0'), +('Rose','21','1'), +('Lulu','19','1'), +('Lili','21','0'), +('abc','20','1') +update stuInfo set time='2007-01-07 01:11:36.590' where stuid=6 +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 courseInfo +select * from scoreInfo +--1.查询出 每个学生 所选修的课程的数量 和所选修的课程的考试的平均分 +select stuname,COUNT(*),avg(score) from stuInfo A inner join scoreInfo B on A.stuid=B.stuid group by stuname +--2.查询出 每门课程的选修的学生的个数和学生考试的总分 +select coursename,COUNT(*),sum(score) from scoreInfo A inner join courseInfo B on A.courseid=B.courseid group by coursename +--3.查询出性别一样并且年龄一样的学生的信息 +select * from stuInfo A inner join stuInfo B on (A.stuid<>B.stuid)and(A.stusex=B.stusex)and(A.stuage=B.stuage) +--4.查询出学分一样的课程信息 +select * from courseInfo A inner join courseInfo B on (A.courseid<>B.courseid)and(A.coursemarks=B.coursemarks) +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select a.stuid,stuname,courseid,score from stuInfo a inner join scoreInfo b on a.stuid=b.stuid +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuid,a.courseid,coursename,coursemarks,score from scoreInfo a inner join courseInfo b on a.courseid=b.courseid +--7.查询出没有参加考试的学生的学号和姓名 +select a.stuid,stuname from stuInfo a left join scoreInfo b on a.stuid=b.stuid where b.scoreid is null +--8.查询出是周六周天来报到的学生 +select * from stuInfo where datename(DW,time)='星期六' or datename(DW,time)='星期天' +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuname like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuid,avg(score),count(*) from scoreInfo group by stuid having count(*)>2 and round(avg(cast(score as float)),1)>70 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\260\255\350\277\233/.keep" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\260\255\350\277\233/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\260\255\350\277\233/3.30\344\275\234\344\270\232.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\260\255\350\277\233/3.30\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..aac0742fcd63d9c36f6b1482cda5ca3205db4647 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\260\255\350\277\233/3.30\344\275\234\344\270\232.sql" @@ -0,0 +1,75 @@ +use master +go +create database student +on +( + name='student', + filename='E:\sql\student.mdf', + size=10MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name='student_log', + filename='E:\sql\student_log.ldf', + size=10MB, + maxsize=50MB, + filegrowth=10% +) +go +use student +go +create table stuInfo +( + stuID int primary key, + stuName varchar(20), + stuAge varchar(2), + stuSex varchar(1) check(stuSex=1 or stuSex=0), + time varchar(50) +) +create table courseInfo +( + courseID int primary key, + courseName varchar(20), + courseMarks char(1) +) +create table scoreInfo +( + scoreID int primary key, + stuID int references stuInfo(stuID), + courseID int references courseInfo(courseID), + score int +) +insert into stuInfo (stuID,stuName,stuAge,stuSex,time) +values(1,'Tom',19,1,null),(2,'Jack',20,0,null),(3,'Rose',21,1,null),(4,'Lulu',19,1,null),(5,'Lili',21,0,null),(6,'abc',20,1,'2007-01-07 01:11:36.590') + +insert into courseInfo(courseID,courseName,courseMarks) +values (1,'JavaBase',4),(2,'HTML',2),(3,'JavaScri',2),(4,'SqlBase',2) + +insert into scoreInfo(scoreID,stuID,courseID,score) +values (1,1,1,80),(2,1,2,85),(3,1,4,50),(4,2,1,75),(5,2,3,45),(6,2,4,75),(7,3,1,45),(8,4,2,75),(9,4,2,75),(10,4,3,90),(11,4,4,45) + +select * from stuInfo +select * from courseInfo +select * from scoreInfo +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuName,count(*)课程数量,round (AVG(cast(score as float)),1)平均分 from scoreInfo join stuInfo on stuInfo.stuID = stuInfo.stuID group by stuName +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName count(*),sum(score) from courseInfo join scoreInfo on courseInfo.courseID = scoreInfo.courseID group by courseName +--3.查询出性别一样并且年龄一样的学生的信息 +select * from stuInfo s1 join stuInfo s2 on s1.stuID = s2.stuID and s1.stuSex= s2.stuSex and s1.stuAge= s2.stuAge +--4.查询出学分一样的课程信息 +select * from courseInfo c1 join courseInfo c2 on c1.courseMarks=c2.courseMarks +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuInfo.stuID,stuName,courseID,score from stuInfo join scoreInfo on stuInfo.stuID=stuInfo.stuID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuInfo.stuID,stuName,score from stuInfo join scoreInfo on stuInfo.stuID=stuInfo.stuID join courseInfo on courseInfo.courseID=courseInfo.courseID +--7.查询出没有参加考试的学生的学号和姓名 +select stuInfo.stuID,stuName from stuInfo join scoreInfo on stuInfo.stuID not in (stuInfo.stuID) +--8.查询出是周六周天来报到的学生 +select stuInfo.stuName from stuInfo where datepart (weekday,getdate())>5 +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like ('%a%') +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuID,count(courseID)选修课程数量,AVG(score)平均分 from scoreInfo group by stuID having COUNT(courseID) >2 and AVG(score) >70 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\265\265\345\256\266\351\275\220/.keep" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\265\265\345\256\266\351\275\220/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\265\265\345\256\266\351\275\220/3.30\344\275\234\344\270\232.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\265\265\345\256\266\351\275\220/3.30\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..aac0742fcd63d9c36f6b1482cda5ca3205db4647 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\265\265\345\256\266\351\275\220/3.30\344\275\234\344\270\232.sql" @@ -0,0 +1,75 @@ +use master +go +create database student +on +( + name='student', + filename='E:\sql\student.mdf', + size=10MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name='student_log', + filename='E:\sql\student_log.ldf', + size=10MB, + maxsize=50MB, + filegrowth=10% +) +go +use student +go +create table stuInfo +( + stuID int primary key, + stuName varchar(20), + stuAge varchar(2), + stuSex varchar(1) check(stuSex=1 or stuSex=0), + time varchar(50) +) +create table courseInfo +( + courseID int primary key, + courseName varchar(20), + courseMarks char(1) +) +create table scoreInfo +( + scoreID int primary key, + stuID int references stuInfo(stuID), + courseID int references courseInfo(courseID), + score int +) +insert into stuInfo (stuID,stuName,stuAge,stuSex,time) +values(1,'Tom',19,1,null),(2,'Jack',20,0,null),(3,'Rose',21,1,null),(4,'Lulu',19,1,null),(5,'Lili',21,0,null),(6,'abc',20,1,'2007-01-07 01:11:36.590') + +insert into courseInfo(courseID,courseName,courseMarks) +values (1,'JavaBase',4),(2,'HTML',2),(3,'JavaScri',2),(4,'SqlBase',2) + +insert into scoreInfo(scoreID,stuID,courseID,score) +values (1,1,1,80),(2,1,2,85),(3,1,4,50),(4,2,1,75),(5,2,3,45),(6,2,4,75),(7,3,1,45),(8,4,2,75),(9,4,2,75),(10,4,3,90),(11,4,4,45) + +select * from stuInfo +select * from courseInfo +select * from scoreInfo +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select stuName,count(*)课程数量,round (AVG(cast(score as float)),1)平均分 from scoreInfo join stuInfo on stuInfo.stuID = stuInfo.stuID group by stuName +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseName count(*),sum(score) from courseInfo join scoreInfo on courseInfo.courseID = scoreInfo.courseID group by courseName +--3.查询出性别一样并且年龄一样的学生的信息 +select * from stuInfo s1 join stuInfo s2 on s1.stuID = s2.stuID and s1.stuSex= s2.stuSex and s1.stuAge= s2.stuAge +--4.查询出学分一样的课程信息 +select * from courseInfo c1 join courseInfo c2 on c1.courseMarks=c2.courseMarks +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuInfo.stuID,stuName,courseID,score from stuInfo join scoreInfo on stuInfo.stuID=stuInfo.stuID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuInfo.stuID,stuName,score from stuInfo join scoreInfo on stuInfo.stuID=stuInfo.stuID join courseInfo on courseInfo.courseID=courseInfo.courseID +--7.查询出没有参加考试的学生的学号和姓名 +select stuInfo.stuID,stuName from stuInfo join scoreInfo on stuInfo.stuID not in (stuInfo.stuID) +--8.查询出是周六周天来报到的学生 +select stuInfo.stuName from stuInfo where datepart (weekday,getdate())>5 +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like ('%a%') +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuID,count(courseID)选修课程数量,AVG(score)平均分 from scoreInfo group by stuID having COUNT(courseID) >2 and AVG(score) >70 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\277\252\345\212\233\344\272\232\345\260\224/1.txt" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\277\252\345\212\233\344\272\232\345\260\224/1.txt" new file mode 100644 index 0000000000000000000000000000000000000000..2b307b2048c3782d29d9621f33890e9019554d52 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\350\277\252\345\212\233\344\272\232\345\260\224/1.txt" @@ -0,0 +1,73 @@ +create database aji +go +use aji +go + +create table stuInfo +( + stuID int primary key identity(1,1), + stuName nvarchar(10) not null, + stuAge char(3) not null, + stusex char(2) not null, + time date +) +create table courseInfo +( + courseID int primary key identity(1,1), + courseName nvarchar(10) not null, + courseMarks int not null +) +create table scoreInfo +( + scoreID int primary key identity(1,1), + stuID int references stuInfo(stuID) not null, + courseID int references courseInfo(courseID) not null, + score int , +) +go + +insert into stuInfo 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 values('JavaBase',4),('HTML',2),('JavaScript',2),('SqlBase',2) + +insert into scoreInfo 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) +go +--鏈夊鍥炬墍绀虹殑涓夊紶琛ㄧ粨鏋勶紝瀛︾敓淇℃伅琛紙stuInfo锛夛紝璇剧▼淇℃伅琛紙courseInfo锛,鍒嗘暟淇℃伅琛紙scoreInfo锛 +select * from stuInfo +select * from courseInfo +select * from scoreInfo +--棰樼洰锛 +--1.鏌ヨ鍑烘瘡涓鐢熸墍閫変慨鐨勮绋嬬殑鏁伴噺鍜屾墍閫変慨鐨勮绋嬬殑鑰冭瘯鐨勫钩鍧囧垎 +select s.stuID,stuName, count(courseName) 璇剧▼鏁伴噺 ,avg(score) 骞冲潎鍒嗘暟 from scoreInfo s +inner join stuInfo stu on s.stuID = stu.stuID +inner join courseInfo c on s.courseID=c.courseID group by s.stuID ,stuName +--2.鏌ヨ鍑 姣忛棬璇剧▼鐨勯変慨鐨勫鐢熺殑涓暟 鍜 瀛︾敓鑰冭瘯鐨勬诲垎 +select c.courseID,courseName,count(s.stuID) 瀛︾敓鐨勪釜鏁,sum(score) 鎬诲垎 from scoreInfo s +inner join stuInfo stu on s.stuID=stu.stuID +inner join courseInfo c on s.courseID=c.courseID group by c.courseID,courseName +--3.鏌ヨ鍑烘у埆涓鏍峰苟涓斿勾榫勪竴鏍风殑瀛︾敓鐨勪俊鎭 +select * from stuInfo s1 join stuInfo s2 on s1.stuName<>s2.stuName and s1.stuAge=s2.stuAge and s1.stusex=s2.stusex +--鍙︿竴绉嶅仛娉晄elect * from stuInfo t where (select COUNT(*) from stuInfo where stuAge=t.stuAge and stuSex=t.stuSex)>1 +--4.鏌ヨ鍑哄鍒嗕竴鏍风殑璇剧▼淇℃伅 +select * from courseInfo c1 join courseInfo c2 on c1.courseID!=c2.courseID and c1.courseMarks=c2.courseMarks +--5.鏌ヨ鍑哄弬鍔犱簡鑰冭瘯鐨 瀛︾敓鐨勫鍙凤紝濮撳悕锛岃绋嬪彿 鍜 鍒嗘暟 +select s.stuID,stuName,c.courseID,score from scoreInfo s +inner join stuInfo stu on s.stuID=stu.stuID +inner join courseInfo c on s.courseID=c.courseID +--6.鏌ヨ鍑哄弬鍔犱簡鑰冭瘯鐨勫鐢熺殑瀛﹀彿锛岃绋嬪彿锛岃绋嬪悕锛岃绋嬪鍒嗗拰鍒嗘暟 +select s.stuID,stuName,c.courseID,courseMarks,score from scoreInfo s +inner join stuInfo stu on s.stuID=stu.stuID +inner join courseInfo c on s.courseID=c.courseID +--7.鏌ヨ鍑烘病鏈夊弬鍔犺冭瘯鐨勫鐢熺殑瀛﹀彿鍜屽鍚 + select * from stuInfo where stuID not in (select stuID from scoreInfo) + --鍙︿竴绉嶅仛娉晄elect * from stuInfo left join scoreInfo on stuInfo.stuID = scoreInfo.stuID where score is null +--8.鏌ヨ鍑烘槸鍛ㄥ叚鍛ㄥぉ鏉ユ姤鍒扮殑瀛︾敓 + +--9.鏌ヨ鍑哄鍚嶄腑鏈夊瓧姣峚鐨勫鐢熺殑淇℃伅 +select * from stuInfo where stuName like '%a%' +--10.鏌ヨ鍑洪変慨浜2闂ㄨ绋嬩互涓婄殑骞朵笖鑰冭瘯骞冲潎鍒嗗湪70浠ヤ笂鐨 瀛︾敓鐨勫鍙 鍜 鑰冭瘯骞冲潎鍒 浠ュ強 閫変慨璇剧▼鐨勬暟閲 +select s.stuID,stuName,avg(score)骞冲潎鍒,count(c.courseID)璇剧▼鏁 from scoreInfo s +inner join stuInfo stu on s.stuID=stu.stuID +inner join courseInfo c on s.courseID=c.courseID group by s.stuID,stuName having avg(score)>70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\202\261\346\236\227\346\236\253/\344\275\234\344\270\232\344\270\200.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\202\261\346\236\227\346\236\253/\344\275\234\344\270\232\344\270\200.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e17b16524208b65a54507a8663c19e2474947ac3 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\202\261\346\236\227\346\236\253/\344\275\234\344\270\232\344\270\200.sql" @@ -0,0 +1,91 @@ +use master +go + +create database student +go + +use student +go + +create table stuInfo +( + stuID int primary key identity(1,1) not null, + stuName char(4)unique not null, + stuAge char(2)not null, + stuSex char(1) default(1) check(stuSex='1' or stuSex='0'), + time datetime +) + +create table courseInfo +( + courseID int primary key identity(1,1) not null, + courseName char(10) not null, + courseMarks char(1) not null +) + +create table scoreInfo +( + scoreID int primary key identity(1,1) not null, + stuID int references stuInfo(stuID), + courseID int references courseInfo(courseID), + score int not null +) + +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 学生,count(courseInfo.courseID) 课程数量,avg(score) 考试平均分 from scoreInfo +right join courseInfo on scoreInfo.courseID = courseInfo.courseID +right join stuInfo on scoreInfo.stuID = stuInfo.stuID group by stuInfo.stuID +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select courseInfo.courseID 课程,count(stuID) 学生数,sum(score) 考试总分 from scoreInfo +inner join courseInfo on scoreInfo .courseID =courseInfo.courseID group by courseInfo.courseID +--3.查询出性别一样并且年龄一样的学生的信息 +select * from stuInfo m where (select count(*) from stuInfo where stuAge=m.stuAge and stuSex=m.stuSex)>1 +--4.查询出学分一样的课程信息 +select * from courseInfo m where (select count(*) from courseInfo where courseMarks=m.courseMarks)>1 +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select stuInfo.stuID 学号,stuName 姓名,courseInfo.courseID 课程号,score 分数 from scoreInfo +inner join courseInfo on scoreInfo.courseID = courseInfo.courseID +inner join stuInfo on scoreInfo.stuID = stuInfo.stuID group by stuInfo.stuID,courseInfo.courseID,scoreInfo.score,stuInfo.stuName +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select stuInfo.stuID 学号,courseInfo.courseID 课程号,courseInfo.courseID 课程名,courseInfo.courseMarks 课程学分,score 分数 from scoreInfo +inner join courseInfo on scoreInfo.courseID = courseInfo.courseID +inner join stuInfo on scoreInfo.stuID = stuInfo.stuID group by stuInfo.stuID,courseInfo.courseID,scoreInfo.score,courseInfo.courseID,courseInfo.courseMarks +--7.查询出没有参加考试的学生的学号和姓名 +select stuInfo.stuID 学号,stuName 姓名 from scoreInfo +right join stuInfo on scoreInfo.stuID = stuInfo.stuID +where score is null or score='' +--8.查询出是周六周天来报到的学生 +select * from StuInfo where datename(weekday,Time)='星期六' or datename(weekday,Time)='星期日' +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select s.StuID 学号 , COUNT(*) 课程数量,AVG(Score) 平均分 from ScoreInfo I inner join StuInfo S on I.StuID=S.StuID +group by S.StuID having COUNT(*)>2 and AVG(Score)>70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\346\214\257\347\201\277/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\346\214\257\347\201\277/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..65057ede61b774f7bbce3186ce32d25c65485f68 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\346\214\257\347\201\277/SQLQuery1.sql" @@ -0,0 +1,126 @@ +use master +go +create database CCA +on +( +name='CCA', +filename='D:\SQL\CCA.mdf', +size=5MB, +maxsize = 50MB, +filegrowth=10% +) +log on +( +name='CCA_log', +filename='D:\SQL\CCA_log.ldf', +size=5MB, +maxsize=50MB, +filegrowth=10% +) +go +use CCA +go +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) +create table stuInfo +( +stuID int primary key, +stuName nvarchar(10) not null, +stuAge nvarchar(10) not null, +stuSex nvarchar(20) check(stuSex='1' or stuSex='0') not null, +time datetime +) +insert into stuInfo values +('1','Tom',19,1,Null), +('2','Jack',20,0,Null), +('3','Rose',21,1,Null), +('4','Lulu',19,1,Null), +('5','Lili',21,0,Null), +('6','abc',20,1,'2007-01-07 01:11:36.590') +select * from stuInfo + +create table courseInfo +( +courseID int primary key, +courseName nvarchar(10) not null, +courseMarks int not null +) +insert into courseInfo values +('1','JavaBase',4), +('2','HTML',2), +('3','JavaScript',2), +('4','SqlBase',2) +select * from courseInfo + + +create table scoreInfo +( +scoreID int primary key, +stuID int references stuInfo(stuID), +courseID int references courseInfo(courseID), +score int not null +) +insert into scoreInfo values +(1,1,1,80), +(2,1,2,85), +(3,1,4,50), +(4,2,1,75), +(5,2,3,45), +(6,2,4,75), +(7,3,1,45), +(8,4,1,95), +(9,4,2,75), +(10,4,3,90), +(11,4,4,45) +select * from scoreInfo +--题目: +select * from stuInfo +select * from courseInfo +select * from scoreInfo +--1.查询出 每个学生 所选修的课程的数量 和 所选修的课程的考试的平均分 + +select I.stuID,I.stuName,COUNT(*) 所选修的课程的数量,AVG(score) 所选修的课程的考试的平均分 from scoreInfo S + inner join stuInfo I on I.stuID=S.stuID + group by I.stuID,I.stuName + +--2.查询出 每门课程 的选修的学生 的个数 和 学生考试的总分 + +select C.courseID,C.courseName,COUNT(*)'学生的个数',SUM(score)'学生考试的总分' from courseInfo C +inner join scoreInfo S on C. courseID = S.courseID +group by C.courseID,C.courseName + +--3.查询出 性别一样 并且 年龄一样 的 学生的信息 + +select * from stuInfo S join stuInfo I on S.stuAge=I.stuAge AND S.stuSex = I.stuSex AND S.stuID != I.stuID + +--4.查询出 学分一样 的 课程信息 + +select * from courseInfo C join courseInfo I on C.courseMarks = I.courseMarks AND C.courseID <> I.courseID + +--5.查询出 参加了考试的学生的学号,姓名,课程号 和 分数 + +select S.stuID 学号,stuName 姓名,C.courseID 课程号,score 分数 from scoreInfo S join courseInfo C on S.courseID = C.courseID join stuInfo I on S.stuID = I.stuID +group by S.stuID,stuName,C.courseID,score + +--6.查询出 参加了考试的 学生的学号,课程号,课程名,课程学分和分数 + +select S.stuID 学生的学号,C.courseID 课程号,C.courseName 课程名,C.courseMarks 课程学分,S.score 分数 from scoreInfo S join courseInfo C on S.courseID = C.courseID + +--7.查询出没有参加考试的学生的学号和姓名 + +select I.stuID 学号,I.stuName 姓名 from stuInfo I left join scoreInfo S on I.stuID = S.stuID where S.score is null + +--8.查询出是周六周天来报到的学生 + +select * from stuInfo where DATENAME(WEEKDAY,time) = '星期六' or DATENAME(WEEKDAY,time) = '星期日' + +--9.查询出姓名中有字母a的学生的信息 + +select * from stuInfo where stuName like '%a%' + +--10.查询出 选修了2门课程以上 的并且 考试平均分在70以上 的 学生的学号 和 考试平均分 以及 选修课程的数量 + +select S.stuID 学号, COUNT(*) 选修课程的数量 ,AVG(score) 平均分 from scoreInfo S join stuInfo I on S.stuID = I.stuID + group by S.stuID +having COUNT(*) > 2 and AVG(score) > 70 + + diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\230\256\345\273\272\351\234\226/.keep" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\230\256\345\273\272\351\234\226/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\230\256\345\273\272\351\234\226/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\230\256\345\273\272\351\234\226/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1878febd51e2e86309196345cb05b09a52bb7208 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\230\256\345\273\272\351\234\226/SQLQuery2.sql" @@ -0,0 +1,103 @@ +use master +go + +create database Student +on +( +name ='Student', +filename='D:\Student.mdf', +size=5MB, +maxsize=50MB, +filegrowth=50% +) + +log on +( +name='Student_log', +filename='D:\Student_log.ldf', +size=5MB, +maxsize=50MB, +filegrowth=50% +) +go + +use Student +go + +create table StuInfo +( + StuID int primary key identity(1,1), + StuName nvarchar(10), + StuAge int , + StuSex varchar(2) check (StuSex in (1,0) ), + Time datetime , + +) + +create table CourseInfo +( + CourseID int primary key identity(1,1), + CourseName nvarchar(20) , + 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) + +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 CourseInfo +Select * from ScoreInfo +--1.鏌ヨ鍑烘瘡涓鐢熸墍閫変慨鐨勮绋嬬殑鏁伴噺鍜屾墍閫変慨鐨勮绋嬬殑鑰冭瘯鐨勫钩鍧囧垎 + +Select T.StuID,T.StuName,count(CourseName) 閫夎鏁伴噺, avg(Score) 骞冲潎鍒 from ScoreInfo S inner join StuInfo T on S.StuID=T.StuID +inner join CourseInfo C on C.CourseID=S.CourseID group by T.StuID,T.StuName + +--2.鏌ヨ鍑烘瘡闂ㄨ绋嬬殑閫変慨鐨勫鐢熺殑涓暟鍜屽鐢熻冭瘯鐨勬诲垎 +Select C.CourseName,C.CourseID,count(*) 閫変慨鐨勪釜鏁,sum(Score) 鑰冭瘯鎬诲垎 from ScoreInfo S inner join CourseInfo C on C.CourseID=S.CourseID +group by C.CourseName,C.CourseID + +--3.鏌ヨ鍑烘у埆涓鏍峰苟涓斿勾榫勪竴鏍风殑瀛︾敓鐨勪俊鎭 +Select S.StuID,S.StuName,S.StuAge,S.StuSex from StuInfo S inner join + StuInfo T on S.StuAge=T.StuAge and S.StuSex=T.StuSex and S.StuName!=T.StuName + +--4.鏌ヨ鍑哄鍒嗕竴鏍风殑璇剧▼淇℃伅 +Select * from CourseInfo C,CourseInfo R where C.CourseMarks=R.CourseMarks and C.CourseID<>R.CourseID +group by C.CourseName,C.CourseID,C.CourseMarks + +--5.鏌ヨ鍑哄弬鍔犱簡鑰冭瘯鐨勫鐢熺殑瀛﹀彿锛屽鍚嶏紝璇剧▼鍙峰拰鍒嗘暟 +Select S.StuID 瀛︾敓瀛﹀彿 , StuName 濮撳悕 , C.CourseID 璇剧▼鍙 , Score 鍒嗘暟 from ScoreInfo S inner join StuInfo I on S.StuID=I.StuID inner join +CourseInfo C on S.CourseID=C.CourseID group by S.StuID,StuName,C.CourseID,Score +--6.鏌ヨ鍑哄弬鍔犱簡鑰冭瘯鐨勫鐢熺殑瀛﹀彿锛岃绋嬪彿锛岃绋嬪悕锛岃绋嬪鍒嗗拰鍒嗘暟 +select S.StuID 瀛︾敓瀛﹀彿 , C.CourseID 璇剧▼鍙 , CourseName 璇剧▼鍚, CourseMarks 璇剧▼瀛﹀垎 , Score 鍒嗘暟 from ScoreInfo S inner join StuInfo I on S.StuID=I.StuID inner join +CourseInfo C on S.CourseID=C.CourseID group by S.StuID,C.CourseID,CourseName,CourseMarks,Score +--7.鏌ヨ鍑烘病鏈夊弬鍔犺冭瘯鐨勫鐢熺殑瀛﹀彿鍜屽鍚 +select S.StuID 瀛﹀彿 , StuName 濮撳悕 from ScoreInfo I right join StuInfo S on I.StuID=S.StuID where Score is null or Score='' +--8.鏌ヨ鍑烘槸鍛ㄥ叚鍛ㄥぉ鏉ユ姤鍒扮殑瀛︾敓 +select * from StuInfo where datename(weekday,StuTime)='鏄熸湡鍏' or datename(weekday,StuTime)='鏄熸湡鏃' +--9.鏌ヨ鍑哄鍚嶄腑鏈夊瓧姣峚鐨勫鐢熺殑淇℃伅 +select * from StuInfo where StuName like '%a%' or StuName like '%A%' +--10.鏌ヨ鍑洪変慨浜2闂ㄨ绋嬩互涓婄殑骞朵笖鑰冭瘯骞冲潎鍒嗗湪70浠ヤ笂鐨勫鐢熺殑瀛﹀彿鍜岃冭瘯骞冲潎鍒嗕互鍙婇変慨璇剧▼鐨勬暟閲 +select S.StuID 瀛﹀彿 , COUNT(*) 璇剧▼鏁伴噺 , AVG(Score) 骞冲潎鍒 from ScoreInfo I inner join +StuInfo S on I.StuID=S.StuID group by S.StuID having COUNT(*)>2 and AVG(Score)>70 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\256\207\347\277\224/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\256\207\347\277\224/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ea2f252f302eb6d0dfadeedc7eea822c03b4656a --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\256\207\347\277\224/SQLQuery1.sql" @@ -0,0 +1,74 @@ + +create database aji +go +use aji +go + +create table stuInfo +( + stuID int primary key identity(1,1), + stuName nvarchar(10) not null, + stuAge char(3) not null, + stusex char(2) not null, + time date +) +create table courseInfo +( + courseID int primary key identity(1,1), + courseName nvarchar(10) not null, + courseMarks int not null +) +create table scoreInfo +( + scoreID int primary key identity(1,1), + stuID int references stuInfo(stuID) not null, + courseID int references courseInfo(courseID) not null, + score int , +) +go + +insert into stuInfo 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 values('JavaBase',4),('HTML',2),('JavaScript',2),('SqlBase',2) + +insert into scoreInfo 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) +go +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) +select * from stuInfo +select * from courseInfo +select * from scoreInfo +--题目: +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select s.stuID,stuName, count(courseName) 课程数量 ,avg(score) 平均分数 from scoreInfo s +inner join stuInfo stu on s.stuID = stu.stuID +inner join courseInfo c on s.courseID=c.courseID group by s.stuID ,stuName +--2.查询出 每门课程的选修的学生的个数 和 学生考试的总分 +select c.courseID,courseName,count(s.stuID) 学生的个数,sum(score) 总分 from scoreInfo s +inner join stuInfo stu on s.stuID=stu.stuID +inner join courseInfo c on s.courseID=c.courseID group by c.courseID,courseName +--3.查询出性别一样并且年龄一样的学生的信息 +select * from stuInfo s1 join stuInfo s2 on s1.stuName<>s2.stuName and s1.stuAge=s2.stuAge and s1.stusex=s2.stusex +--另一种做法select * from stuInfo t where (select COUNT(*) from stuInfo where stuAge=t.stuAge and stuSex=t.stuSex)>1 +--4.查询出学分一样的课程信息 +select * from courseInfo c1 join courseInfo c2 on c1.courseID!=c2.courseID and c1.courseMarks=c2.courseMarks +--5.查询出参加了考试的 学生的学号,姓名,课程号 和 分数 +select s.stuID,stuName,c.courseID,score from scoreInfo s +inner join stuInfo stu on s.stuID=stu.stuID +inner join courseInfo c on s.courseID=c.courseID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select s.stuID,stuName,c.courseID,courseMarks,score from scoreInfo s +inner join stuInfo stu on s.stuID=stu.stuID +inner join courseInfo c on s.courseID=c.courseID +--7.查询出没有参加考试的学生的学号和姓名 + select * from stuInfo where stuID not in (select stuID from scoreInfo) + --另一种做法select * from stuInfo left join scoreInfo on stuInfo.stuID = scoreInfo.stuID where score is null +--8.查询出是周六周天来报到的学生 + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的 学生的学号 和 考试平均分 以及 选修课程的数量 +select s.stuID,stuName,avg(score)平均分,count(c.courseID)课程数 from scoreInfo s +inner join stuInfo stu on s.stuID=stu.stuID +inner join courseInfo c on s.courseID=c.courseID group by s.stuID,stuName having avg(score)>70 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\200\235\351\224\220/.keep" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\200\235\351\224\220/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\200\235\351\224\220/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\200\235\351\224\220/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..887f88f7581b3aa322e44ac8ddb4c665fd2adfad --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\200\235\351\224\220/SQLQuery1.sql" @@ -0,0 +1,178 @@ +use master +go + +if exists(select * from sys.databases where name='student') + drop database student +go + +create database student +go + +use student +go + +create table student_info +( + stuID int primary key identity, + stuName nvarchar(20) not null, + stuAge int, + stuSex int check(stuSex in (0,1)), + time datetime +) +go + +create table course_info +( + courseID int primary key identity, + courseName varchar(20) not null, + couseMarks int +) +go + +create table score_info +( + scoreID int primary key identity, + stuID int references student_info(stuID), + courseID int references course_info(courseID), + score int check(score between 0 and 150) +) +go + +--dbcc checkident('student_info', reseed, 1) +insert into student_info 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') + +--dbcc checkident('course_info', reseed, 1) +insert into course_info values +('JavaBase', 4), +('HTML', 2), +('Javascript', 2), +('SqlBase', 2) +go + +insert into score_info 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) +go + +select * from score_info + +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select + st.stuName ,count(*) '课程数量', round(avg(cast(score as float)), 1) '平均分' +from + score_info s +join + student_info st +on + s.stuID = st.stuID +group by + st.stuName + + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select + c.courseName, count(*) '选修的学生个数', sum(score) '总分' +from + course_info c +join + score_info s +on + c.courseID = s.courseID +group by + c.courseName + + +--3.查询出性别一样并且年龄一样的学生的信息 +select + s1.* +from + student_info s1 +join + student_info s2 +on + (s1.stuID <> s2.stuID) and (s1.stuSex = s2.stuSex) and (s1.stuAge = s2.stuAge) + +--4.查询出学分一样的课程信息 +select + distinct c1.* +from + course_info c1 +join + course_info c2 +on + (c1.courseID <> c2.courseID) and (c1.couseMarks = c2.couseMarks) + + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select + st.stuID, st.stuName, s.courseID, s.score +from + score_info s +join + student_info st +on + s.stuID = st.stuID + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select + s.stuID, s.courseID, c.courseName, c.couseMarks, s.score +from + score_info s +join + course_info c +on + c.courseID = s.courseID + +--7.查询出没有参加考试的学生的学号和姓名 +select + st.stuID, st.stuName +from + student_info st +left join + score_info sc +on + sc.stuID = st.stuID +where + sc.scoreID is null + +--8.查询出是周六周天来报到的学生 +select + * +from + student_info +where + DATENAME(dw, time) = '星期六' or DATENAME(dw, time) = '星期日' + +--9.查询出姓名中有字母a的学生的信息 +select + * +from + student_info +where + stuName like '%a%' + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select + stuID '学号', round(avg(cast(score as float)), 1) '平均成绩', count(*) '选课数量' +from + score_info +group by + stuID +having + count(*) > 2 and round(avg(cast(score as float)), 1) > 70 + diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\200\235\351\224\220/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\200\235\351\224\220/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e592302bccfea469950cb4c73d8e10054b96e493 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\200\235\351\224\220/SQLQuery2.sql" @@ -0,0 +1,211 @@ +use master +go + +if exists(select * from sys.databases where name = 'cybercafe_charge_sys') + drop database cybercafe_charge_sys +go + +create database cybercafe_charge_sys +go + +use cybercafe_charge_sys +go + +create table tbl_card +( + id varchar(10) primary key, + [passWord] nvarchar(20) check(len(passWord) between 2 and 20), + balance decimal(10,1), + userName nvarchar(20) +) +go + +create table tbl_computer +( + id varchar(10) primary key, + onUse int check(onUse in (0, 1)), + note text +) +go + +create table tbl_record +( + id varchar(10) primary key, + cardId varchar(10) references tbl_card(id), + ComputerId varchar(10) references tbl_computer(id), + beginTime smalldatetime, + endTime smalldatetime, + fee decimal(10,1) +) +go + +insert into tbl_card 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, '张峻') +go + +insert into tbl_computer values +('02', 0, '25555'), +('03', 1, '55555'), +('04', 0, '66666'), +('05', 1, '88888'), +('06', 0, '688878'), +('B01', 0, '558558') +go + +insert into tbl_record values +('23', '0078_RJV', 'B01', '2007-07-15 19:00:00', '2007-07-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', 30), +('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) +go + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select + tc.id '卡号', tc.userName '用户名', tr.ComputerId '机器编号', tr.beginTime '开始时间', tr.endTime '结束时间', tr.fee '消费金额' +from + tbl_card tc +join + tbl_record tr +on + tc.id = tr.cardId +where + tc.userName = '张军' +order by + tr.fee desc + +--2. 查询出每台机器上的上网次数和消费的总金额 +select + ComputerId, count(*) '上网次数', sum(fee) '消费总金额' +from + tbl_record +group by + ComputerId + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select + cardId, sum(fee) '消费总金额' +from + tbl_record +group by + cardId + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select + tc.id, tc.userName +from + tbl_card tc +left join + tbl_record tr +on + tr.cardId = tc.id +where + tr.id is null + +--5. 将密码与用户名一样的上网卡信息查询出来 +select + * +from + tbl_card t1 +join + tbl_card t2 +on + (t1.id = t2.id) and (t1.[passWord] = t2.userName) + + +--6. 查询出使用次数最多的机器号和使用次数 +-- 1 +select + ComputerId, count(*) '使用次数' +from + tbl_record +group by + ComputerId +having + count(*) = (select max(t.使用次数) from (select ComputerId, count(*) '使用次数' from tbl_record group by ComputerId) t) + +--2 +--create view v_count +--as +--select +-- ComputerId, count(*) '使用次数' +--from +-- tbl_record +--group by +-- ComputerId +--go + +--select +-- * +--from +-- v_count v1 +--where +-- v1.使用次数 = (select max(使用次数) from v_count) + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select + tc.id, tc.userName, tr.ComputerId, tr.fee +from + tbl_record tr +join + tbl_card tc +on + tr.cardId = tc.id +where + tr.cardId like '%ABC' + + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select + tc.id, tc.userName, tr.ComputerId, tr.beginTime, tr.endTime, tr.fee +from + tbl_record tr +join + tbl_card tc +on + tr.cardId = tc.id +where + DATENAME(dw, tr.beginTime) in ('星期六','星期日') + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select + tc.id, tc.userName, tr.ComputerId, tr.beginTime, tr.endTime, tr.fee +from + tbl_record tr +join + tbl_card tc +on + tr.cardId = tc.id +where + DATEDIFF(HH, tr.beginTime, tr.endTime) > 12 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select + tc.id, tc.userName, tr.ComputerId, tr.beginTime, tr.endTime, tr.fee +from + tbl_record tr +join + tbl_card tc +on + tr.cardId = tc.id +where + tr.fee in ( + select + distinct top 3 fee + from + tbl_record + order by + fee desc + ) +order by + tr.fee desc diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\277\233\345\220\211/.keep" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\277\233\345\220\211/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\277\233\345\220\211/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\277\233\345\220\211/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ea2f252f302eb6d0dfadeedc7eea822c03b4656a --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\277\233\345\220\211/SQLQuery1.sql" @@ -0,0 +1,74 @@ + +create database aji +go +use aji +go + +create table stuInfo +( + stuID int primary key identity(1,1), + stuName nvarchar(10) not null, + stuAge char(3) not null, + stusex char(2) not null, + time date +) +create table courseInfo +( + courseID int primary key identity(1,1), + courseName nvarchar(10) not null, + courseMarks int not null +) +create table scoreInfo +( + scoreID int primary key identity(1,1), + stuID int references stuInfo(stuID) not null, + courseID int references courseInfo(courseID) not null, + score int , +) +go + +insert into stuInfo 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 values('JavaBase',4),('HTML',2),('JavaScript',2),('SqlBase',2) + +insert into scoreInfo 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) +go +--有如图所示的三张表结构,学生信息表(stuInfo),课程信息表(courseInfo),分数信息表(scoreInfo) +select * from stuInfo +select * from courseInfo +select * from scoreInfo +--题目: +--1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 +select s.stuID,stuName, count(courseName) 课程数量 ,avg(score) 平均分数 from scoreInfo s +inner join stuInfo stu on s.stuID = stu.stuID +inner join courseInfo c on s.courseID=c.courseID group by s.stuID ,stuName +--2.查询出 每门课程的选修的学生的个数 和 学生考试的总分 +select c.courseID,courseName,count(s.stuID) 学生的个数,sum(score) 总分 from scoreInfo s +inner join stuInfo stu on s.stuID=stu.stuID +inner join courseInfo c on s.courseID=c.courseID group by c.courseID,courseName +--3.查询出性别一样并且年龄一样的学生的信息 +select * from stuInfo s1 join stuInfo s2 on s1.stuName<>s2.stuName and s1.stuAge=s2.stuAge and s1.stusex=s2.stusex +--另一种做法select * from stuInfo t where (select COUNT(*) from stuInfo where stuAge=t.stuAge and stuSex=t.stuSex)>1 +--4.查询出学分一样的课程信息 +select * from courseInfo c1 join courseInfo c2 on c1.courseID!=c2.courseID and c1.courseMarks=c2.courseMarks +--5.查询出参加了考试的 学生的学号,姓名,课程号 和 分数 +select s.stuID,stuName,c.courseID,score from scoreInfo s +inner join stuInfo stu on s.stuID=stu.stuID +inner join courseInfo c on s.courseID=c.courseID +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select s.stuID,stuName,c.courseID,courseMarks,score from scoreInfo s +inner join stuInfo stu on s.stuID=stu.stuID +inner join courseInfo c on s.courseID=c.courseID +--7.查询出没有参加考试的学生的学号和姓名 + select * from stuInfo where stuID not in (select stuID from scoreInfo) + --另一种做法select * from stuInfo left join scoreInfo on stuInfo.stuID = scoreInfo.stuID where score is null +--8.查询出是周六周天来报到的学生 + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' +--10.查询出选修了2门课程以上的并且考试平均分在70以上的 学生的学号 和 考试平均分 以及 选修课程的数量 +select s.stuID,stuName,avg(score)平均分,count(c.courseID)课程数 from scoreInfo s +inner join stuInfo stu on s.stuID=stu.stuID +inner join courseInfo c on s.courseID=c.courseID group by s.stuID,stuName having avg(score)>70 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\251\267\350\212\263/.keep" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\251\267\350\212\263/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\251\267\350\212\263/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243 (2).txt" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\251\267\350\212\263/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243 (2).txt" new file mode 100644 index 0000000000000000000000000000000000000000..05448182b8b0a88b9b02641a90f9ea0a6e880001 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\251\267\350\212\263/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243 (2).txt" @@ -0,0 +1,91 @@ +create database student +go + +use student +go +create table Stuinfo +( + StuID int primary key identity(1,1), + StuName nvarchar(10), + StuAge int, + StuSex int, + STime datetime default(cast('2007-01-07 01:11:36.590' as datetime)) +) + +insert into Stuinfo values ('Tom',19,1,null), +('Jack',20,0,null), +('Rose',21,1,null), +('Lulu',19,1,null), +('Lili',21,0,null), +('abc',20,1 ,default) + +select * from Stuinfo + +create table CourseInfo +( + CourseID int primary key identity(1,1), + CourseName nvarchar(10), + CourseMarks int +) + +insert into CourseInfo values('JavaBase',4), +('HTML',2), +('JavaScript',2), +('SqlBase',2) + +select * from CourseInfo + +create table ScoreInfo +( + ScoreID int primary key identity(1,1), + StuID int references Stuinfo(StuID), + CourseID int references CourseInfo(CourseID), + Score int +) + +insert into ScoreInfo 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 ScoreInfo + +select * from Stuinfo +select * from CourseInfo +select * from ScoreInfo + +--鏈夊鍥炬墍绀虹殑涓夊紶琛ㄧ粨鏋勶紝瀛︾敓淇℃伅琛紙stuInfo锛夛紝璇剧▼淇℃伅琛紙courseInfo锛,鍒嗘暟淇℃伅琛紙scoreInfo锛 + +--棰樼洰锛 +--1.鏌ヨ鍑烘瘡涓鐢 鎵閫変慨鐨勮绋嬬殑鏁伴噺 鍜屾墍閫変慨鐨勮绋嬬殑鑰冭瘯鐨勫钩鍧囧垎 +select st.StuID 瀛︾敓鍙,count(co.CourseID) 鎵閫夎绋嬫暟閲,AVG(Score) 骞冲潎鍒 from Stuinfo st +inner join CourseInfo co on st.StuID=co.CourseID +inner join ScoreInfo sc on st.StuID=sc.CourseID group by st.StuID + +--2.鏌ヨ鍑烘瘡闂ㄨ绋嬬殑閫変慨鐨勫鐢熺殑涓暟鍜屽鐢熻冭瘯鐨勬诲垎 +select st.StuID,count(co.CourseID), sum(Score) from Stuinfo st +inner join CourseInfo co on st.StuID=co.CourseID +inner join ScoreInfo sc on st.StuID=sc.CourseID group by st.StuID +--3.鏌ヨ鍑烘у埆涓鏍峰苟涓斿勾榫勪竴鏍风殑瀛︾敓鐨勪俊鎭 +select * from stuInfo s1 join stuInfo s2 on s1.stuName锛=s2.stuName and s1.stuAge=s2.stuAge and s1.stusex=s2.stusex +--鍙︿竴绉嶅仛娉晄elect * from stuInfo t where (select COUNT(*) from stuInfo where stuAge=t.stuAge and stuSex=t.stuSex)>1 +--4.鏌ヨ鍑哄鍒嗕竴鏍风殑璇剧▼淇℃伅 +select * from courseInfo c1 join courseInfo c2 on c1.courseID!=c2.courseID and c1.courseMarks=c2.courseMarks +--5.鏌ヨ鍑哄弬鍔犱簡鑰冭瘯鐨 瀛︾敓鐨勫鍙凤紝濮撳悕锛岃绋嬪彿 鍜 鍒嗘暟 +select s.stuID,stuName,c.courseID,score from scoreInfo s +inner join stuInfo stu on s.stuID=stu.stuID +inner join courseInfo c on s.courseID=c.courseID +--6.鏌ヨ鍑哄弬鍔犱簡鑰冭瘯鐨勫鐢熺殑瀛﹀彿锛岃绋嬪彿锛岃绋嬪悕锛岃绋嬪鍒嗗拰鍒嗘暟 +select s.stuID,stuName,c.courseID,courseMarks,score from scoreInfo s +inner join stuInfo stu on s.stuID=stu.stuID +inner join courseInfo c on s.courseID=c.courseID +--7.鏌ヨ鍑烘病鏈夊弬鍔犺冭瘯鐨勫鐢熺殑瀛﹀彿鍜屽鍚 + select * from stuInfo where stuID not in (select stuID from scoreInfo) + --鍙︿竴绉嶅仛娉晄elect * from stuInfo left join scoreInfo on stuInfo.stuID = scoreInfo.stuID where score is null +--8.鏌ヨ鍑烘槸鍛ㄥ叚鍛ㄥぉ鏉ユ姤鍒扮殑瀛︾敓 + +--9.鏌ヨ鍑哄鍚嶄腑鏈夊瓧姣峚鐨勫鐢熺殑淇℃伅 +select * from stuInfo where stuName like '%a%' +--10.鏌ヨ鍑洪変慨浜2闂ㄨ绋嬩互涓婄殑骞朵笖鑰冭瘯骞冲潎鍒嗗湪70浠ヤ笂鐨 瀛︾敓鐨勫鍙 鍜 鑰冭瘯骞冲潎鍒 浠ュ強 閫変慨璇剧▼鐨勬暟閲 +select s.stuID,stuName,avg(score)骞冲潎鍒,count(c.courseID)璇剧▼鏁 from scoreInfo s +inner join stuInfo stu on s.stuID=stu.stuID +inner join courseInfo c on s.courseID=c.courseID group by s.stuID,stuName having avg(score)>70 diff --git "a/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\274\272/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\274\272/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c4fa82cc12c58531e432580e1d7cb2793d21bbb3 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\200\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\274\272/SQLQuery1.sql" @@ -0,0 +1,108 @@ +use master +go +create database ku +on +( + name='ku', + filename='D:\ku.mdf', + size=10MB, + maxsize=20MB, + filegrowth=10% +) +log on +( + name='ku_log', + filename='D:\ku_log.ldf', + size=5MB, + maxsize=10MB, + filegrowth=2MB +) +go +use ku +go +create table stulnfo +( + stuID int primary key identity(1,1) not null, + stuName nvarchar(10) not null, + stuAge int not null, + stuSex int not null, + time datetime +) + +create table courselnfo +( + courseID int primary key identity(1,1), + courseName nvarchar(10) not null, + courseMarks int not null +) + +create table scoreinfo +( + scoreID int primary key identity(1,1), + stuID int not null, + courseID int not null, + score int not null +) + insert into stulnfo(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') + + insert into courselnfo(courseName,courseMarks) values + ('JavaBase',4), + ('HTML',2), + ('JavaAcript',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 courselnfo + select * from scoreinfo + select * from stulnfo + --1.查询出每个学生所选修的课程的数量和所选修的课程的考试的平均分 + select i.stuName ,count(*)数量 ,avg(score)平均分 from scoreInfo s JOIN stuInfo i on i. stuID= s .stuID group by i.stuName + +--2.查询出每门课程的选修的学生的个数和学生考试的总分 +select c.courseName, count(*) '选修的学生个数', sum(score) '总分'from courseInfo c join scoreInfo s on c.courseID = s.courseID group by c.courseName + +--3.查询出性别一样并且年龄一样的学生的信息 +select s1.* from stuInfo s1 join stuInfo s2 on s1.stuName<>s2.stuName and s1. stuSex=s2.stuSex and s1.stuAge =s2.stuAge + +--4.查询出学分一样的课程信息 +select distinct c1.* from courseInfo c1 join courseInfo c2 on (c1.courseID <> c2.courseID) and (c1.courseMarks = c2.courseMarks ) + + +--5.查询出参加了考试的学生的学号,姓名,课程号和分数 +select st.stuID, st.stuName, s.courseID, s.score from scoreInfo s joinstuInfo st on s.stuID = st.stuID + +--6.查询出参加了考试的学生的学号,课程号,课程名,课程学分和分数 +select s.stuID, s.courseID, c.courseName, c.courseMarks , s.score fromscoreInfo s join courseInfo c on c.courseID = s.courseID + +--7.查询出没有参加考试的学生的学号和姓名 +select st.stuID, st.stuName from stuInfo st left join scoreInfo sc on sc.stuID = st.stuID where sc.scoreID is null + +--8.查询出是周六周天来报到的学生 +select * from stuInfo where DATENAME(dw, time) = '星期六' or DATENAME(dw, time) = '星期日' + +--9.查询出姓名中有字母a的学生的信息 +select * from stuInfo where stuName like '%a%' + +--10.查询出选修了2门课程以上的并且考试平均分在70以上的学生的学号和考试平均分以及选修课程的数量 +select stuID '学号', round(avg(cast(score as float)), 1) '平均成绩', count(*) '选课数量' from scoreInfo group by stuID having count(*) > 2 and round(avg(cast(score as float)), 1) > 70 + + + + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/.keep" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/.keep" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\345\220\257\350\210\252/.keep" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\345\220\257\350\210\252/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\345\220\257\350\210\252/SQLQuery16.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\345\220\257\350\210\252/SQLQuery16.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1d3d03e448d6a9a68f645da0d22d1eb661b2d756 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\345\220\257\350\210\252/SQLQuery16.sql" @@ -0,0 +1,97 @@ +use master +go +create database chargesystem +on +( + name='chargesystem', + filename='D:\chargesystem.mdf', + size=10, + maxsize=50, + filegrowth=5% +) +log on +( + name='chargesystem_log', + filename='D:\chargesystem_log.mdf', + size=10, + maxsize=50, + filegrowth=5% +) +go +use chargesystem +go +create table tbl_card +( + id varchar(10) primary key, + passWord nvarchar(10), + balance int, + userName nvarchar(10) +) +create table tbl_computer +( + id varchar(10) primary key, + onUse char(2) check(onUse=0 or onUse=1), + note varchar(20) +) +create table tbl_record +( + id int primary key, + cardid varchar(10) references tbl_card(id), + Computerid varchar(10) references tbl_computer(id), + beginTime datetime, + endTime datetime, + fee int +) + +insert into tbl_card values ('0023_ABC','555',98,'张军'), +('0025_bbd','abc',300,'朱俊'),('0036_CCD','何柳',100,'何柳'), +('0045_YGR','0045_YGR',58,'证验'),('0078_RJV','55885fg',600,'校庆'), +('0089_EDE','zhang',134,'张峻') + +insert into tbl_computer values ('02',0,25555),('03',0,55555), +('04',0,66666),('05',1,88888),('06',0,688878),('B01',0,558558) + +insert into tbl_record values (23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-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',3), +(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) + + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 + select c.id 卡号,userName 用户名,Computerid 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_card c inner join tbl_record r on c.id=r.cardid + where username='张军'order by fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 + select Computerid 机器编号,COUNT(*) 上网次数,sum(fee) 总金额 from tbl_record group by Computerid +--3. 查询出所有已经使用过的上网卡的消费总金额 + select sum(fee) 消费总金额 from tbl_card c right join tbl_record r on c.id = r.cardid +--4. 查询出从未消费过的上网卡的卡号和用户名 + select c.id,userName from tbl_record r right join tbl_card c on r.cardid = c.id where fee is NULL or fee=' ' +--5. 将密码与用户名一样的上网卡信息查询出来 + select A.* from tbl_card A inner join tbl_card B on A.passWord=B.userName +--6. 查询出使用次数最多的机器号和使用次数 + select top 1 Computerid ,COUNT(*) from tbl_record group by Computerid order by count(*) desc +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 + select c.id,userName,Computerid,fee from tbl_card c inner join tbl_record r on c.id =r.cardid where c.id like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + select c.id,userName,Computerid,beginTime,endTime,fee from tbl_record r inner join tbl_card c on r.cardid = c.id + where datename(DW,beginTime)='星期六' or datename(DW,beginTime)='星期日' +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + select c.id,userName,Computerid,beginTime,endTime,fee from tbl_record r inner join tbl_card c on r.cardid = c.id + where DATEDIFF(HH,beginTime,endTime)>12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + select c.id,userName,Computerid,beginTime,endTime,fee from tbl_card c inner join tbl_record r on c.id=r.cardid + where fee not in(select top 3 fee from tbl_record order by fee desc ) + + + select c.id,userName,Computerid,beginTime,endTime,fee from tbl_card c inner join tbl_record r on c.id=r.cardid + except + select top 3 c.id,userName,Computerid,beginTime,endTime,fee from tbl_card c inner join tbl_record r on c.id=r.cardid order by fee desc +select * from tbl_card +select * from tbl_computer +select * from tbl_record \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\231\346\226\207\350\243\225/.keep" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\231\346\226\207\350\243\225/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\231\346\226\207\350\243\225/SQLQuery4.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\231\346\226\207\350\243\225/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b779ec583a83ca1cfcef34fb06c2dfac7d9082e3 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\231\346\226\207\350\243\225/SQLQuery4.sql" @@ -0,0 +1,118 @@ +use master +go + +create database Card +on +( + name = 'Card', + filename = 'E:\SQLwork\Card.mdf', + size = 10MB, + maxsize = 50MB, + filegrowth = 10% +) +log on +( + name = 'Card_log', + filename = 'E:\SQLwork\Card_log.ldf', + size = 10MB, + maxsize = 50MB, + filegrowth = 10% +) +go + +use Card +go + +create table tbl_card +( + ID char(30) primary key not null, + passWord varchar(50) not null, + balance money, + userName nvarchar(20) +) + +create table tbl_computer +( + ID char(30) primary key, + onUse char(1) check(onUse in(0,1)), + note nvarchar(10) +) + +create table tbl_record +( + RID int primary key , + CardID char(30) references tbl_Card(ID), + CID char(30) references tbl_Computer(ID), + BeginTime datetime, + Endtime datetime, + fee money +) +go + +insert into tbl_card 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 values('02',0,25555),('03',1,55555),('04',0,66666),('05',1,88888),('06',0,688878),('B01',0,558558) + +insert into tbl_record values +(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-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:25:00',50), +(46,'0023_ABC','03','2006-12-23 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',20), +(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',3), +(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) +go + +select * from tbl_card +select * from tbl_computer +select * from tbl_record +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 + +select C.ID,userName,T.ID,beginTime,endTime,fee from tbl_card C inner join tbl_record R on C.ID = R.CardID inner join tbl_computer T on T.ID = R.CID where userName = '张军' order by fee DESC + +--2. 查询出每台机器上的上网次数和消费的总金额 + +select R.CID,COUNT(*)次数,SUM(fee)总金额 from tbl_computer T inner join tbl_record R on T.ID = R.CID group by R.CID + +--3. 查询出所有已经使用过的上网卡的消费总金额 + +select R.CID onUse,SUM(fee)总金额 from tbl_computer T inner join tbl_record R on T.ID = R.CID group by R.CID + +--4. 查询出从未消费过的上网卡的卡号和用户名 + +select C.ID,userName from tbl_card C left join tbl_record R on C.ID = R.CardID where fee is NULL + +--5. 将密码与用户名一样的上网卡信息查询出来 + +select A.* from tbl_Card A,TBL_Card B where A.PassWord=B.UserName + +--6. 查询出使用次数最多的机器号和使用次数 + +select top 1 cardid,count(*)使用次数 from tbl_record group by cardid + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 + +select userName,tbl_record.CID,fee from tbl_record inner join tbl_card on tbl_card.id=tbl_record.cardid +where cardid like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select tbl_card.id,tbl_card.userName,tbl_record.CID,beginTime,endTime,tbl_record.fee from tbl_record +inner join tbl_card on tbl_card.id=tbl_record.cardid +where datename(DW,beginTime)='星期六' or datename(DW,beginTime)='星期日' + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select cardid,tbl_card.userName,ID,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardid=tbl_card.id +where DATEDIFF(hh,beginTime,endTime)>12 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select top 3 R.cardID 卡号, userName 用户名, ID 机器编号,beginTime 开始时间,endTime 结束时间, fee 消费金额 from +tbl_record R inner join tbl_card C on R.cardID=C.ID order by fee DESC + + diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\262\263\345\220\257\345\215\216/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\262\263\345\220\257\345\215\216/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a8709ae2f60638e6b11c6d76d135f880b6f9675d --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\262\263\345\220\257\345\215\216/SQLQuery1.sql" @@ -0,0 +1,161 @@ +use master +go + +create database TD +go +use TD +go + +create table Tbl_card +( + ID nchar(8) primary key, + PassWord varchar(20) unique not null, + Balance int, + UserName nvarchar(5) +) + +insert into + Tbl_card +values + ('0023_ABC','555',98,'张军'), + ('0025_bbd','abe',300,'朱俊'), + ('0036_CCD','何柳',100,'何柳'), + ('0045_YGR','0045_YGR',58,'证验'), + ('0078_RJV','55885fg',600,'校庆'), + ('0089_EDF','zhang',134,'张峻') + +create table Tbl_computer +( + ID varchar(5) primary key, + OnUse char(1) default('0') check(OnUse in(0, 1)), + note varchar(10) +) + +insert into + Tbl_computer +values + ('02',0,'25555'), + ('03',1,'55555'), + ('04',0,'66666'), + ('05',1,'88888'), + ('06',0,'688878'), + ('B01',0,'558558') + +create table Tbl_record +( + ID int primary key, + CardID nchar(8) foreign key references Tbl_card(ID), + ComputerID varchar(5) foreign key references Tbl_computer(ID), + BeginTime datetime, + EndTime datetime, + Fee int +) + +insert into + Tbl_record +values + (23,'0078_RJV','B01','2007-07-25 19:00:00','2007-07-25 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-23 22:55:00',6), + (47,'0023_ABC','03','2006-12-24 15:26:00','2006-12-24 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',35), + (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) + +-- 1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select + Tc.UserName,Tr.* +from + Tbl_record Tr +join + Tbl_card Tc +on + UserName = '张军' +order by + Fee +desc +-- 2. 查询出每台机器上的上网次数和消费的总金额 +select + ComputerID 机器ID,count(ComputerID) 上网次数,sum(Fee) 总金额 +from + Tbl_record +group by + ComputerID + +-- 3. 查询出所有已经使用过的上网卡的消费总金额 +select + Tr.ComputerID 上网卡ID,sum(Fee) 总金额 +from + Tbl_record Tr +join + Tbl_Computer Tc +on + Tr.ComputerID = Tc.ID and Tc.OnUse = 1 +group by + Tr.ComputerID + +-- 4. 查询出从未消费过的上网卡的卡号和用户名 +select + distinct Tc.UserName, Tc.ID +from + Tbl_record Tr +join + Tbl_card Tc +on + Tc.ID not in + ( + select distinct CardID from Tbl_record + ) + +-- 5. 将密码与用户名一样的上网卡信息查询出来 +select + * +from + Tbl_card +where + UserName = PassWord + +-- 6. 查询出使用次数最多的机器号和使用次数 + +select + top 1 Max(ComputerID) 机器号,count(ComputerID) 使用次数 +from + Tbl_record + +-- 7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select + * +from + Tbl_record +where + CardID like ('%ABC') + +-- 8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select + * +from + Tbl_record +where + datepart(weekday,BeginTime) = 7 or datepart(weekday,BeginTime) = 1 + +-- 9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select + * +from + Tbl_record +where + Datediff(hh,BeginTime,EndTime) > 12 + +-- 10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select + top 3 * +from + Tbl_record +order by + Fee +desc + + diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\351\223\255\350\275\251/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\351\223\255\350\275\251/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8ed22b984730b4307ea9e3860027e8de61f7b655 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\351\223\255\350\275\251/SQLQuery1.sql" @@ -0,0 +1,136 @@ +use master + +go + +create database tbl + +on +( +name ='tbl', +filename = 'D:\tbl.mdf', +size=5, +maxsize = 10, +filegrowth=10% + +) +log on +( +name ='tbl_log', +filename = 'D:\tbl', +size=5, +maxsize = 10, +filegrowth=10% + +) +go + +use tbl + +create table tbl_card +( +id nvarchar(10) primary key , +PassWord varchar(20) not null , +BaLance money , +UserName varchar(10) +) + +create table tbl_computer +( + +id nvarchar(10) primary key , +OnUse char(1) check(OnUse in(1,0)), +NOte text +) +create table tbl_record +( +id int primary key , +CardID nvarchar(10) references tbl_card(id), +ConputerID nvarchar(10) references tbl_computer(id), +BeginTime datetime, +EndTime datetime, +Fee money + +) +insert into tbl_card values +('0023_Abc','555','98','张军'), +('0025_bbd','abc','300','朱俊'), +('0036_CCD','何柳','100','何柳'), +('0045_YGR','0045_YGR','58','验证'), +('0078_RJV','55885fg','600','校庆'), +('0089_EDE','zhang','134','张峻') + +insert into tbl_computer values +('02','0','25555'), +('03','1','55555'), +('04','0','66666'), +('05','1','88888'), +('06','0','688878'), +('B01','0','558558') + + +insert into tbl_record values +('23','0078_RJV','B01','2007-07-15 19:00:00','2007-07-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-28 22:00:00','3'), +('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') + + +--1. 查询出用户名为'张军' 的上网卡的 上网记录 ,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按 消费金额 降序排列 +select* from tbl_card +select* from tbl_computer +select* from tbl_record +select* from tbl_card a inner join tbl_record r on a.id=r.CardID where UserName ='张军'order by Fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 +select ConputerID 机器编号,COUNT(*) 上网次数,sum(fee) 总金额 from tbl_record group by ConputerID +--3. 查询出所有已经使用过的上网卡的消费总金额 + select sum(fee) 消费总金额 from tbl_card c right join tbl_record r on c.id = r.cardid +--4. 查询出从未消费过的上网卡的卡号和用户名 + select c.id,userName from tbl_record r right join tbl_card c on r.cardid = c.id where fee is NULL or fee=' ' +--5. 将密码与用户名一样的上网卡信息查询出来 + select A.* from tbl_card A inner join tbl_card B on A.passWord=B.userName +--6. 查询出使用次数最多的机器号和使用次数 + select top 1 ConputerID ,COUNT(*) from tbl_record group by ConputerID order by count(*) desc +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 + select c.id,userName,ConputerID,fee from tbl_card c inner join tbl_record r on c.id =r.cardid where c.id like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + select c.id,userName,ConputerID,beginTime,endTime,fee from tbl_record r inner join tbl_card c on r.cardid = c.id + where datename(DW,beginTime)='星期六' or datename(DW,beginTime)='星期日' +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + select c.id,userName,ConputerID,beginTime,endTime,fee from tbl_record r inner join tbl_card c on r.cardid = c.id + where DATEDIFF(HH,beginTime,endTime)>12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + select c.id,userName,ConputerID,beginTime,endTime,fee from tbl_card c inner join tbl_record r on c.id=r.cardid + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\345\211\221\351\230\263/.keep" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\345\211\221\351\230\263/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\345\211\221\351\230\263/work2.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\345\211\221\351\230\263/work2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e328f2725162fd04d82deb65a3938c1b8f36960b --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\345\211\221\351\230\263/work2.sql" @@ -0,0 +1,94 @@ +create database intent +on +( +name = 'intent', +filename = 'D:\intent.mdf', +size = 5, +maxsize = 10, +filegrowth = 10% +) +log on +( +name = 'intent_log', +filename = 'D:\intent_log.ldf', +size = 5, +maxsize = 10, +filegrowth = 10% +) +go +use intent +go +create table tbl_card +( +ID char(10) primary key, +passWord char(10), +balance int, +userName nvarchar(10) +) +create table tbl_computer +( +ID char(10) primary key, +OnUse int check(OnUse in(0,1)), +note int +) +create table tbl_record +( +ID int primary key, +CardID char(10) references tbl_card(ID), +ComputerID char(10) references tbl_computer(ID), +beginTime datetime, +endTime datetime, +Fee int +) +select * from tbl_card +select * from tbl_computer +select * from tbl_record + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select tca.ID,userName,ComputerID,beginTime,endTime,Fee from tbl_record tr +inner join tbl_card tca on tr.CardID=tca.ID +where userName='张军' order by Fee desc + +--2. 查询出每台机器上的上网次数和消费的总金额 +select computerID,COUNT(*) 上网次数,SUM(Fee) 消费总金额 from tbl_record group by computerID + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select CardID,SUM(Fee) 消费总金额 from tbl_record tr +left join tbl_card tca on tr.CardID=tca.ID group by CardID + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select tca.ID,userName from tbl_record tr +right join tbl_card tca on tr.CardID=tca.ID +where CardID IS NULL group by tca.ID,userName + +--5. 将密码与用户名一样的上网卡信息查询出来 +select a.* from tbl_card a,tbl_card b where a.ID=b.passWord + +--6. 查询出使用次数最多的机器号和使用次数 +select ComputerID,COUNT(*) 使用次数 from tbl_record tr +inner join tbl_computer tco on tr.ComputerID=tco.ID +group by ComputerID +having COUNT(*)=(select top 1 COUNT(*) from tbl_record group by ComputerID order by COUNT(*) desc) + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select tca.ID,userName,ComputerID,Fee from tbl_card tca +inner join tbl_record tr on tca.ID=tr.CardID +where tca.ID like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tr.CardID,userName,ComputerID,beginTime,endTime,Fee from tbl_record tr +inner join tbl_card tca on tr.CardID=tca.ID +where datename(DW,beginTime)='星期六' or datename(DW,beginTime)='星期日' + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tr.CardID,userName,ComputerID,beginTime,endTime,Fee from tbl_record tr +inner join tbl_card tca on tr.CardID=tca.ID +where DATEDIFF(HH,beginTime,endTime)>12 + +--10. 查询出消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 tr.CardID,userName,ComputerID,beginTime,endTime,Fee from tbl_record tr +inner join tbl_card tca on tr.CardID=tca.ID +order by Fee desc + + + diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\233\271\346\226\207\346\226\214/SQLQuery11-2.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\233\271\346\226\207\346\226\214/SQLQuery11-2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..237be26c6b3f6b871a6a7f942704761e085c5d56 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\233\271\346\226\207\346\226\214/SQLQuery11-2.sql" @@ -0,0 +1,98 @@ +use master +go +create database chargesystem +on +( + name='chargesystem', + filename='D:\chargesystem.mdf', + size=10, + maxsize=50, + filegrowth=5% +) +log on +( + name='chargesystem_log', + filename='D:\chargesystem_log.mdf', + size=10, + maxsize=50, + filegrowth=5% +) +go +use chargesystem +go +create table tbl_card +( + id varchar(10) primary key, + passWord nvarchar(10), + balance int, + userName nvarchar(10) +) +create table tbl_computer +( + id varchar(10) primary key, + onUse char(2) check(onUse=0 or onUse=1), + note varchar(20) +) +create table tbl_record +( + id int primary key, + cardid varchar(10) references tbl_card(id), + Computerid varchar(10) references tbl_computer(id), + beginTime datetime, + endTime datetime, + fee int +) + +insert into tbl_card values ('0023_ABC','555',98,'张军'), +('0025_bbd','abc',300,'朱俊'),('0036_CCD','何柳',100,'何柳'), +('0045_YGR','0045_YGR',58,'证验'),('0078_RJV','55885fg',600,'校庆'), +('0089_EDE','zhang',134,'张峻') + +insert into tbl_computer values ('02',0,25555),('03',0,55555), +('04',0,66666),('05',1,88888),('06',0,688878),('B01',0,558558) + +insert into tbl_record values (23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-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',3), +(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 tbl_card.id,userName,tbl_record.id,beginTime,endTime,fee from tbl_record +inner join tbl_card on tbl_card.id=tbl_record.cardid +where userName='张军'order by fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 +select Computerid,count(*)上网次数,sum(fee)总金额 from tbl_record group by Computerid,fee +--3. 查询出所有已经使用过的上网卡的消费总金额 +select sum(fee) from tbl_record inner join tbl_card on tbl_record.cardid=tbl_card.id +--4. 查询出从未消费过的上网卡的卡号和用户名 +select tbl_card.id,userName from tbl_card +left join tbl_record on tbl_record.cardid=tbl_card.id where fee is null +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from tbl_card A inner join tbl_card B on A.passWord=B.userName +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 cardid,count(*)使用次数 from tbl_record group by cardid +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select userName,tbl_record.Computerid,fee from tbl_record inner join tbl_card on tbl_card.id=tbl_record.cardid +where cardid like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tbl_card.id,tbl_card.userName,tbl_record.Computerid,beginTime,endTime,tbl_record.fee from tbl_record +inner join tbl_card on tbl_card.id=tbl_record.cardid +where datename(DW,beginTime)='星期六' or datename(DW,beginTime)='星期日' +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tbl_record.id,userName,Computerid,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_card.id=cardid +where (beginTime-endTime)>12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 fee, tbl_record.id,userName,Computerid,beginTime,endTime from tbl_record inner join tbl_card on tbl_card.id=cardid +order by fee desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\233\276\344\273\216\346\235\234/SQLQueryZ2.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\233\276\344\273\216\346\235\234/SQLQueryZ2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c359ef69d4d4cda7ab640218aa1b924a7dc755e5 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\233\276\344\273\216\346\235\234/SQLQueryZ2.sql" @@ -0,0 +1,66 @@ +use master +go +create database billing +use billing +go +create table tbl_card +( +id char(10) primary key, +passWord char(20), +banlance money, +userName nvarchar(10) +) +create table tbl_computer +( +id char(10) primary key , +onUse int check(onUse=0 or onUse=1), +note text +) +create table tbl_record +( +id int primary key, +cardid char(10) references tbl_card(id), +ComputerId char(10) references tbl_computer(id), +beginTime date, +endTime date, +fee money +) +insert into tbl_card values +('0023_ABC','555',98,'张军'), +('0025_bbd','abe',300,'朱骏'), +('0036_CCD','何柳',100,'何柳'), +('0045_YGR','0045_YGR',58,'证验'), +('0078_RJV','5585fg',600,'校庆'), +('0089_EDE','zhang',134,'张峻') +insert into tbl_computer values +('02',0,'25555'), +('03',1,'55555'), +('04',0,'66666'), +('05',1,'88888'), +('06',0,'688878'), +('B01',0,'558558') +insert into tbl_record 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','2007-07-21 15:26:00','2007-07-21 22:55:00',50), +('64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',23), +('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 tbl_card.id,userName,tbl_record.ComputerId,tbl_record.beginTime,tbl_record.endTime,tbl_record.fee from tbl_card inner join tbl_record on tbl_card.id=tbl_record.cardid where userName='张军' order by fee desc +select ComputerId,COUNT(ComputerId) 上网次数,sum(fee) 消费总金额 from tbl_record group by ComputerId +select cardid,sum(fee) 消费总金额 from tbl_record group by cardid +select cardid,userName from tbl_card left join tbl_record on tbl_card.id=tbl_record.cardid where fee is null +select * from tbl_card where passWord=userName +select top 1 ComputerId,count(ComputerId) 次数 from tbl_record group by ComputerId order by count(ComputerId) desc +select cardid,tbl_card.userName,ComputerId,fee from tbl_record inner join tbl_card on tbl_record.cardid=tbl_card.id where cardid like '%ABC' +select cardid,tbl_card.userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardid=tbl_card.id where DATENAME(DW,beginTime)='星期日' or DATENAME(DW,beginTime)='星期六' +select cardid,tbl_card.userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardid=tbl_card.id where DATEDIFF(hh,beginTime,endTime)>12 +select top 3 cardid,tbl_card.userName,ComputerId,beginTime,endTime,fee from tbl_record inner join tbl_card on tbl_record.cardid=tbl_card.id order by fee desc + +select * from tbl_card +select * from tbl_computer +select * from tbl_record \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\344\274\237/1111.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\344\274\237/1111.sql" new file mode 100644 index 0000000000000000000000000000000000000000..19f26080a63d5f2f48ac3403f33f2bf30f937dfb --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\344\274\237/1111.sql" @@ -0,0 +1,122 @@ +use master +go +create database cbb +on +( +name='cbb', +filename='D:\cbb.mdf', +size=5mb, +maxsize=20mb, +filegrowth=10% +) +log on +( +name='cbb_log', +filename='D:\cbb_log.ldf', +size=5mb, +maxsize=20mb, +filegrowth=10% +) +use cbb +go +create table tbl_card +( +CardId char(30) primary key , +passWord char(20) not null, +balance int not null, +userName nvarchar(20) not null +) +create table tbl_computer +( +ComputerId varchar(20) primary key , +onUse char not null, +note int +) +create table tbl_record +( +RecordId int primary key , +cardId char(30) references tbl_card(CardId) not null , +ComputerId varchar(20) references tbl_computer(ComputerId) not null, +beginTime datetime, +endTime datetime, +fee money +) +insert into tbl_card 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,'张峻') +select * from tbl_card +insert into tbl_computer values +('02',0,25555), +('03',1,55555), +('04',0,66666), +('05',1,88888), +('06',0,688878), +('B01',0,558558) +select * from tbl_computer +insert into tbl_record values +(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-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_record + + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select Tc.CardId,userName,ComputerId ,beginTime ,endTime ,fee from tbl_card TC +inner join tbl_record TR on TC.CardId=TR.cardId where userName='张军' +order by fee desc + +--2. 查询出每台机器上的上网次数和消费的总金额 +select TC.ComputerId,count(*),sum(fee) from tbl_computer TC +inner join tbl_record TR on TC.ComputerId=TR.ComputerId group by TC.ComputerId + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select TC.CardId,sum(fee) from tbl_card TC +inner join tbl_record TR on TC.CardId=TR.cardId group by TC.CardId + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select TC.CardId,userName from tbl_card TC +left join tbl_record TR on TC.CardId=TR.cardId where fee is NULL or fee ='' + +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from tbl_card A +inner join tbl_card B on A.passWord=B.userName + +--6. 查询出使用次数最多的机器号和使用次数 +select TC.ComputerID,count(*)from tbl_computer TC +inner join tbl_record TR on TC.ComputerId=TR.ComputerId +group by TC.ComputerId + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select TC.CardId,userName,ComputerId ,sum(fee) from tbl_card TC +inner join tbl_record TR on TC.CardId=TR.cardId where TC.CardId like '%ABC'GROUP BY TC.CardId,userName,ComputerId + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +SELECT TC.CardId,userName ,ComputerId,beginTime,endTime ,SUM(fee) from tbl_card TC +inner join tbl_record TR on TC.CardId=TR.cardId +group by TC.CardId,TC.userName,TR.ComputerId,beginTime,endTime having DATENAME(DW,beginTime)='星期六'or DATEPART(DW,beginTime)=1 + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、 +select tc.cardId, tc.userName, tr.ComputerId, tr.beginTime, tr.endTime, tr.fee +from tbl_record tr +join tbl_card tc on tr.cardId = tc.cardId +where + DATEDIFF(HH, tr.beginTime, tr.endTime) > 12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tc.cardId, tc.userName, tr.ComputerId, tr.beginTime, tr.endTime, tr.fee +from tbl_record tr +join tbl_card tc +on tr.cardId = tc.cardId +where tr.fee in ( select distinct top 3 fee from tbl_record order by fee desc) order by tr.fee desc + + diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\350\213\261\347\276\244/.keep" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\350\213\261\347\276\244/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\350\213\261\347\276\244/\344\275\234\344\270\23212.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\350\213\261\347\276\244/\344\275\234\344\270\23212.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9cd05910254b9b9155538d0f1578b9b0e522d255 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\350\213\261\347\276\244/\344\275\234\344\270\23212.sql" @@ -0,0 +1,118 @@ +use master +go +create database tbl +on +( + name='tbl', + filename='D:\tbl.mdf', + size=50MB, + filegrowth=10% +) +log on +( + name='tbl_log', + filename='D:\tbl_log.ldf', + size=50MB, + filegrowth=10% +) + +go +use tbl +go +--上网卡信息表 +create table tbl_card +( + id char(30) primary key not null, + passWord varchar(50) not null, + balance money, + userName nvarchar(10) not null +) +insert into tbl_card values +('0023_ABC','555',98,'张军'), +('0025_bbd','abe',300,'朱俊'), +('0036_CCD','何柳',100,'何柳'), +('0045_YGR','00445_YGR',58,'证验'), +('0078_RJV','55885fg',600,'校庆'), +('0089_EDE','zhang',134,'张俊') + +--网吧机器说明表 +create table tbl_computer +( + id char(30) primary key not null, + onUse int not null check(onUse='0' or onUse='1'), + note varchar(100) not null +) +insert into tbl_computer values +('02','0','25555'), +('03','1','55555'), +('04','0','66666'), +('05','1','88888'), +('06','0','688878'), +('B01','0','558558') + + +--上网记录表 +create table tbl_record +( + id int primary key not null, + cardId char(30) not null references tbl_card(id), + ComputerId char(30) not null references tbl_computer(id), + beginTime datetime, + endTime datetime, + fee money +) + +insert into tbl_record values +(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-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:25:00',50), +(46,'0023_ABC','03','2006-12-23 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',20), +(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',3), +(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 tbl_card.id 卡号,tbl_card.userName 用户名,tbl_record.ComputerId 机器编号,tbl_record.beginTime 开始时间, +tbl_record.endTime 结束时间,tbl_record.fee 消费金额 from tbl_record +inner join tbl_card on tbl_card.id=tbl_record.cardId where tbl_card.userName='张军'order by tbl_record.fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 +select tbl_computer.id 机器编号, COUNT(ComputerId) 上网次数,SUM(fee) 消费的总金额 from tbl_record inner join tbl_computer on tbl_computer.id=tbl_record.ComputerId +group by tbl_computer.id +--3. 查询出所有已经使用过的上网卡的消费总金额 +select computerId, SUM(fee) 消费总金额 from tbl_record GROUP BY computerId +--4. 查询出从未消费过的上网卡的卡号和用户名 +select CD.id,CD.userName from tbl_card CD inner join tbl_computer CR on CR.id=CD.id where CR.onUse='0' group by CD.id,CD.userName +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from tbl_card where tbl_card.userName=tbl_card.passWord +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 COUNT(id) 使用次数最多,ComputerId 机器号 from tbl_record group by ComputerId order by COUNT(id) desc +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select userName,ComputerId,fee from tbl_card C +inner join tbl_record R ON R.cardId=C.id +where C.id like '%ABC' + +select RD.cardId 卡号,CD.userName 用户名,RD.ComputerId 上网的机器号,RD.fee 消费金额 from tbl_record RD +inner join tbl_card CD on CD.id=RD.cardId +where RD.cardId LIKE '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select RD.cardId 上网的卡号,CD.userName 用户名,RD.ComputerId 机器号, +RD.beginTime 开始时间,RD.endTime 结束时间,RD.fee 消费金额 from tbl_record RD +inner join tbl_card CD on CD.id=RD.cardId where DATENAME(DW,beginTime)='星期六' or DATENAME(DW,beginTime)='星期天' +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardId 卡号,userName 用户名,ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 +from tbl_record join tbl_card on tbl_record.cardId = tbl_card.id +where datediff(HH,BeginTime,Endtime)>12 +--10. 查询出消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardId 卡号,userName 用户名,ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 +from tbl_record join tbl_card on tbl_record.cardId = tbl_card.id +where fee in (select top 3 fee from tbl_record order by fee DESC) +order by fee DESC \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\344\270\226\347\221\236/.keep" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\344\270\226\347\221\236/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\344\270\226\347\221\236/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\344\270\226\347\221\236/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..fc7145d7b72602648c94c29c7bdb45c46a68f56b --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\250\344\270\226\347\221\236/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232.sql" @@ -0,0 +1,124 @@ +create database 第十一次作业 +on +( + name='第十一次作业', + size=5, + filename ='D:\test\第十一次作业.mdf', + maxsize=100, + filegrowth=5 +) +log on +( + name='第十一次作业_log', + size=5, + filename ='D:\test\第十一次作业_log.ldf', + maxsize=100, + filegrowth=5 +) +go +use 第十一次作业 +go + +create table tbl_card +( + id varchar(10) primary key, + passWord varchar(20), + balance money, + userName varchar(20) +) + +create table tbl_computer +( + id varchar(10) primary key, + oneUse varchar(2) check(oneUse='0' or oneUse='1') default(0), + note varchar(200) +) +go +create table tbl_record +( + id varchar(10) primary key, + cardId varchar(10) references tbl_card(id), + ComputerId varchar(10) references tbl_computer(id), + beginTime datetime not null, + endTime datetime not null, + fee money not null +) + +insert into tbl_card values +('0023_ABC','555',98,'张军'), +('0025_bbd','abe',300,'朱俊'), +('0036_CCD','何柳',100,'何柳'), +('0045_YGR','0045_YGR',58,'证验'), +('0078_RJV','55885fg',600,'校庆'), +('0084_ECD','zhang',134,'张峻') + +insert into tbl_computer values +('02','0','25555'), +('03','1','55555'), +('04','0','66666'), +('05','1','88888'), +('06','0','6888778'), +('B01','0','558558') + +insert into tbl_record values +('24','0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 19: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:25:00','2007-1-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','3'), +('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') + + + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select A.id 卡号,userName 用户名,ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 +from tbl_record C inner join tbl_card A on A.id=C.cardId where userName='张军' order by fee desc + + +--2. 查询出每台机器上的上网次数和消费的总金额 +select ComputerId 机器编号, count(id) 上网次数, sum(fee) 消费总金额 from tbl_record group by fee,ComputerId + + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardId 卡号,sum(fee) 消费总额 from tbl_record group by cardId + + +--4. 查询出从未消费过的上网卡的卡号和用户名 + +select A.id 卡号,userName 用户名 from tbl_card A left join tbl_record C on A.id=C.cardId where C.id is null + +--5. 将密码与用户名一样的上网卡信息查询出来 + +select * from tbl_card where userName=passWord + +--6. 查询出使用次数最多的机器号和使用次数 + +select top 1 ComputerId 机器号, count(*) 使用次数 from tbl_record group by ComputerId order by count(*) desc + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 + +select A.id 卡号,userName 用户名,ComputerId 上网机器编号,sum(fee) 消费金额 from tbl_card A +inner join tbl_record C on A.id=C.cardId where CardId like '%ABC' +group by A.id,userName,ComputerId + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select * from tbl_record where datename(weekday,beginTime)='星期六' or datename(weekday,beginTime)='星期日' + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select cardId 卡号,userName 用户名,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 消费金额 +from tbl_record C inner join tbl_card A on A.id=C.cardId +where endTime-beginTime>=12 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardId 卡号,userName 用户名,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 消费金额 +from tbl_record C inner join tbl_card A on A.id=C.cardId +except +select top 3 cardId 卡号,userName 用户名,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 消费金额 +from tbl_record C inner join tbl_card A on A.id=C.cardId +order by fee diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\200\235\345\260\271/.keep" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\200\235\345\260\271/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\200\235\345\260\271/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\200\235\345\260\271/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f9ca5cae6a6ee83bfebb4a17b053a337993ca3dd --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\200\235\345\260\271/SQLQuery2.sql" @@ -0,0 +1,143 @@ +use master +go +create database internet_bar +on +(name='internet_bar', +filename='D:\internet_bar.mdf', +size=10mb, +maxsize=100mb, +filegrowth=10% +) +log on +(name='internet_bar_log', +filename='D:\internet_bar_log.ldf', +size=10mb, +maxsize=100mb, +filegrowth=10% +) +go +use internet_bar +go +create table tbl_card +(id varchar (10) primary key , +[password] nvarchar(20) check(len(passWord) between 2 and 20) not null, +balance decimal(10,1), +userName nvarchar(20) not null +) +go +create table tbl_computer +(id varchar(10) primary key, +onUse int check(onUse in (0,1)), +note text +) +go + +create table tbl_record +( + id varchar(10) primary key, + cardId varchar(10) references tbl_card(id), + ComputerId varchar(10) references tbl_computer(id), + beginTime smalldatetime, + endTime smalldatetime, + fee decimal(10,1) +) +go + +insert into tbl_card 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, '张峻') +go + +insert into tbl_computer values +('02', 0, '25555'), +('03', 1, '55555'), +('04', 0, '66666'), +('05', 1, '88888'), +('06', 0, '688878'), +('B01', 0, '558558') +go + +insert into tbl_record values +('23', '0078_RJV', 'B01', '2007-07-15 19:00:00', '2007-07-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', 3), +('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) +go + +select* from tbl_card +select* from tbl_computer +select* from tbl_record + + + + +--请完成以下题目: +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select c.id '卡号',userName '用户名',ComputerId '机器编号', beginTime '开始时间', endTime '结束时间', fee '消费金额'from tbl_record r join tbl_card c on r.cardId = c.id where userName='张军' + 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 c.id, userName from tbl_card c left join tbl_record r on r.cardId = c.id where r.id is null +--5. 将密码与用户名一样的上网卡信息查询出来 +select id,password,balance, userName from tbl_card where password=userName +select c1. * from tbl_card c1 join tbl_card c2 on c1.[passWord] =c2.userName + +--6. 查询出使用次数最多的机器号和使用次数 +select ComputerId, count(*) '使用次数' from tbl_record group by ComputerId having count(*) = (select max(t.使用次数) from (select ComputerId, count(*) '使用次数' from tbl_record group by ComputerId) t) + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select c .id , userName, ComputerId,fee from tbl_record r join tbl_card c on r.cardId = c.id where c.id like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select c.id, c.userName, r.ComputerId, r.beginTime, r.endTime, r.fee from tbl_record r join tbl_card c +on + r.cardId = c.id +where + DATENAME(dw, r.beginTime) in ('星期六','星期日') +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select + c.id,c.userName, r.ComputerId, r.beginTime,r.endTime, r.fee from tbl_record r +join + tbl_card c +on + r.cardId = c.id +where + DATEDIFF(HH, r.beginTime, r.endTime) > 12 +--10. 查询出消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select + c.id, c.userName, r.ComputerId, r.beginTime, r.endTime, r.fee +from + tbl_record r +join + tbl_card c +on + r.cardId = c.id +where + r.fee in ( + select + top 3 fee + from + tbl_record + order by + fee desc + ) +order by + r.fee desc + diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\242\201\344\270\226\350\261\252/3.31zuoye2.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\242\201\344\270\226\350\261\252/3.31zuoye2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..1259560e217dc09df784aae327a1449d547becaf --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\242\201\344\270\226\350\261\252/3.31zuoye2.sql" @@ -0,0 +1,118 @@ +use master +go +create database wb +on +( + name ='wb.mdf', + filename='D:\wb.mdf', + size=5, + maxsize=10, + filegrowth=15% +) +log on +( + name ='wb_log.ldf', + filename='D:\wb_log.ldf', + size=5, + maxsize=10, + filegrowth=15% +) +go +use wb +go +create table tbl_card +( + id nvarchar(20) primary key, + passWord nvarchar(20) not null, + balance money, + userName nvarchar(10) +) +create table tbl_computer +( + id nvarchar(20) primary key, + onUse nvarchar(1) check((onUse) in ('0','1')), + note text +) +create table tbl_record +( + id nvarchar(20) primary key, + cardId nvarchar(20) references tbl_card(id), + ComputerId nvarchar(20) 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','张峻') +go +insert into tbl_computer values +('02', '0', '25555'), +('03', '1', '55555'), +('04', '0', '66666'), +('05', '1', '88888'), +('06', '0', '688878'), +('B01', '0', '558558') +go + +insert into tbl_record values +('23', '0078_RJV', 'B01', '2007-07-15 19:00:00', '2007-07-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', 30), +('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) +go +select * from tbl_card --上网卡信息表(tbl_card) +select * from tbl_computer--网吧机器说明表(tbl_computer) +select * from tbl_record --上网记录表(tbl_record) +--1. 查询出用户名为'张军'的上网卡的上网记录, +--要求显示卡费金额,号,用户名,机器编号、开始时间、结束时间,和消并按消费金额降序排列 +select tbl_card.id 卡号,tbl_card.userName 用户名,tbl_record.ComputerId 机器编号, +tbl_record.beginTime 开始时间,tbl_record.endTime 结束时间 ,tbl_record.fee 消费金额 + from tbl_record inner join tbl_card on +tbl_record.cardId=tbl_card.id where tbl_card.userName='张军' order by tbl_record.fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 +select tbl_computer.id 每台机器,count(*) 上网次数,sum(fee) 消费的总金额 from tbl_record inner join tbl_computer on +tbl_computer.id=tbl_record.ComputerId group by tbl_computer.id +--3. 查询出所有已经使用过的上网卡的消费总金额 +select tbl_record.cardId 上网卡, sum(fee) 消费总金额 from tbl_record inner join tbl_card on +tbl_record.cardId=tbl_card.id where tbl_record.fee is not null group by tbl_record.cardId +--4. 查询出从未消费过的上网卡的卡号和用户名 +select tbl_record.cardId 上网卡, tbl_card.id 卡号,tbl_card.userName 用户名 from tbl_record right join tbl_card on +tbl_record.cardId=tbl_card.id where tbl_record.fee is null group by tbl_record.cardId , tbl_card.id ,tbl_card.userName +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from tbl_card where passWord =userName +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 count(id)使用次数, ComputerId 机器号 from tbl_record + group by ComputerId order by count(id) desc +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select tbl_card.id 卡号,tbl_card.userName 用户名,tbl_record.ComputerId 机器编号,tbl_record.fee 消费金额 + from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id where tbl_card.id like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tbl_card.id 卡号,tbl_card.userName 用户名,tbl_record.ComputerId 机器编号, +tbl_record.beginTime 开始时间,tbl_record.endTime 结束时间, +tbl_record.fee 消费金额 + from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id + where datename(DW,beginTime)='星期六' or datename(DW,beginTime)='星期日' +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号, +--开始时间、结束时间和消费金额 +select tbl_card.id 卡号,tbl_card.userName 用户名,tbl_record.ComputerId 机器编号, +tbl_record.beginTime 开始时间,tbl_record.endTime 结束时间, +tbl_record.fee 消费金额 from +tbl_record full join tbl_card on tbl_record.cardId=tbl_card.id + where datediff(HOUR,beginTime,endTime)>12 +--10. 查询出消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号, +--开始时间、结束时间和消费金额 +select top 3 tbl_card.id 卡号,tbl_card.userName 用户名,tbl_record.ComputerId 机器编号, +tbl_record.beginTime 开始时间,tbl_record.endTime 结束时间, +tbl_record.fee 消费金额 from +tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id +order by tbl_record.fee desc + + + diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\242\201\351\223\266\350\212\235/.keep" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\242\201\351\223\266\350\212\235/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\242\201\351\223\266\350\212\235/\346\242\201\351\223\266\350\212\235/SQLQuery21.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\242\201\351\223\266\350\212\235/\346\242\201\351\223\266\350\212\235/SQLQuery21.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e5cc1e53c33024c26ddb90de565e85452b3c4770 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\242\201\351\223\266\350\212\235/\346\242\201\351\223\266\350\212\235/SQLQuery21.sql" @@ -0,0 +1,134 @@ +use master +go +create database tbl +on +( + name='tbl', + filename='D:\SQL\tbl.mdf', + size=5MB, + maxsize=20MB, + filegrowth=10% +) +log on +( + name='tbl_log', + filename='D:\SQL\tbl_log.ldf', + size=5MB, + maxsize=20MB, + filegrowth=10% +) +go + +use tbl +go + +create table tbl_card +( +id varchar(10) primary key, +passWord varchar(10), +balance varchar(5), +userName nvarchar(2) +) +go + +create table tbl_computer +( +id varchar(5) primary key, +onUse varchar(1) check(onUse=0 or onUse=1), +note varchar(10) +) +go + +create table tbl_record +( +id varchar(2) primary key, +cardId varchar(10) references tbl_card(id), +ComputerId varchar(5) references tbl_computer(id), +beginTime datetime, +endTime datetime, +fee money +) +go + +insert into tbl_card values +('0023_ABC','555',98,'张军'), +('0025_bbd','abe',300,'朱俊'), +('0036_CCD','何柳',100,'何柳'), +('0045_YGR','00445_YGR',58,'证验'), +('0078_RJV','55885fg',600,'校庆'), +('0089_EDE','zhang',134,'张俊') + +insert into tbl_computer values +('02','0','25555'), +('03','1','55555'), +('04','0','66666'), +('05','1','88888'), +('06','0','688878'), +('B01','0','558558') + +insert into tbl_record values +(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-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:25: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',20), +(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) +go + +select * from tbl_card +select * from tbl_computer +select * from tbl_record +go + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select cardId 卡号,userName 用户名,ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 +from tbl_record join tbl_card on tbl_record.cardId = tbl_card.id +where userName='张军' order by fee DESC + +--2. 查询出每台机器上的上网次数和消费的总金额 +select ComputerId 机器号,COUNT(ComputerId) 上网次数,SUM(fee) 消费的总金额 from tbl_record group by ComputerId + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardId 卡号,SUM(fee) 消费总金额 from tbl_record group by cardId + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select tbl_card.id 卡号,userName 用户名 from tbl_record right join tbl_card on tbl_record.cardId = tbl_card.id where tbl_record.id is null + +--5. 将密码与用户名一样的上网卡信息查询出来 +select a.* from tbl_card a,tbl_card b where a.passWord=b.passWord and a.userName=b.userName and a.id<>b.id + +--6. 查询出使用次数最多的机器号和使用次数 +select ComputerId 机器号,COUNT(ComputerId) 次数 from tbl_record group by ComputerId +having COUNT(ComputerId)=(select top 1 COUNT(ComputerId) from tbl_record group by ComputerId order by COUNT(ComputerId) DESC) + +--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 cardId 卡号,userName 用户名,ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 +from tbl_record join tbl_card on tbl_record.cardId = tbl_card.id +where DATENAME(DW,beginTime)='星期六' or DATENAME(DW,beginTime)='星期日' + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardId 卡号,userName 用户名,ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 +from tbl_record join tbl_card on tbl_record.cardId = tbl_card.id +where datediff(HH,BeginTime,Endtime)>12 + +--10. 查询出消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardId 卡号,userName 用户名,ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 +from tbl_record join tbl_card on tbl_record.cardId = tbl_card.id +where fee in (select top 3 fee from tbl_record order by fee DESC) +order by fee DESC + +--11. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardId 卡号,userName 用户名,ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 +from tbl_record join tbl_card on tbl_record.cardId = tbl_card.id +where fee not in (select top 3 fee from tbl_record order by fee DESC) +order by fee DESC + +select * from tbl_record order by fee DESC \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\267\346\231\250\346\227\255/2.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\267\346\231\250\346\227\255/2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ec43a13c143101fa0f491cb840a397dd57e56f59 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\256\267\346\231\250\346\227\255/2.sql" @@ -0,0 +1,109 @@ +use master +go +create database TBL +on +( + name='TBL', + filename='D:\SQL\TBL.mdf', + size=5MB, + maxsize=20MB, + filegrowth=10% +) +log on +( + name='TBL_log', + filename='D:\SQL\TBL_log.ldf', + size=5MB, + maxsize=20MB, + filegrowth=10% +) +go + +use TBL +go +create table TBL_Card +( + CardID char(30) primary key not null, + PassWord varchar(50) not null, + Balance money , + UserName nvarchar(20) +) + +create table TBL_Computer +( + CID char(30) primary key , + OnUse char(1) check(OnUse in(1,0)), + note text +) + +create table TBL_Record +( + RID int primary key , + CardID char(30) references TBL_Card(CardID), + CID char(30) references TBL_Computer(CID), + BeginTime datetime, + Endtime datetime, + fee money +) +go + +insert into TBL_Card values +('0023_ABC','555',98,'张军'), +('0025_bbd','abe',300,'朱俊'), +('0036_CCD','何柳',100,'何柳'), +('0045_YGR','00445_YGR',58,'证验'), +('0078_RJV','55885fg',600,'校庆'), +('0089_EDE','zhang',134,'张俊') + +insert into TBL_Computer values +('02','0','25555'), +('03','1','55555'), +('04','0','66666'), +('05','1','88888'), +('06','0','688878'), +('B01','0','558558') + +insert into TBL_Record values +(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-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:25:00',50), +(46,'0023_ABC','03','2006-12-23 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',20), +(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',3), +(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) +go + +select * from TBL_Card +select * from TBL_Computer +select * from TBL_Record +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select C.CardID 卡号 , UserName 用户名 , CID 机器编号 , BeginTime 开始时间 , Endtime 结束时间 , fee 消费金额 from TBL_Record R inner join TBL_Card C on R.CardID=C.CardID where UserName='张军' order by fee DESC +--2. 查询出每台机器上的上网次数和消费的总金额 +select CID 机器编号 , COUNT(*) 上网次数 , SUM(fee) 总金额 from TBL_Record R group by CID +--3. 查询出所有已经使用过的上网卡的消费总金额 +select CardID 卡号 , SUM(fee) 总金额 from TBL_Record group by CardID +--4. 查询出从未消费过的上网卡的卡号和用户名 +select C.CardID 卡号 , UserName 用户名 , RID from TBL_Record R right join TBL_Card C on R.CardID=C.CardID group by C.CardID,UserName,RID having RID is null or RID='' +--5. 将密码与用户名一样的上网卡信息查询出来 +select A.* from TBL_Card A,TBL_Card B where A.PassWord=B.UserName +--6. 查询出使用次数最多的机器号和使用次数 +--select top 1 COUNT(*) from TBL_Record group by CID order by COUNT(*) DESC +select CID 机器号 , COUNT(*) 使用次数 from TBL_Record group by CID having COUNT(*)=(select top 1 COUNT(*) from TBL_Record group by CID order by COUNT(*) DESC) +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select C.CardID 卡号 , UserName 用户名 , CID 机器编号 , fee 消费金额 from TBL_Record R inner join TBL_Card C on R.CardID=C.CardID where C.CardID like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select C.CardID 卡号 , UserName 用户名 , CID 机器编号 , BeginTime 开始时间 , Endtime 结束时间 , fee 消费金额 from TBL_Record R inner join TBL_Card C on R.CardID=C.CardID where datename(weekday,BeginTime)='星期六' or datepart(weekday,BeginTime)=1 +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select C.CardID 卡号 , UserName 用户名 , CID 机器编号 , BeginTime 开始时间 , Endtime 结束时间 , fee 消费金额 from TBL_Record R inner join TBL_Card C on R.CardID=C.CardID where DATEDIFF(HH,BeginTime,Endtime)>12 +--10. 查询出消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select C.CardID 卡号 , UserName 用户名 , CID 机器编号 , BeginTime 开始时间 , Endtime 结束时间 , fee 消费金额 from TBL_Record R +inner join TBL_Card C on R.CardID=C.CardID +where RID not in (select top 3 RID from TBL_Record order by fee DESC) +group by C.CardID,UserName,CID,BeginTime,Endtime,fee +--11. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 RID from TBL_Record order by fee DESC +select C.CardID 卡号 , UserName 用户名 , CID 机器编号 , BeginTime 开始时间 , Endtime 结束时间 , fee 消费金额 from TBL_Record R inner join TBL_Card C on R.CardID=C.CardID where RID in (select top 3 RID from TBL_Record order by fee DESC) +group by C.CardID , UserName , CID , BeginTime , Endtime , fee diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\346\231\266\345\251\267/.keep" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\346\231\266\345\251\267/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\346\231\266\345\251\267/\344\275\234\344\270\232\344\272\214\344\273\243\347\240\201.txt" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\346\231\266\345\251\267/\344\275\234\344\270\232\344\272\214\344\273\243\347\240\201.txt" new file mode 100644 index 0000000000000000000000000000000000000000..b585266ce3c7ac848deaacde6409773570dc3c9e --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\346\231\266\345\251\267/\344\275\234\344\270\232\344\272\214\344\273\243\347\240\201.txt" @@ -0,0 +1,144 @@ +use master +go + +create database SQL +on +( + name='SQL', + filename='D:\SQL.mdf', + size=5MB, + maxsize=5MB, + filegrowth=10% +) +log on +( + name='SQL_log', + filename='D:\SQL_log.ldf', + size=5MB, + maxsize=5MB, + filegrowth=10% +) +go + +use SQL +go + +--表一为上网卡信息表(tbl_card) +--id: 卡号,主键 +--passWord:密码 +--balance:卡上的余额 +--userName:该上网卡所属的用户名 +create table card +( + CardID varchar(20) primary key, + PassWord varchar(10), + Balance money, + UserName varchar(20) +) + +insert into card 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,'张峻') + +select * from card + +--表2为网吧机器说明表(tbl_computer) +--id:机器编号,主键 +--onUse:该机器是否正在使用,0为未使用,1为正在使用 +--note:该机器的说明 +create table computer +( + ComputerID varchar(10) primary key, + OnUse int, + Note text +) + +insert into computer values('02',0,'25555'), +('03',1,'55555'), +('04',0,'66666'), +('05',1,'88888'), +('06',0,'688878'), +('B01',0,'558558') + +select * from computer + +--表3为上网记录表(tbl_record): +--id:记录编号,主键 +--cardId:本次上网的卡号,外键 +--ComputerId:本次上网记录所使用的机器号,外键 +--beginTime:本次上网记录的开始时间 +--endTime:本次上网记录的结束时间 +--fee:本次上网的费用 +create table Record +( + ID int primary key, + CardID varchar(20) references card(CardID), + ComputerID varchar(10) references computer(ComputerID), + BeginTime datetime, + EndTime datetime, + Fee money +) + +insert into Record values(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-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 card +select * from computer +select * from Record + +--请完成以下题目: +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select card.CardID 卡号,UserName 用户名,ComputerID 机器编号,BeginTime 开始时间,EndTime 结束时间,Fee 消费金额 from Record +inner join card on card.CardID=Record.CardID where UserName='张军' order by Fee desc + +--2. 查询出每台机器上的 上网次数 和消费的总金额 +select computer.ComputerID 机器编号,count(Record.ComputerID)上网次数,sum(Fee )消费的总金额 from computer +inner join Record on computer.ComputerID=Record.ComputerID group by computer.ComputerID + +--3. 查询出 所有已经使用过的 上网卡的消费总金额 +select computer.ComputerID,sum(Fee )消费的总金额 from computer +left join Record on computer.ComputerID=Record.ComputerID where OnUse=1 group by computer.ComputerID + +--4. 查询出 从未消费过的上网卡的 卡号和 用户名 +select card.CardID 卡号,UserName 用户名 from card +left join Record on card.CardID=Record.CardID +where fee is null + +--5. 将 密码与用户名一样的 上网卡信息查询出来 +select a.* from card a ,card b where a.UserName=b.PassWord + +--6. 查询出 使用次数最多的机器号 和使用次数 +select ComputerID 机械号,count(ComputerID)使用次数 from Record group by ComputerID +having count(ComputerID) = (select top 1 count(ComputerID)使用次数 from Record group by ComputerID order by COUNT(ComputerID) desc) + +--7. 查询出卡号是以 'ABC'结尾的卡号, 用户名, 上网的机器号和 消费金额 +select Record.CardID 卡号,UserName 用户名 ,ComputerID 机械号,Fee 消费金额 from card +inner join Record on card.CardID=Record.CardID +where Record.CardID like '%ABC' + +--8. 查询出是 周六、周天上网的记录, 要求显示上网的卡号, 用户名, 机器号, 开始时间、 结束时间和 消费金额 +select Record.CardID 卡号,UserName 用户名 ,ComputerID 机器号,BeginTime 开始时间,EndTime 结束时间,Fee 消费金额 from card +inner join Record on card.CardID=Record.CardID +where datename(DW,BeginTime)='星期日' or DATEPART(DW,BeginTime)='7' + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select Record.CardID 卡号,UserName 用户名 ,ComputerID 机器号,BeginTime 开始时间,EndTime 结束时间,Fee 消费金额 +from Record join Card on Record.cardId = Card.CardID +where datediff(HH,BeginTime,Endtime)>12 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select Card.cardId 卡号,userName 用户名,ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 +from Record join Card on Record.cardId =Card.CardID +where fee in (select top 3 fee from Record order by fee DESC) +order by fee DESC \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\347\204\246\346\230\216\345\220\233/3.31zuoye2.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\347\204\246\346\230\216\345\220\233/3.31zuoye2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0827f8ca92e6e5895b3e5a4f51242ac44eb931d4 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\347\204\246\346\230\216\345\220\233/3.31zuoye2.sql" @@ -0,0 +1,118 @@ +use master +go +create database wb +on +( + name ='wb.mdf', + filename='D:\wb.mdf', + size=5, + maxsize=10, + filegrowth=15% +) +log on +( + name ='wb_log.ldf', + filename='D:\wb_log.ldf', + size=5, + maxsize=10, + filegrowth=15% +) +go +use wb +go +create table tbl_card +( + id nvarchar(20) primary key, + passWord nvarchar(20) not null, + balance money, + userName nvarchar(10) +) +create table tbl_computer +( + id nvarchar(20) primary key, + onUse nvarchar(1) check((onUse) in ('0','1')), + note text +) +create table tbl_record +( + id nvarchar(20) primary key, + cardId nvarchar(20) references tbl_card(id), + ComputerId nvarchar(20) 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','张峻') +go +insert into tbl_computer values +('02', '0', '25555'), +('03', '1', '55555'), +('04', '0', '66666'), +('05', '1', '88888'), +('06', '0', '688878'), +('B01', '0', '558558') +go + +insert into tbl_record values +('23', '0078_RJV', 'B01', '2007-07-15 19:00:00', '2007-07-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', 30), +('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) +go +select * from tbl_card --上网卡信息表(tbl_card) +select * from tbl_computer--网吧机器说明表(tbl_computer) +select * from tbl_record --上网记录表(tbl_record) +--1. 查询出用户名为'张军'的上网卡的上网记录, +--要求显示卡费金额,号,用户名,机器编号、开始时间、结束时间,和消并按消费金额降序排列 +select tbl_card.id 卡号,tbl_card.userName 用户名,tbl_record.ComputerId 机器编号, +tbl_record.beginTime 开始时间,tbl_record.endTime 结束时间 ,tbl_record.fee 消费金额 + from tbl_record inner join tbl_card on +tbl_record.cardId=tbl_card.id where tbl_card.userName='张军' order by tbl_record.fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 +select tbl_computer.id 每台机器,count(*) 上网次数,sum(fee) 消费的总金额 from tbl_record inner join tbl_computer on +tbl_computer.id=tbl_record.ComputerId group by tbl_computer.id +--3. 查询出所有已经使用过的上网卡的消费总金额 +select tbl_record.cardId 上网卡, sum(fee) 消费总金额 from tbl_record inner join tbl_card on +tbl_record.cardId=tbl_card.id where tbl_record.fee is not null group by tbl_record.cardId +--4. 查询出从未消费过的上网卡的卡号和用户名 +select tbl_record.cardId 上网卡, tbl_card.id 卡号,tbl_card.userName 用户名 from tbl_record right join tbl_card on +tbl_record.cardId=tbl_card.id where tbl_record.fee is null group by tbl_record.cardId , tbl_card.id ,tbl_card.userName +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from tbl_card where passWord =userName +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 count(id)使用次数, ComputerId 机器号 from tbl_record + group by ComputerId order by count(id) desc +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select tbl_card.id 卡号,tbl_card.userName 用户名,tbl_record.ComputerId 机器编号,tbl_record.fee 消费金额 + from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id where tbl_card.id like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tbl_card.id 卡号,tbl_card.userName 用户名,tbl_record.ComputerId 机器编号, +tbl_record.beginTime 开始时间,tbl_record.endTime 结束时间, +tbl_record.fee 消费金额 + from tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id + where datename(DW,beginTime)='星期六' or datename(DW,beginTime)='星期日' +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号, +--开始时间、结束时间和消费金额 +select tbl_card.id 卡号,tbl_card.userName 用户名,tbl_record.ComputerId 机器编号, +tbl_record.beginTime 开始时间,tbl_record.endTime 结束时间, +tbl_record.fee 消费金额 from +tbl_record full join tbl_card on tbl_record.cardId=tbl_card.id + where datediff(HOUR,beginTime,endTime)>12 +--10. 查询出消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号, +--开始时间、结束时间和消费金额 +select top 3 tbl_card.id 卡号,tbl_card.userName 用户名,tbl_record.ComputerId 机器编号, +tbl_record.beginTime 开始时间,tbl_record.endTime 结束时间, +tbl_record.fee 消费金额 from +tbl_record inner join tbl_card on tbl_record.cardId=tbl_card.id +order by tbl_record.fee desc + + + diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\345\206\240\346\235\260/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\345\206\240\346\235\260/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a52095a0fc8ad9bb3e73f8414f25387873b71b70 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\345\206\240\346\235\260/SQLQuery2.sql" @@ -0,0 +1,105 @@ +use master +go + +create database bmp +go + +use bmp +go + +create table tbl_card +( + ID nvarchar(30) primary key , + passWord nvarchar(10) not null, + balance int , + userName nvarchar(5) not null +) + +create table tbl_computer +( + ID nvarchar(30) primary key , + OnUse nvarchar(2) check(OnUse=0 or OnUse=1), + Note int +) + +create table tbl_record +( + ID char(5) primary key , + CardID nvarchar(30) references tbl_card(ID), + ComputerId nvarchar(30) references tbl_computer(ID), + BeginTime datetime , + EndTime datetime, + Fee int +) + +insert into tbl_card 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 values +('02',0,25555), +('03',1,55555), +('04',0,66666), +('05',1,88888), +('06',0,688878), +('B01',0,558558) + +insert into tbl_record values +('23','0078_RJV','B01','2007-07-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',3), +('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 c.ID,userName,ComputerId,BeginTime,EndTime,Fee from tbl_card c inner join tbl_record r on c.ID=r.CardID where userName='张军' order by Fee + +--2. 查询出每台机器上的上网次数和消费的总金额 +select CardID,count(*) 上网次数,sum(Fee) 消费总金额 from tbl_record group by CardID + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select sum(fee) from tbl_record + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select c.ID,userName from tbl_card c left join tbl_record r on c.ID=r.CardID where Fee is null + +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from tbl_card c1 join tbl_card c2 on c1.passWord=c2.userName + +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 CardID,count(*)使用次数 from tbl_record group by CardID order by count(*) desc + +--7. 查询出卡号是以'ABC'结尾的 卡号,用户名,上网的机器号和消费金额 +select c.id,userName,ComputerId,Fee from tbl_card c inner join tbl_record r on c.ID=r.CardID where c.ID like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select c.ID 卡号,userName 用户名,o.ID 机器号,BeginTime 开始时间,EndTime 结束时间,Fee 消费金额 from tbl_record r inner join tbl_card c on r.CardID=c.ID +inner join tbl_computer o on r.ComputerId=o.ID where datename(dw,BeginTime)='星期日' or datename(dw,BeginTime)='7' + +select * from tbl_card +select * from tbl_computer +select * from tbl_record +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + select c.ID 卡号,userName 用户名,o.ID 机器号,BeginTime 开始时间,EndTime 结束时间,Fee 消费金额 from tbl_record r inner join tbl_card c on r.CardID=c.ID +inner join tbl_computer o on r.ComputerId=o.ID where datediff(HH,BeginTime,EndTime)>12 + +--10. 查询出 消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 c.ID 卡号,userName 用户名,ComputerId 机器号,BeginTime 开始时间,EndTime 结束时间 from tbl_card c inner join tbl_record r on c.ID=r.CardID order by Fee desc + +--10. 查询除 消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select * from tbl_record where id not in (select top 3 id from tbl_record order by fee desc) diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\350\277\234\346\226\271/1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\350\277\234\346\226\271/1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..30f27b157324ed5f81d7a3d71eabbc8668edd145 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\347\216\213\350\277\234\346\226\271/1.sql" @@ -0,0 +1,100 @@ +use master +go +create database bmp +on +( + name='bmp', + filename='D:\bmp.mdf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +log on +( + name='bmp_log', + filename='D:\bmp_log.ldf', + size=5mb, + maxsize=50mb, + filegrowth=10% +) +use bmp +go +create table tbl_card +( + id varchar(20) primary key not null, + passWord varchar(20) not null, + balance int not null, + userName varchar(20) not null +) +create table tbl_computer +( + id varchar(20) primary key not null, + onUse char(1) check(onUse=0 or onUse=1) not null, + note int not null, +) +create table tbl_record +( + id int primary key , + cardld varchar(20) references tbl_card(id), + ComputerId varchar(20) references tbl_computer(id), + beginTime datetime not null, + endTime datetime not null, + fee money not null +) +insert into tbl_card values +('0023_ABC','555',98,'张军'), +('0025_bbd','ade',300,'朱俊'), +('0036_CCD','何柳',100,'何柳'), +('0045_YGR','0045_YGR',58,'证验'), +('0078_RJV','55885fg',600,'校庆'), +('0089_EDE','zhang',134,'张峻') +insert into tbl_computer values +('02',0,25555), +('03',1,55555), +('04',0,66666), +('05',1,88888), +('06',0,688878), +('B01',0,558558) +insert into tbl_record values +(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-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 CD.id 卡号,userName 用户名,CR.id 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 +from tbl_card CD inner join tbl_record RD on CD.id = RD.cardld +inner join tbl_computer CR on RD.ComputerId=CR.id where userName='张军' order by fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 +select ComputerId 机器编号,COUNT(*) 上网次数,SUM(fee) 消费总金额 from tbl_record group by ComputerId +--3. 查询出所有已经使用过的上网卡的消费总金额 +select RD.ComputerId 网卡编号, CR.onUse 已经使用,sum(fee) 消费总金额 +from tbl_record RD right join tbl_computer CR on RD.ComputerId=CR.id +where CR.onUse='1' group by RD.ComputerId,CR.onUse +--4. 查询出从未消费过的上网卡的卡号和用户名 +select c.ID,userName from tbl_card c left join tbl_record r on c.ID=r.cardld where Fee is null +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from tbl_card t1 join tbl_card t2 on (t1.id = t2.id) and (t1.[passWord] = t2.userName) +--6. 查询出使用次数最多的机器号和使用次数 +select ComputerId, count(*) '使用次数' from tbl_record group by ComputerId +having count(*) = (select max(t.使用次数) from (select ComputerId, count(*) '使用次数' from tbl_record group by ComputerId) t) +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select tc.id, tc.userName, tr.ComputerId, tr.fee from tbl_record tr join tbl_card tc on tr.cardld = tc.id where tr.cardld like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tc.id, tc.userName, tr.ComputerId, tr.beginTime, tr.endTime, tr.fee from tbl_record tr join tbl_card tc on tr.cardld = tc.id +where DATENAME(dw, tr.beginTime) in ('星期六','星期日') +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select c.ID 卡号,userName 用户名,o.ID 机器号,BeginTime 开始时间,EndTime 结束时间,Fee 消费金额 from tbl_record r inner join tbl_card c on r.cardld=c.ID +inner join tbl_computer o on r.ComputerId=o.ID where datediff(HH,BeginTime,EndTime)>12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 c.ID 卡号,userName 用户名,ComputerId 机器号,BeginTime 开始时间,EndTime 结束时间,fee 消费金额 +from tbl_card c inner join tbl_record r on c.ID=r.cardld order by Fee desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\347\250\213\346\226\207\345\220\233/.keep" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\347\250\213\346\226\207\345\220\233/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\347\250\213\346\226\207\345\220\233/SQLQusry2.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\347\250\213\346\226\207\345\220\233/SQLQusry2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8b5ce95dc320c0a2b194aa97205f04275cebe5d4 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\347\250\213\346\226\207\345\220\233/SQLQusry2.sql" @@ -0,0 +1,85 @@ +use master +go + +create database TBL +go + +use TBL +go + +create table Card +( + ID nvarchar(20) primary key , + PassWord nvarchar(10), + Balance int , + UserName nvarchar(10) +) + +create table Computer +( + ID nvarchar(20) primary key, + OnUse char(1) check(OnUse in (1,0)), + note text , +) + +create table Record +( + ID int primary key , + Cardld nvarchar(20) references Card(ID), + ComputerID nvarchar(20) references Computer(ID), + BeginTime datetime, + Endtime datetime, + Fee int +) + +insert into Card values +('0023_ABC','555',98,'寮犲啗'),('0025_bbd','abe',300,'鏈变繆'),('036__CCD','浣曟煶',100,'浣曟煶'), +('0045_YGR','0045_YGR',58,'璇侀獙'),('0078_RJV','55885fg',600,'鏍″簡'),('0089','zhang',134,'寮犲郴') + + +insert into Computer values +('02','0','25555'),('03','1','55555'),('04','0','66666'),('05','1','88888'), +('06','0','688878'),('B01','0','558558') + +insert into Record values +(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-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:0',6), +(47,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50), +(48,'0023_ABC','03','2007-1-06 15:26:00','2007-1-06 22:55:0',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',3), +(65,'0025_bbd','02','2006-12-28 18:00:00','006-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 Card +select * from Computer +select * from Record +--1. 鏌ヨ鍑虹敤鎴峰悕涓'寮犲啗'鐨勪笂缃戝崱鐨勪笂缃戣褰曪紝瑕佹眰鏄剧ず鍗″彿锛岀敤鎴峰悕锛屾満鍣ㄧ紪鍙枫佸紑濮嬫椂闂淬佺粨鏉熸椂闂达紝鍜屾秷璐归噾棰濓紝骞舵寜娑堣垂閲戦闄嶅簭鎺掑垪 + Select R.Cardld 鍗″彿,C.UserName 鐢ㄦ埛鍚,R.ComputerID 鏈哄櫒缂栧彿,R.BeginTime 寮濮嬫椂闂,R.Endtime 缁撴潫鏃堕棿,R.Fee 娑堣垂閲戦 from Record R + inner join Card C on R.Cardld=C.ID where UserName='寮犲啗' order by Fee desc +--2. 鏌ヨ鍑烘瘡鍙版満鍣ㄤ笂鐨勪笂缃戞鏁板拰娑堣垂鐨勬婚噾棰 + Select ComputerID,count(ComputerID) 涓婄綉娆℃暟,sum(Fee) 鎬婚噾棰 from Record group by ComputerID +--3. 鏌ヨ鍑烘墍鏈夊凡缁忎娇鐢ㄨ繃鐨勪笂缃戝崱鐨勬秷璐规婚噾棰 + Select Cardld 鍗″彿,Sum(Fee) 娑堣垂鎬婚噾棰 from Record group by Cardld +--4. 鏌ヨ鍑轰粠鏈秷璐硅繃鐨勪笂缃戝崱鐨勫崱鍙峰拰鐢ㄦ埛鍚 + Select C.ID 鍗″彿,C.UserName 鐢ㄦ埛鍚 from Record R right join Card C on R.Cardld=C.ID where fee is null or Fee='' + +--5. 灏嗗瘑鐮佷笌鐢ㄦ埛鍚嶄竴鏍风殑涓婄綉鍗′俊鎭煡璇㈠嚭鏉 + Select A.* from Card A,Card B where A.ID=B.PassWord +--6. 鏌ヨ鍑轰娇鐢ㄦ鏁版渶澶氱殑鏈哄櫒鍙峰拰浣跨敤娆℃暟 + Select C.ID 鏈哄櫒鍙,count(Cardld) 浣跨敤娆℃暟 from Record R inner join Computer C on R.ComputerID=C.ID group by C.ID + having C.ID=(Select top 1 ComputerID from Record group by ComputerID order by count(Cardld) desc ) +--7. 鏌ヨ鍑哄崱鍙锋槸浠'ABC'缁撳熬鐨勭敤鎴峰崱鍙凤紝鍚嶏紝涓婄綉鐨勬満鍣ㄥ彿鍜屾秷璐归噾棰 + Select C.ID 鐢ㄦ埛鍗″彿,C.UserName 鐢ㄦ埛鍚,R.ComputerID 鏈哄櫒鍙, sum(Fee) 娑堣垂鎬婚噾棰 from Card C inner join Record R on C.ID=R.Cardld where C.ID like '%ABC' group by C.ID,C.UserName,R.ComputerID +--8. 鏌ヨ鍑烘槸鍛ㄥ叚銆佸懆澶╀笂缃戠殑璁板綍锛岃姹傛樉绀轰笂缃戠殑鍗″彿锛岀敤鎴峰悕锛屾満鍣ㄥ彿锛屽紑濮嬫椂闂淬佺粨鏉熸椂闂村拰娑堣垂閲戦 + Select C.ID 鍗″彿,C.UserName 鐢ㄦ埛鍚,R.ComputerID 鏈哄櫒鍙,R.BeginTime 寮濮嬫椂闂,R.Endtime 缁撴潫鏃堕棿,fee 娑堣垂閲戦 from Record R + inner join Card C on R.Cardld=C.ID where datename(DW,R.BeginTime)='鏄熸湡鍏' or datename(DW,R.BeginTime)='鏄熸湡鏃' +--9. 鏌ヨ鎴愪竴娆′笂缃戞椂闂磋秴杩12灏忔椂鐨勭殑涓婄綉璁板綍锛岃姹傛樉绀轰笂缃戠殑鍗″彿锛岀敤鎴峰悕锛屾満鍣ㄥ彿锛屽紑濮嬫椂闂淬佺粨鏉熸椂闂村拰娑堣垂閲戦 + select R.Cardld 鍗″彿,C.UserName 鐢ㄦ埛鍚,R.ComputerID 鏈哄櫒鍙, R.BeginTime 寮濮嬫椂闂,R.Endtime 缁撴潫鏃堕棿,fee 娑堣垂閲戦 from Record R + inner join Card C on R.Cardld=C.ID where datediff(hh,BeginTime,EndTime)>12 + --10. 鏌ヨ闄ゆ秷璐归噾棰濇帓鍒楀墠涓夊悕(鏈楂)鐨勪笂缃戣褰曪紝瑕佹眰鏄剧ず涓婄綉鐨勫崱鍙凤紝鐢ㄦ埛鍚嶏紝鏈哄櫒鍙凤紝寮濮嬫椂闂淬佺粨鏉熸椂闂村拰娑堣垂閲戦 + Select top 3 R.Cardld 鍗″彿,C.UserName 鐢ㄦ埛鍚,R.ComputerID 鏈哄櫒鍙, R.BeginTime 寮濮嬫椂闂, R.Endtime 缁撴潫鏃堕棿, fee 娑堣垂閲戦 from Record R + inner join Card C on R.Cardld=C.ID order by Fee desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\347\273\203\346\226\207\346\265\251/SQLQuery5.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\347\273\203\346\226\207\346\265\251/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..679639ff97bb7d9fc60ee448547e96d6e36f8d52 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\347\273\203\346\226\207\346\265\251/SQLQuery5.sql" @@ -0,0 +1,103 @@ +use master +go + +create database TBL +on +( +name='TBL', +filename='D:\TBL.mdf', +size=5MB, +MAXsize=50MB, +filegrowth=50% +) + +log on +( +name='TBL_log', +filename='D:\TBL_log.ldf', +size=5MB, +maxsize=50MB, +filegrowth=50% +) +go + +use TBL +go + +create table Card +( + ID nvarchar(20) primary key , + PassWord nvarchar(10), + Balance int , + UserName nvarchar(10) +) + +create table Computer +( + ID nvarchar(20) primary key, + OnUse char(1) check(OnUse in (1,0)), + note text , +) + +create table Record +( + ID int primary key , + Cardld nvarchar(20) references Card(ID), + ComputerID nvarchar(20) references Computer(ID), + BeginTime datetime, + Endtime datetime, + Fee int +) + +insert into Card values +('0023_ABC','555',98,'张军'),('0025_bbd','abe',300,'朱俊'),('036__CCD','何柳',100,'何柳'), +('0045_YGR','0045_YGR',58,'证验'),('0078_RJV','55885fg',600,'校庆'),('0089','zhang',134,'张峻') + + +insert into Computer values +('02','0','25555'),('03','1','55555'),('04','0','66666'),('05','1','88888'), +('06','0','688878'),('B01','0','558558') + +insert into Record values +(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-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:0',6), +(47,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50), +(48,'0023_ABC','03','2007-1-06 15:26:00','2007-1-06 22:55:0',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',3), +(65,'0025_bbd','02','2006-12-28 18:00:00','006-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 Card +select * from Computer +select * from Record +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 + Select R.Cardld 卡号,C.UserName 用户名,R.ComputerID 机器编号,R.BeginTime 开始时间,R.Endtime 结束时间,R.Fee 消费金额 from Record R + inner join Card C on R.Cardld=C.ID where UserName='张军' order by Fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 + Select ComputerID,count(ComputerID) 上网次数,sum(Fee) 总金额 from Record group by ComputerID +--3. 查询出所有已经使用过的上网卡的消费总金额 + Select Cardld 卡号,Sum(Fee) 消费总金额 from Record group by Cardld +--4. 查询出从未消费过的上网卡的卡号和用户名 + Select C.ID 卡号,C.UserName 用户名 from Record R right join Card C on R.Cardld=C.ID where fee is null or Fee='' + +--5. 将密码与用户名一样的上网卡信息查询出来 + Select A.* from Card A,Card B where A.ID=B.PassWord +--6. 查询出使用次数最多的机器号和使用次数 + Select C.ID 机器号,count(Cardld) 使用次数 from Record R inner join Computer C on R.ComputerID=C.ID group by C.ID + having C.ID=(Select top 1 ComputerID from Record group by ComputerID order by count(Cardld) desc ) +--7. 查询出卡号是以'ABC'结尾的用户卡号,名,上网的机器号和消费金额 + Select C.ID 用户卡号,C.UserName 用户名,R.ComputerID 机器号, sum(Fee) 消费总金额 from Card C inner join Record R on C.ID=R.Cardld where C.ID like '%ABC' group by C.ID,C.UserName,R.ComputerID +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + Select C.ID 卡号,C.UserName 用户名,R.ComputerID 机器号,R.BeginTime 开始时间,R.Endtime 结束时间,fee 消费金额 from Record R + inner join Card C on R.Cardld=C.ID where datename(DW,R.BeginTime)='星期六' or datename(DW,R.BeginTime)='星期日' +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + select R.Cardld 卡号,C.UserName 用户名,R.ComputerID 机器号, R.BeginTime 开始时间,R.Endtime 结束时间,fee 消费金额 from Record R + inner join Card C on R.Cardld=C.ID where datediff(hh,BeginTime,EndTime)>12 + --10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + Select top 3 R.Cardld 卡号,C.UserName 用户名,R.ComputerID 机器号, R.BeginTime 开始时间, R.Endtime 结束时间, fee 消费金额 from Record R + inner join Card C on R.Cardld=C.ID order by Fee desc + diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\347\277\201\346\231\266\350\276\211/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\347\277\201\346\231\266\350\276\211/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a52095a0fc8ad9bb3e73f8414f25387873b71b70 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\347\277\201\346\231\266\350\276\211/SQLQuery2.sql" @@ -0,0 +1,105 @@ +use master +go + +create database bmp +go + +use bmp +go + +create table tbl_card +( + ID nvarchar(30) primary key , + passWord nvarchar(10) not null, + balance int , + userName nvarchar(5) not null +) + +create table tbl_computer +( + ID nvarchar(30) primary key , + OnUse nvarchar(2) check(OnUse=0 or OnUse=1), + Note int +) + +create table tbl_record +( + ID char(5) primary key , + CardID nvarchar(30) references tbl_card(ID), + ComputerId nvarchar(30) references tbl_computer(ID), + BeginTime datetime , + EndTime datetime, + Fee int +) + +insert into tbl_card 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 values +('02',0,25555), +('03',1,55555), +('04',0,66666), +('05',1,88888), +('06',0,688878), +('B01',0,558558) + +insert into tbl_record values +('23','0078_RJV','B01','2007-07-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',3), +('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 c.ID,userName,ComputerId,BeginTime,EndTime,Fee from tbl_card c inner join tbl_record r on c.ID=r.CardID where userName='张军' order by Fee + +--2. 查询出每台机器上的上网次数和消费的总金额 +select CardID,count(*) 上网次数,sum(Fee) 消费总金额 from tbl_record group by CardID + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select sum(fee) from tbl_record + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select c.ID,userName from tbl_card c left join tbl_record r on c.ID=r.CardID where Fee is null + +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from tbl_card c1 join tbl_card c2 on c1.passWord=c2.userName + +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 CardID,count(*)使用次数 from tbl_record group by CardID order by count(*) desc + +--7. 查询出卡号是以'ABC'结尾的 卡号,用户名,上网的机器号和消费金额 +select c.id,userName,ComputerId,Fee from tbl_card c inner join tbl_record r on c.ID=r.CardID where c.ID like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select c.ID 卡号,userName 用户名,o.ID 机器号,BeginTime 开始时间,EndTime 结束时间,Fee 消费金额 from tbl_record r inner join tbl_card c on r.CardID=c.ID +inner join tbl_computer o on r.ComputerId=o.ID where datename(dw,BeginTime)='星期日' or datename(dw,BeginTime)='7' + +select * from tbl_card +select * from tbl_computer +select * from tbl_record +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + select c.ID 卡号,userName 用户名,o.ID 机器号,BeginTime 开始时间,EndTime 结束时间,Fee 消费金额 from tbl_record r inner join tbl_card c on r.CardID=c.ID +inner join tbl_computer o on r.ComputerId=o.ID where datediff(HH,BeginTime,EndTime)>12 + +--10. 查询出 消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 c.ID 卡号,userName 用户名,ComputerId 机器号,BeginTime 开始时间,EndTime 结束时间 from tbl_card c inner join tbl_record r on c.ID=r.CardID order by Fee desc + +--10. 查询除 消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select * from tbl_record where id not in (select top 3 id from tbl_record order by fee desc) diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\202\226\346\265\267\346\231\237/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\202\226\346\265\267\346\231\237/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e6a0f8b5238ba028853598c676d8b7b0c80508a7 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\202\226\346\265\267\346\231\237/SQLQuery2.sql" @@ -0,0 +1,125 @@ +use master +go +create database internetbar +on +( + name = 'internetbar', + filename='D:\sql\internetbar.mdf', + size = 5MB, + maxsize = 500MB, + filegrowth = 5MB +) +log on +( + name = 'internetbar_log', + filename='D:\sql\internetbar_log.ldf', + size = 5MB, + maxsize = 500MB, + filegrowth = 5MB +) +go +use internetbar +go + +create table tbl_card +( + cardID varchar(20) primary key not null, + passWord nvarchar(20) not null, + balance nvarchar(20) not null, + userName nvarchar(20) not null, +) +--卡号 +--密码 +--卡上余额 +--用户名 + +create table tbl_computer +( + computerID varchar(20) primary key not null, + onUse char(1) default(0) check(onUse=1 or onUse=0) not null, + note nvarchar(50) not null +) +--机器编号 +--机器使用情况 +--说明 + +create table tbl_record +( + recordID varchar(20) primary key not null, + cardID varchar(20) references tbl_card(cardID), + computerID varchar(20) references tbl_computer(computerID), + beginTime datetime, + endTime datetime, + fee int +) +--记录编号 +--卡号 +--机器编号 +--开始时间 +--结束时间 +--费用 + +insert into tbl_card values +('0023_ABC','555',98,'张军'), +('0025_bbd','abe',300,'朱俊'), +('0036_CCD','何柳',100,'何柳'), +('0045_YGR','0045_YGR',58,'证验'), +('0078_RJV','5585fg',600,'校庆'), +('0089_EDE','zhang',134,'张俊') + +insert into tbl_computer values +('02',0,25555), +('03',1,55555), +('04',0,66666), +('05',1,88888), +('06',0,688878), +('B01',0,558558) + +insert into tbl_record values +('23','0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00',20), +('34','0025_bbd','02','2006-12-25 16: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-23 15:26:00','2006-12-23 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',3), +('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 C.cardID 卡号, userName 用户名, computerID 机器编号, beginTime 开始时间, endTime 结束时间,fee 消费金额 from +tbl_record R inner join tbl_card C on R.cardID=C.cardID +where userName='张军' order by fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 +select computerID 机器编号, count(*) 机器上网次数, sum(fee) 总金额 from tbl_record group by computerID +--3. 查询出所有已经使用过的 上网卡的 消费总金额 +select sum(fee) 消费总金额 from tbl_record +--4. 查询出从未消费过的上网卡的卡号和用户名 +select C.cardID 卡号, userName 用户名 from +tbl_card C left join tbl_record R on C.cardID=R.cardID where fee is null or fee=' ' +--5. 将密码与用户名一样的上网卡信息查询出来 +select distinct A. * from +tbl_card A , tbl_card B where A.passWord=B.userName +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 computerID 机器编号, count(*) 使用次数 from +tbl_record group by computerID order by count(*) desc +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select R.cardID 卡号, userName 用户名, computerID 机器编号, fee 消费金额 from +tbl_record R inner join tbl_card C on R.cardID=C.cardID where R.cardID like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select R.cardID 卡号, userName 用户名, computerID 机器编号,beginTime 开始时间,endTime 结束时间, fee 消费金额 from +tbl_record R inner join tbl_card C on R.cardID=C.cardID +where datepart(DW,beginTime)=7 or DATENAME(DW,beginTime)='星期日' +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select R.cardID 卡号, userName 用户名, computerID 机器编号,beginTime 开始时间,endTime 结束时间, fee 消费金额 from +tbl_record R inner join tbl_card C on R.cardID=C.cardID +where (endtime-beginTime)>12 +--10. 查询出消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 R.cardID 卡号, userName 用户名, computerID 机器编号,beginTime 开始时间,endTime 结束时间, fee 消费金额 from +tbl_record R inner join tbl_card C on R.cardID=C.cardID order by fee desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\346\264\245\345\215\216/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\346\264\245\345\215\216/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0e334acc65d1ab753c08d9a2a5deb1255a5b85e3 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\346\264\245\345\215\216/SQLQuery3.sql" @@ -0,0 +1,82 @@ +use master +go +create database Computer +on +( + name='Computer ', + filename='D:\SQLwork\Computer.mdf', + Size=5MB, + maxsize=50MB, + filegrowth=10% + ) + log on +( +name='Computer _log', + filename='D:\SQLwork\Computer_log.ldf', + Size=5MB, + maxsize=50MB, + filegrowth=10% +) +go +use Computer +go +create table tbl_card +( +cardId nvarchar(10) primary key , +passWord nvarchar(10) , +balance money , +userName nvarchar(10), +) +create table tbl_computer +( +computerId nvarchar(10) primary key , +onUse int check(onUse=0 or onUse =1), +note text, +) +create table tbl_record +( +recordId int primary key , +cardId nvarchar(10) references tbl_card(cardId), +computerId nvarchar(10) references tbl_computer(computerId), +beginTime datetime, +endTime datetime, +fee money, +) +select * from tbl_card +select * from tbl_computer +select * from tbl_record +insert into tbl_card values ('0023_ABC','555',98,'张军'),('0025_bbd','abe',300,'朱俊'),('0036_CDD','何柳',100,'何柳'),('0045_YGR','0045_YGR',58,'证验'),('0078_RJV','55885fg',600,'校庆'),('0089_EDE','zhang',134,'张峻') +insert into tbl_computer values ('02',0,'25555'),('03',1,'55555'),('04',0,'66666'),('05',1,'88888'),('06',0,'688878'),('B01',0,'558558') +insert into tbl_record values (23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-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-22 15:26:00','2006-12-22 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',3), +(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) +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select R.cardId 显示卡号,userName 用户名,computerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_record R inner join tbl_card C on R.cardId=C.cardId where userName='张军' order by fee desc +--2. 查询出 每台机器上的上网次数和消费的总金额 +select computerId,count (*) 上网次数, sum(fee) 消费的总金额 from tbl_record group by computerId +--3. 查询出所有已经使用过的上网卡的消费总金额 +select P.computerId , sum(fee)消费总金额 from tbl_computer P left join tbl_record R on P.computerId=R.computerId where onUse=1 GROUP BY P.computerId +--4. 查询出从未消费过的上网卡的卡号和用户名 +select R.cardId 卡号,T.userName 用户名 from tbl_record R right join tbl_computer C on R.computerId=C.computerId RIGHT join tbl_card T on R.cardId=T.cardId WHERE FEE IS NULL +--5. 将密码与用户名一样的上网卡信息查询出来 +select A.* from tbl_card A,tbl_card B where A.passWord =B.passWord AND A.userName=B.userName +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 computerId ,sum (recordId)使用次数 from tbl_record group by computerId order by sum (recordId) desc +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select distinct T.userName 用户名,R.computerId 上网的机器号,R.fee 消费金额 from tbl_record R inner join tbl_card T on R.cardId=T.cardId where T.cardId like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select T.cardId 上网的卡号,T.userName 用户名,R.computerId 机器号,R.beginTime 开始时间,R.endTime 结束时间,R.fee 消费金额 from tbl_record R inner join tbl_card T on R.cardId=T.cardId WHERE DATENAME(DW,beginTime )='星期日' or DATEPART(DW,beginTime)=7 +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select R.cardID 卡号, userName 用户名, computerID 机器编号,beginTime 开始时间,endTime 结束时间, fee 消费金额 from +tbl_record R inner join tbl_card C on R.cardID=C.cardID +where (endtime-beginTime)>12 +--10. 查询出消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额>12 +select top 3 R.cardID 卡号, userName 用户名, computerID 机器编号,beginTime 开始时间,endTime 结束时间, fee 消费金额 from +tbl_record R inner join tbl_card C on R.cardID=C.cardID order by fee desc diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\213\217\346\235\234\345\206\233/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\213\217\346\235\234\345\206\233/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d0376e1263f8ff112b13c8d4be25cbfdd79283e5 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\213\217\346\235\234\345\206\233/SQLQuery1.sql" @@ -0,0 +1,134 @@ +use master +go +create database tbl +on +( + name='tbl', + filename='D:\SQL\tbl.mdf', + size=5MB, + maxsize=20MB, + filegrowth=10% +) +log on +( + name='tbl_log', + filename='D:\SQL\tbl_log.ldf', + size=5MB, + maxsize=20MB, + filegrowth=10% +) +go + +use tbl +go + +create table tbl_card +( +id varchar(10) primary key, +passWord varchar(10), +balance varchar(5), +userName nvarchar(2) +) +go + +create table tbl_computer +( +id varchar(5) primary key, +onUse varchar(1) check(onUse=0 or onUse=1), +note varchar(10) +) +go + +create table tbl_record +( +id varchar(2) primary key, +cardId varchar(10) references tbl_card(id), +ComputerId varchar(5) references tbl_computer(id), +beginTime datetime, +endTime datetime, +fee money +) +go + +insert into tbl_card values +('0023_ABC','555',98,'张军'), +('0025_bbd','abe',300,'朱俊'), +('0036_CCD','何柳',100,'何柳'), +('0045_YGR','00445_YGR',58,'证验'), +('0078_RJV','55885fg',600,'校庆'), +('0089_EDE','zhang',134,'张俊') + +insert into tbl_computer values +('02','0','25555'), +('03','1','55555'), +('04','0','66666'), +('05','1','88888'), +('06','0','688878'), +('B01','0','558558') + +insert into tbl_record values +(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-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:25: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',20), +(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) +go + +select * from tbl_card +select * from tbl_computer +select * from tbl_record +go + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select cardId 卡号,userName 用户名,ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 +from tbl_record join tbl_card on tbl_record.cardId = tbl_card.id +where userName='张军' order by fee DESC + +--2. 查询出每台机器上的上网次数和消费的总金额 +select ComputerId 机器号,COUNT(ComputerId) 上网次数,SUM(fee) 消费的总金额 from tbl_record group by ComputerId + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardId 卡号,SUM(fee) 消费总金额 from tbl_record group by cardId + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select tbl_card.id 卡号,userName 用户名 from tbl_record right join tbl_card on tbl_record.cardId = tbl_card.id where tbl_record.id is null + +--5. 将密码与用户名一样的上网卡信息查询出来 +select a.* from tbl_card a,tbl_card b where a.passWord=b.passWord and a.userName=b.userName and a.id<>b.id + +--6. 查询出使用次数最多的机器号和使用次数 +select ComputerId 机器号,COUNT(ComputerId) 次数 from tbl_record group by ComputerId +having COUNT(ComputerId)=(select top 1 COUNT(ComputerId) from tbl_record group by ComputerId order by COUNT(ComputerId) DESC) + +--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 cardId 卡号,userName 用户名,ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 +from tbl_record join tbl_card on tbl_record.cardId = tbl_card.id +where DATENAME(DW,beginTime)='星期六' or DATENAME(DW,beginTime)='星期日' + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardId 卡号,userName 用户名,ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 +from tbl_record join tbl_card on tbl_record.cardId = tbl_card.id +where datediff(HH,BeginTime,Endtime)>12 + +--10. 查询出消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardId 卡号,userName 用户名,ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 +from tbl_record join tbl_card on tbl_record.cardId = tbl_card.id +where fee in (select top 3 fee from tbl_record order by fee DESC) +order by fee DESC + +--11. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select cardId 卡号,userName 用户名,ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 +from tbl_record join tbl_card on tbl_record.cardId = tbl_card.id +where fee not in (select top 3 fee from tbl_record order by fee DESC) +order by fee DESC + +select * from tbl_record order by fee DESC \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\350\245\277\346\266\233/.keep" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\350\245\277\346\266\233/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\350\245\277\346\266\233/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\350\245\277\346\266\233/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..95d34210060c7c01ec9ce753137e6ba83506637e --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\350\245\277\346\266\233/SQLQuery2.sql" @@ -0,0 +1,105 @@ +use master + +create database LAN01 +on +( + name='LAN01', + filename='D:\LAN01.mdf', + size=5, + maxsize=15, + filegrowth=15 +) +log on +( + name='LAN_log01', + filename='D:\LAN01_log.ldf', + size=5, + maxsize=15, + filegrowth=15 +) +go +use LAN01 +go +create table tbl_card +( + id varchar(10) primary key, + [passWord] nvarchar(20) check(len(passWord) between 2 and 20), + balance decimal(10,1), + userName nvarchar(20) +) + +create table tbl_computer +( + id varchar(10) primary key, + onUse int check(onUse in (0, 1)), + note text +) + +create table tbl_record +( + id varchar(10) primary key, + cardId varchar(10) references tbl_card(id), + ComputerId varchar(10) references tbl_computer(id), + beginTime smalldatetime, + endTime smalldatetime, + fee decimal(10,1) +) + +insert into tbl_card 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 values +('02', 0, '25555'), +('03', 1, '55555'), +('04', 0, '66666'), +('05', 1, '88888'), +('06', 0, '688878'), +('B01', 0, '558558') + +insert into tbl_record values +('23', '0078_RJV', 'B01', '2007-07-15 19:00:00', '2007-07-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', 30), +('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 c.id 卡号,c.userName 用户名,ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_record D + join tbl_card C on D.cardId=C.id + where c.userName='张军' order by fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 + select ComputerId,count(*)上网次数,SUM(fee)消费的总金额 from tbl_record + group by ComputerId +--3. 查询出所有已经使用过的上网卡的消费总金额 + select cardid 上网卡,sum(fee) 消费总金额 from tbl_record group by cardId +--4. 查询出从未消费过的上网卡的卡号和用户名 + select c.id 卡号,c.userName 用户名 from tbl_card C left join tbl_record R on C.id=R.cardId where R.fee is null +--5. 将密码与用户名一样的上网卡信息查询出来 + select * from tbl_card t1 join tbl_card t2 on (t1.id = t2.id) and (t1.[passWord] = t2.userName) +--6. 查询出使用次数最多的机器号和使用次数 + select ComputerId, count(*) '使用次数' from tbl_record group by ComputerId + having count(*) = (select max(t.使用次数) from (select ComputerId, count(*) '使用次数' from tbl_record group by ComputerId) t) +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 + select tc.id, tc.userName, tr.ComputerId, tr.fee from tbl_record tr join tbl_card tc on tr.cardld = tc.id where tr.cardld like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + select tc.id, tc.userName, tr.ComputerId, tr.beginTime, tr.endTime, tr.fee from tbl_record tr join tbl_card tc on tr.cardld = tc.id + where DATENAME(dw, tr.beginTime) in ('星期六','星期日') +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + select c.ID 卡号,userName 用户名,o.ID 机器号,BeginTime 开始时间,EndTime 结束时间,Fee 消费金额 from tbl_record r inner join tbl_card c on r.cardld=c.ID + inner join tbl_computer o on r.ComputerId=o.ID where datediff(HH,BeginTime,EndTime)>12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + select top 3 c.ID 卡号,userName 用户名,ComputerId 机器号,BeginTime 开始时间,EndTime 结束时间,fee 消费金额 + from tbl_card c inner join tbl_record r on c.ID=r.cardld order by Fee desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\226\233\351\207\221\345\201\245/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\226\233\351\207\221\345\201\245/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0430e66ee539edf803781e7e5b70ad7125890ed2 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\226\233\351\207\221\345\201\245/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232.sql" @@ -0,0 +1,106 @@ +use master +go +create database tbl +on +( +name='tbl', +filename='D:\tbl.mdf', +size=10mb, +maxsize=50mb, +filegrowth=10% +) +log +on +( +name='tbl_log', +filename='D:\tbl_log.ldf', +size=10mb, +maxsize=50mb, +filegrowth=10% +) +go +use tbl +go + +create table tbl_card +( +cardid nvarchar(20) primary key , +password nvarchar(20), +balance int, +username nvarchar(20) +) +create table tbl_computer +( +computerid varchar(20) primary key , +onuse char(2) check(onuse=1 or onuse=0), +note nvarchar(50) +) +create table tbl_record +( +recordid varchar(20) primary key, +cardid nvarchar(20) references tbl_card(cardid), +computerId varchar(20) references tbl_computer(computerid), +beginTime datetime , +endTime datetime , +fee int , +) + +insert into tbl_card 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 values +('02',0,25555),('03',1,55555),('04',0,66666),('05',1,88888),('06',0,688878),('b01',0,558558) + +insert into tbl_record values +(23,'0078_RJV','b01','2007-07-15 19:00:00','2007-07-15 21:00:00',20), +(34,'0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00',30), +(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 19:00:00','2007-07-15 21:00:00',20), +(55,'0078_RJV','b01','2006-12-23 15:26:00','2006-12-23 22:55:00',20), +(64,'0078_RJV','b01','2006-12-23 15:26:00','2006-12-23 22:55:00',50), +(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 userName 卡号,recordid 用户名,ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_record R +inner join tbl_card C on R.cardid=C.cardid +where userName='张军' order by fee desc +--2. 查询出 每台机器上 的上网次数 和 消费的总金额 +select ComputerId,count(*)上网次数,sum(fee)总金额 from tbl_record group by ComputerId +--3. 查询出 所有已经使用过 的 上网卡 的 消费总金额 +select cardid,sum(fee)消费总金额 from tbl_record group by cardid +--4. 查询出从未消费过的上网卡的卡号和用户名 +select C.cardid 卡号,C.username 用户名 from tbl_card C left join tbl_record R on C.cardid=R.cardid where fee is null +--5. 将密码 与 用户名一样 的 上网卡信息 查询出来 +select * from tbl_card where passWord=userName +--6. 查询出使用次数最多的机器号和使用次数 + select ComputerId,count(ComputerId) from tbl_record group by ComputerId order by count(ComputerId) desc +--7. 查询出卡号是以'ABC'结尾的 卡号,用户名,上网的机器号和消费金额 +select C.cardid 卡号,C.username 用户名,R.computerId 机器号,R.fee 消费金额 from tbl_card C +inner join tbl_record R on C.cardid=R.cardid +where C.cardid like'%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select C.cardid 卡号,C.username 用户名,R.computerId 机器号, R.beginTime 开始时间,R.endTime 结束时间,R.fee 消费金额 from tbl_record R +inner join tbl_card C on C.cardid=R.cardid +where datename(DW,beginTime)='星期六'or datename(DW,beginTime)='星期日' +--9. 查询成 一次上网时间超过12小时 的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select C.cardid 卡号,C.username 用户名,R.computerId 机器号, R.beginTime 开始时间,R.endTime 结束时间,R.fee 消费金额 from tbl_record R +inner join tbl_card C on C.cardid=R.cardid +where datediff(HH ,R.beginTime ,R.endTime )>12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select C.cardid 卡号,C.username 用户名,R.computerId 机器号, R.beginTime 开始时间,R.endTime 结束时间,R.fee 消费金额 from tbl_record R +inner join tbl_card C on C.cardid=R.cardid +where fee not in (select top 3 fee from tbl_record order by fee desc) order by fee desc + +--前三名 +select C.cardid 卡号,C.username 用户名,R.computerId 机器号, R.beginTime 开始时间,R.endTime 结束时间,R.fee 消费金额 from tbl_record R +inner join tbl_card C on C.cardid=R.cardid where fee in (select top 3 fee from tbl_record order by fee desc) order by fee desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\242\201\345\235\232/SQLQueryq2.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\242\201\345\235\232/SQLQueryq2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7e58053c59508d440d8b1ad2cee1dbcfab363294 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\242\201\345\235\232/SQLQueryq2.sql" @@ -0,0 +1,96 @@ +use master +go +create database wb +go +use wb +go +create table cardq --上网卡信息表 +( + ID char(8) primary key, --卡号 + PassWord char(12) not null, --密码 + Balance int not null,--卡上的余额 + UserName nchar(6) not null--该上网卡所属的用户名 +) +create table computer --机器说明表 +( + ID char(4) primary key, --机器编号 + OnUse char(1) check(OnUse='0' or OnUse='1') not null,--该机器是否正在使用 + Notr char(7) not null --该机器的说明 +) +create table record --上网记录表 +( + ID int primary key, --记录编号 + CardID char(8) references cardq(ID) not null, --本次上网的卡号 + ComputerID char(4) references computer(ID) not null, --本次上网记录所使用的机器号 + BeginTime datetime , --本次上网记录的开始时间 + EndTime datetime, --本次上网记录的结束时间 + Fee money --本次上网的费用 +) +insert into cardq values +('0023_ABC','555',98,'张军'),('0025_bbd','abc',300,'朱骏'),('0036_CCD','何柳',100,'何柳'), +('0045_YGR','0045_YGR',58,'验证'),('0078_RJV','55885fg',600,'校庆'),('0089_EDE','zhang',134,'张峻') +insert into computer values +('02','0','25555'),('03','1','55555'),('04','0','6666'), +('05','1','88888'),('06','0','688878'),('B01','0','558558') +insert into record values +(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-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:00:00','2006-12-23 22:55:00','50'), +(46,'0023_ABC','03','2006-12-22 15:00:00','2006-12-22 22:55:00','6'), +(47,'0023_ABC','03','2006-12-23 15:00:00','2006-12-23 22:55:00','50'), +(48,'0023_ABC','03','2007-01-06 15:00:00','2006-01-06 22:55:00','6'), +(55,'0023_ABC','03','2006-07-21 15:00: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 cardq --上网卡信息表 +select*from computer --机器说明表 +select*from record --上网记录表 + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select CardID 卡号,UserName 用户名,co.ID 机器编号,BeginTime 开始时间,EndTime 结束时间,Fee 消费金额 from record r +inner join cardq c on r.CardID=c.ID inner join computer co on co.ID=r.ComputerID where UserName='张军' order by Fee desc + +--2. 查询出每台机器上的上网次数和消费的总金额 +select c.ID 机器编号,count(*) 上网次数,sum(Fee )消费的总金额 from record r +left join computer c on r.ComputerID=c.ID +group by c.ID + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select C.ID 网卡号,sum(Fee) 消费总金额 from cardq c +right join record r on c.ID=r.CardID +group by C.ID + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select C.ID 网卡号,UserName 用户名 from cardq c +left join record r on c.ID=r.CardID +where Fee is null +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from cardq C inner join cardq A on C.PassWord=A.UserName + +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 c.ID 机器号,count(*) 使用次数 from record r +left join computer c on c.ID=r.ComputerID +group by c.ID order by count(*) desc + + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select ca.ID 卡号,UserName 用户名,ComputerID 机器号,Fee 消费金额 from cardq ca +left join record r on ca.ID=r.CardID +where ca.ID like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select C.ID 卡号 , UserName 用户名 , ComputerID 机器编号 , BeginTime 开始时间 , Endtime 结束时间 from record R +inner join cardq C on R.CardID=C.ID +where datename(weekday,BeginTime)='星期六' or datename(weekday,BeginTime)='星期日' + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select C.ID 卡号 , UserName 用户名 , ComputerID 机器编号 , Begintime 开始时间 , Endtime 结束时间 , fee 消费金额 from record R +inner join cardq C on R.CardID=C.ID +where datediff(hh,EndTime,BeginTime)>12 + +--10. 查询消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 C.ID 卡号 , UserName 用户名 ,ComputerID 机器编号 , BeginTime 开始时间, EndTime 结束时间 , fee 消费金额 from record R +inner join cardq C on C.Id = R.CardID +order by fee desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\246\203\347\245\232\345\233\275/.keep" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\246\203\347\245\232\345\233\275/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\246\203\347\245\232\345\233\275/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\246\203\347\245\232\345\233\275/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..70730ee80138f4a9452945f0caf81ab89ddba0f3 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\246\203\347\245\232\345\233\275/SQLQuery1.sql" @@ -0,0 +1,90 @@ +use master +go +create database tbl +on +( +name='tbl', +filename='D:\tbl.mdf', +size=5, +maxsize=50, +filegrowth=10% +) +log on +( +name='tbl_log', +filename='D:\tbl_log.ldf', +size=5, +maxsize=50, +filegrowth=10% +) +go +use tbl +go +create table tbl_card--上网卡信息表 +( +id varchar(10) primary key ,--卡号 +passWord varchar(20) ,--密码 +balance int,--余额 +userName varchar(10) --上网卡所属的用户名 +) +create table tbl_computer--网吧机器说明表 +( +id varchar(10) primary key not null,--机器编号 +onUs int,--该机器是否正在使用 +note int --该机器的说明 +) +create table tbl_record --上网记录表 +( +id int primary key ,--记录编号 +cardId varchar(10) references tbl_card(id),--本次上网的卡号 +ComputerId varchar(10) references tbl_computer(id),--本次上网记录所使用的机器号 +beginTime datetime,--本次上网记录的开始时间 +endTime datetime,--本次上网记录的结束时间 +fee int--本次上网的费用 +) +go +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,onUs,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-07-15 19:00:00','2007-07-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','3'), +('64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','3'), +('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 C.id 卡号,userName 用户名,ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_card C inner join tbl_record R on C.id=R.cardId where userName='张军' order by fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 +select ComputerId 机器,count(ComputerId)上网次数,sum(fee)消费的总金额 from tbl_record group by ComputerId +--3. 查询出所有已经使用过的上网卡的消费总金额 +select cardId 网卡,sum(fee)消费总金额 from tbl_record group by cardId +--4. 查询出从未消费过的上网卡的卡号和用户名 +select a.id 卡号,userName 用户名 from tbl_card a left join tbl_record b on a.id=b.cardId where fee is null +--5. 将密码与用户名一样的上网卡信息查询出来 +select a.* from tbl_card a inner join tbl_card b on a.id=b.id and a.passWord=b.userName +--6. 查询出使用次数最多的机器号和使用次数 +select top 1 ComputerId 机器号,count(ComputerId) 使用次数 from tbl_record group by ComputerId order by count(ComputerId) desc +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select a.id,userName,ComputerId,fee from tbl_card a inner join tbl_record b on a.id=b.cardId +where a.id like'%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select a.id 卡号,userName 用户名,ComputerId 机器号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_card a inner join tbl_record b on a.id=b.cardId +where datename(weekday,beginTime)='星期六' or datename(weekday,beginTime)='星期日' +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select b.id 卡号, userName 用户名, computerID 机器编号,beginTime 开始时间,endTime 结束时间, fee 消费金额 from tbl_record a inner join tbl_card b on a.cardID=b.id +where DATEDIFF(HH,beginTime,endTime)>12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select * from tbl_record where id in (select top 3 id from tbl_record order by fee desc) + + + diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\260\255\350\277\233/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\260\255\350\277\233/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0430e66ee539edf803781e7e5b70ad7125890ed2 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\260\255\350\277\233/\347\254\254\345\215\201\344\272\214\346\254\241\344\275\234\344\270\232.sql" @@ -0,0 +1,106 @@ +use master +go +create database tbl +on +( +name='tbl', +filename='D:\tbl.mdf', +size=10mb, +maxsize=50mb, +filegrowth=10% +) +log +on +( +name='tbl_log', +filename='D:\tbl_log.ldf', +size=10mb, +maxsize=50mb, +filegrowth=10% +) +go +use tbl +go + +create table tbl_card +( +cardid nvarchar(20) primary key , +password nvarchar(20), +balance int, +username nvarchar(20) +) +create table tbl_computer +( +computerid varchar(20) primary key , +onuse char(2) check(onuse=1 or onuse=0), +note nvarchar(50) +) +create table tbl_record +( +recordid varchar(20) primary key, +cardid nvarchar(20) references tbl_card(cardid), +computerId varchar(20) references tbl_computer(computerid), +beginTime datetime , +endTime datetime , +fee int , +) + +insert into tbl_card 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 values +('02',0,25555),('03',1,55555),('04',0,66666),('05',1,88888),('06',0,688878),('b01',0,558558) + +insert into tbl_record values +(23,'0078_RJV','b01','2007-07-15 19:00:00','2007-07-15 21:00:00',20), +(34,'0025_bbd','02','2006-12-25 18:00:00','2006-12-25 22:00:00',30), +(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 19:00:00','2007-07-15 21:00:00',20), +(55,'0078_RJV','b01','2006-12-23 15:26:00','2006-12-23 22:55:00',20), +(64,'0078_RJV','b01','2006-12-23 15:26:00','2006-12-23 22:55:00',50), +(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 userName 卡号,recordid 用户名,ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_record R +inner join tbl_card C on R.cardid=C.cardid +where userName='张军' order by fee desc +--2. 查询出 每台机器上 的上网次数 和 消费的总金额 +select ComputerId,count(*)上网次数,sum(fee)总金额 from tbl_record group by ComputerId +--3. 查询出 所有已经使用过 的 上网卡 的 消费总金额 +select cardid,sum(fee)消费总金额 from tbl_record group by cardid +--4. 查询出从未消费过的上网卡的卡号和用户名 +select C.cardid 卡号,C.username 用户名 from tbl_card C left join tbl_record R on C.cardid=R.cardid where fee is null +--5. 将密码 与 用户名一样 的 上网卡信息 查询出来 +select * from tbl_card where passWord=userName +--6. 查询出使用次数最多的机器号和使用次数 + select ComputerId,count(ComputerId) from tbl_record group by ComputerId order by count(ComputerId) desc +--7. 查询出卡号是以'ABC'结尾的 卡号,用户名,上网的机器号和消费金额 +select C.cardid 卡号,C.username 用户名,R.computerId 机器号,R.fee 消费金额 from tbl_card C +inner join tbl_record R on C.cardid=R.cardid +where C.cardid like'%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select C.cardid 卡号,C.username 用户名,R.computerId 机器号, R.beginTime 开始时间,R.endTime 结束时间,R.fee 消费金额 from tbl_record R +inner join tbl_card C on C.cardid=R.cardid +where datename(DW,beginTime)='星期六'or datename(DW,beginTime)='星期日' +--9. 查询成 一次上网时间超过12小时 的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select C.cardid 卡号,C.username 用户名,R.computerId 机器号, R.beginTime 开始时间,R.endTime 结束时间,R.fee 消费金额 from tbl_record R +inner join tbl_card C on C.cardid=R.cardid +where datediff(HH ,R.beginTime ,R.endTime )>12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select C.cardid 卡号,C.username 用户名,R.computerId 机器号, R.beginTime 开始时间,R.endTime 结束时间,R.fee 消费金额 from tbl_record R +inner join tbl_card C on C.cardid=R.cardid +where fee not in (select top 3 fee from tbl_record order by fee desc) order by fee desc + +--前三名 +select C.cardid 卡号,C.username 用户名,R.computerId 机器号, R.beginTime 开始时间,R.endTime 结束时间,R.fee 消费金额 from tbl_record R +inner join tbl_card C on C.cardid=R.cardid where fee in (select top 3 fee from tbl_record order by fee desc) order by fee desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\265\265\345\256\266\351\275\220/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\265\265\345\256\266\351\275\220/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a06ab013cfdc7c7d058cabba9da02da79f6885b5 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\265\265\345\256\266\351\275\220/SQLQuery1.sql" @@ -0,0 +1,109 @@ +use master +go +create database TBL +on +( + name='TBL', + filename='D:\SQL\TBL.mdf', + size=5MB, + maxsize=20MB, + filegrowth=10% +) +log on +( + name='TBL_log', + filename='D:\SQL\TBL_log.ldf', + size=5MB, + maxsize=20MB, + filegrowth=10% +) +go + +use TBL +go +create table TBL_Card +( + CardID char(30) primary key not null, + PassWord varchar(50) not null, + Balance money , + UserName nvarchar(20) +) + +create table TBL_Computer +( + CID char(30) primary key , + OnUse char(1) check(OnUse in(1,0)), + note text +) + +create table TBL_Record +( + RID int primary key , + CardID char(30) references TBL_Card(CardID), + CID char(30) references TBL_Computer(CID), + BeginTime datetime, + Endtime datetime, + fee money +) +go + +insert into TBL_Card values +('0023_ABC','555',98,'张军'), +('0025_bbd','abe',300,'朱俊'), +('0036_CCD','何柳',100,'何柳'), +('0045_YGR','00445_YGR',58,'证验'), +('0078_RJV','55885fg',600,'校庆'), +('0089_EDE','zhang',134,'张俊') + +insert into TBL_Computer values +('02','0','25555'), +('03','1','55555'), +('04','0','66666'), +('05','1','88888'), +('06','0','688878'), +('B01','0','558558') + +insert into TBL_Record values +(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-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:25:00',50), +(46,'0023_ABC','03','2006-12-23 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',20), +(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',3), +(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) +go + +select * from TBL_Card +select * from TBL_Computer +select * from TBL_Record +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select C.CardID 卡号 , UserName 用户名 , CID 机器编号 , BeginTime 开始时间 , Endtime 结束时间 , fee 消费金额 from TBL_Record R inner join TBL_Card C on R.CardID=C.CardID where UserName='张军' order by fee DESC +--2. 查询出每台机器上的上网次数和消费的总金额 +select CID 机器编号 , COUNT(*) 上网次数 , SUM(fee) 总金额 from TBL_Record R group by CID +--3. 查询出所有已经使用过的上网卡的消费总金额 +select CardID 卡号 , SUM(fee) 总金额 from TBL_Record group by CardID +--4. 查询出从未消费过的上网卡的卡号和用户名 +select C.CardID 卡号 , UserName 用户名 , RID from TBL_Record R right join TBL_Card C on R.CardID=C.CardID group by C.CardID,UserName,RID having RID is null or RID='' +--5. 将密码与用户名一样的上网卡信息查询出来 +select A.* from TBL_Card A,TBL_Card B where A.PassWord=B.UserName +--6. 查询出使用次数最多的机器号和使用次数 +--select top 1 COUNT(*) from TBL_Record group by CID order by COUNT(*) DESC +select CID 机器号 , COUNT(*) 使用次数 from TBL_Record group by CID having COUNT(*)=(select top 1 COUNT(*) from TBL_Record group by CID order by COUNT(*) DESC) +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select C.CardID 卡号 , UserName 用户名 , CID 机器编号 , fee 消费金额 from TBL_Record R inner join TBL_Card C on R.CardID=C.CardID where C.CardID like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select C.CardID 卡号 , UserName 用户名 , CID 机器编号 , BeginTime 开始时间 , Endtime 结束时间 , fee 消费金额 from TBL_Record R inner join TBL_Card C on R.CardID=C.CardID where datename(weekday,BeginTime)='星期六' or datepart(weekday,BeginTime)=1 +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select C.CardID 卡号 , UserName 用户名 , CID 机器编号 , BeginTime 开始时间 , Endtime 结束时间 , fee 消费金额 from TBL_Record R inner join TBL_Card C on R.CardID=C.CardID where DATEDIFF(HH,BeginTime,Endtime)>12 +--10. 查询出消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select C.CardID 卡号 , UserName 用户名 , CID 机器编号 , BeginTime 开始时间 , Endtime 结束时间 , fee 消费金额 from TBL_Record R +inner join TBL_Card C on R.CardID=C.CardID +where RID not in (select top 3 RID from TBL_Record order by fee DESC) +group by C.CardID,UserName,CID,BeginTime,Endtime,fee +--11. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 RID from TBL_Record order by fee DESC +select C.CardID 卡号 , UserName 用户名 , CID 机器编号 , BeginTime 开始时间 , Endtime 结束时间 , fee 消费金额 from TBL_Record R inner join TBL_Card C on R.CardID=C.CardID where RID in (select top 3 RID from TBL_Record order by fee DESC) +group by C.CardID , UserName , CID , BeginTime , Endtime , fee \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\277\252\345\212\233\344\272\232\345\260\224/1.txt" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\277\252\345\212\233\344\272\232\345\260\224/1.txt" new file mode 100644 index 0000000000000000000000000000000000000000..0b1355a04b5462bc6c9dfc1f582d14101f707d87 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\350\277\252\345\212\233\344\272\232\345\260\224/1.txt" @@ -0,0 +1,135 @@ +use master +go +create database tbl +on +( + name='tbl', + filename='D:\SQL\tbl.mdf', + size=5MB, + maxsize=20MB, + filegrowth=10% +) +log on +( + name='tbl_log', + filename='D:\SQL\tbl_log.ldf', + size=5MB, + maxsize=20MB, + filegrowth=10% +) +go + +use tbl +go + +create table tbl_card +( +id varchar(10) primary key, +passWord varchar(10), +balance varchar(5), +userName nvarchar(2) +) +go + +create table tbl_computer +( +id varchar(5) primary key, +onUse varchar(1) check(onUse=0 or onUse=1), +note varchar(10) +) +go + +create table tbl_record +( +id varchar(2) primary key, +cardId varchar(10) references tbl_card(id), +ComputerId varchar(5) references tbl_computer(id), +beginTime datetime, +endTime datetime, +fee money +) +go + +insert into tbl_card values +('0023_ABC','555',98,'寮犲啗'), +('0025_bbd','abe',300,'鏈变繆'), +('0036_CCD','浣曟煶',100,'浣曟煶'), +('0045_YGR','00445_YGR',58,'璇侀獙'), +('0078_RJV','55885fg',600,'鏍″簡'), +('0089_EDE','zhang',134,'寮犱繆') + +insert into tbl_computer values +('02','0','25555'), +('03','1','55555'), +('04','0','66666'), +('05','1','88888'), +('06','0','688878'), +('B01','0','558558') + +insert into tbl_record values +(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-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:25: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',20), +(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) +go + +select * from tbl_card +select * from tbl_computer +select * from tbl_record +go + +--1. 鏌ヨ鍑虹敤鎴峰悕涓'寮犲啗'鐨勪笂缃戝崱鐨勪笂缃戣褰曪紝瑕佹眰鏄剧ず鍗″彿锛岀敤鎴峰悕锛屾満鍣ㄧ紪鍙枫佸紑濮嬫椂闂淬佺粨鏉熸椂闂达紝鍜屾秷璐归噾棰濓紝骞舵寜娑堣垂閲戦闄嶅簭鎺掑垪 +select cardId 鍗″彿,userName 鐢ㄦ埛鍚,ComputerId 鏈哄櫒缂栧彿,beginTime 寮濮嬫椂闂,endTime 缁撴潫鏃堕棿,fee 娑堣垂閲戦 +from tbl_record join tbl_card on tbl_record.cardId = tbl_card.id +where userName='寮犲啗' order by fee DESC + +--2. 鏌ヨ鍑烘瘡鍙版満鍣ㄤ笂鐨勪笂缃戞鏁板拰娑堣垂鐨勬婚噾棰 +select ComputerId 鏈哄櫒鍙,COUNT(ComputerId) 涓婄綉娆℃暟,SUM(fee) 娑堣垂鐨勬婚噾棰 from tbl_record group by ComputerId + +--3. 鏌ヨ鍑烘墍鏈夊凡缁忎娇鐢ㄨ繃鐨勪笂缃戝崱鐨勬秷璐规婚噾棰 +select cardId 鍗″彿,SUM(fee) 娑堣垂鎬婚噾棰 from tbl_record group by cardId + +--4. 鏌ヨ鍑轰粠鏈秷璐硅繃鐨勪笂缃戝崱鐨勫崱鍙峰拰鐢ㄦ埛鍚 +select tbl_card.id 鍗″彿,userName 鐢ㄦ埛鍚 from tbl_record right join tbl_card on tbl_record.cardId = tbl_card.id where tbl_record.id is null + +--5. 灏嗗瘑鐮佷笌鐢ㄦ埛鍚嶄竴鏍风殑涓婄綉鍗′俊鎭煡璇㈠嚭鏉 +select a.* from tbl_card a,tbl_card b where a.passWord=b.passWord and a.userName=b.userName and a.id<>b.id + +--6. 鏌ヨ鍑轰娇鐢ㄦ鏁版渶澶氱殑鏈哄櫒鍙峰拰浣跨敤娆℃暟 +select ComputerId 鏈哄櫒鍙,COUNT(ComputerId) 娆℃暟 from tbl_record group by ComputerId +having COUNT(ComputerId)=(select top 1 COUNT(ComputerId) from tbl_record group by ComputerId order by COUNT(ComputerId) DESC) + +--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 cardId 鍗″彿,userName 鐢ㄦ埛鍚,ComputerId 鏈哄櫒缂栧彿,beginTime 寮濮嬫椂闂,endTime 缁撴潫鏃堕棿,fee 娑堣垂閲戦 +from tbl_record join tbl_card on tbl_record.cardId = tbl_card.id +where DATENAME(DW,beginTime)='鏄熸湡鍏' or DATENAME(DW,beginTime)='鏄熸湡鏃' + +--9. 鏌ヨ鎴愪竴娆′笂缃戞椂闂磋秴杩12灏忔椂鐨勭殑涓婄綉璁板綍锛岃姹傛樉绀轰笂缃戠殑鍗″彿锛岀敤鎴峰悕锛屾満鍣ㄥ彿锛屽紑濮嬫椂闂淬佺粨鏉熸椂闂村拰娑堣垂閲戦 +select cardId 鍗″彿,userName 鐢ㄦ埛鍚,ComputerId 鏈哄櫒缂栧彿,beginTime 寮濮嬫椂闂,endTime 缁撴潫鏃堕棿,fee 娑堣垂閲戦 +from tbl_record join tbl_card on tbl_record.cardId = tbl_card.id +where datediff(HH,BeginTime,Endtime)>12 + +--10. 鏌ヨ鍑烘秷璐归噾棰濇帓鍒楀墠涓夊悕(鏈楂)鐨勪笂缃戣褰曪紝瑕佹眰鏄剧ず涓婄綉鐨勫崱鍙凤紝鐢ㄦ埛鍚嶏紝鏈哄櫒鍙凤紝寮濮嬫椂闂淬佺粨鏉熸椂闂村拰娑堣垂閲戦 +select cardId 鍗″彿,userName 鐢ㄦ埛鍚,ComputerId 鏈哄櫒缂栧彿,beginTime 寮濮嬫椂闂,endTime 缁撴潫鏃堕棿,fee 娑堣垂閲戦 +from tbl_record join tbl_card on tbl_record.cardId = tbl_card.id +where fee in (select top 3 fee from tbl_record order by fee DESC) +order by fee DESC + +--11. 鏌ヨ闄ゆ秷璐归噾棰濇帓鍒楀墠涓夊悕(鏈楂)鐨勪笂缃戣褰曪紝瑕佹眰鏄剧ず涓婄綉鐨勫崱鍙凤紝鐢ㄦ埛鍚嶏紝鏈哄櫒鍙凤紝寮濮嬫椂闂淬佺粨鏉熸椂闂村拰娑堣垂閲戦 +select cardId 鍗″彿,userName 鐢ㄦ埛鍚,ComputerId 鏈哄櫒缂栧彿,beginTime 寮濮嬫椂闂,endTime 缁撴潫鏃堕棿,fee 娑堣垂閲戦 +from tbl_record join tbl_card on tbl_record.cardId = tbl_card.id +where fee not in (select top 3 fee from tbl_record order by fee DESC) +order by fee DESC + +select * from tbl_record order by fee DESC +璇勮 ( 0 ) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\202\261\346\236\227\346\236\253/.keep" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\202\261\346\236\227\346\236\253/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\202\261\346\236\227\346\236\253/\344\275\234\344\270\232\344\272\214.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\202\261\346\236\227\346\236\253/\344\275\234\344\270\232\344\272\214.sql" new file mode 100644 index 0000000000000000000000000000000000000000..eecbb0c94270d8b96dff2fa7691de8c935218a5e --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\202\261\346\236\227\346\236\253/\344\275\234\344\270\232\344\272\214.sql" @@ -0,0 +1,104 @@ +use master +go + +if exists(select * from sys.databases where name = 'tbl') +drop database tbl + +create database tbl +go + +use tbl +go + +create table tbl_card +( + id varchar(10) primary key, + passWord varchar(10) not null, + balance decimal(10,1) not null, + userName nchar(2) not null +) + +create table tbl_computer +( + id varchar(10) primary key, + onUse char(1) check(onUse='0' or onUse='1') not null, + note text +) + +create table tbl_record +( + id int primary key, + cardId varchar(10) references tbl_card(id) not null, + ComputerId varchar(10) references tbl_computer(id) not null, + beginTime datetime not null, + endTime datetime not null, + fee decimal(10,1) +) + +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,'8888'), +('06',0,'688878'), +('B01',0,'558558') + +insert into tbl_record(id,cardId,ComputerId,beginTime,endTime,fee) values +('23','0078_RJV','B01','2007-07-15 19:00:00','2007-07-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','30'), +('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') +--璇峰畬鎴愪互涓嬮鐩細 +--1. 鏌ヨ鍑虹敤鎴峰悕涓'寮犲啗'鐨勪笂缃戝崱鐨勪笂缃戣褰曪紝瑕佹眰鏄剧ず鍗″彿锛岀敤鎴峰悕锛屾満鍣ㄧ紪鍙枫佸紑濮嬫椂闂淬佺粨鏉熸椂闂达紝鍜屾秷璐归噾棰濓紝骞舵寜娑堣垂閲戦闄嶅簭鎺掑垪 +select tc.id 鍗″彿,userName 鐢ㄦ埛鍚,tc.id 鏈哄櫒缂栧彿,beginTime 寮濮嬫椂闂,endTime 缁撴潫鏃堕棿,fee 娑堣垂閲戦 from tbl_card tc +inner join tbl_record tr on tc.id = tr.cardId where userName='寮犲啗' order by fee desc + +--2. 鏌ヨ鍑烘瘡鍙版満鍣ㄤ笂鐨勪笂缃戞鏁板拰娑堣垂鐨勬婚噾棰 +select ComputerId,count(*) 涓婄綉娆℃暟,sum(fee)娑堣垂鐨勬婚噾棰 from tbl_record group by ComputerId + +--3. 鏌ヨ鍑烘墍鏈夊凡缁忎娇鐢ㄨ繃鐨勪笂缃戝崱鐨勬秷璐规婚噾棰 +select cardId ,sum(fee)娑堣垂鎬婚噾棰 from tbl_record group by cardId + +--4. 鏌ヨ鍑轰粠鏈秷璐硅繃鐨勪笂缃戝崱鐨勫崱鍙峰拰鐢ㄦ埛鍚 +select tc.id,tc.userName from tbl_card tc +left join tbl_record tr on tr.cardId = tc.id where tr.id is null + +--5. 灏嗗瘑鐮佷笌鐢ㄦ埛鍚嶄竴鏍风殑涓婄綉鍗′俊鎭煡璇㈠嚭鏉 +select * from tbl_card t1 join tbl_card t2 on t1.id = t2.id and t1.passWord = t2.userName + +--6. 鏌ヨ鍑轰娇鐢ㄦ鏁版渶澶氱殑鏈哄櫒鍙峰拰浣跨敤娆℃暟 +select ComputerId,count(*)浣跨敤娆℃暟 from tbl_record group by ComputerId +having count(*)=(select max(m.浣跨敤娆℃暟) from (select ComputerId,count(*)浣跨敤娆℃暟 from tbl_record group by ComputerId)m) + +--7. 鏌ヨ鍑哄崱鍙锋槸浠'ABC'缁撳熬鐨勫崱鍙凤紝鐢ㄦ埛鍚嶏紝涓婄綉鐨勬満鍣ㄥ彿鍜屾秷璐归噾棰 +select tc.id 鍗″彿,tc.userName 鐢ㄦ埛鍚,tr.ComputerId 鏈哄櫒鍙,tr.fee 娑堣垂閲戦 from tbl_record tr join tbl_card tc on tr.cardId = tc.id + +where tc.id like '%ABC' +--8. 鏌ヨ鍑烘槸鍛ㄥ叚銆佸懆澶╀笂缃戠殑璁板綍锛岃姹傛樉绀轰笂缃戠殑鍗″彿锛岀敤鎴峰悕锛屾満鍣ㄥ彿锛屽紑濮嬫椂闂淬佺粨鏉熸椂闂村拰娑堣垂閲戦 +select tc.id 鍗″彿,tc.userName 鐢ㄦ埛鍚,tr.ComputerId 鏈哄櫒鍙,beginTime 寮濮嬫椂闂,endTime 缁撴潫鏃堕棿,tr.fee 娑堣垂閲戦 from tbl_record tr join tbl_card tc on tr.cardId = tc.id +where datename(DW,beginTime)='鏄熸湡鍏' or datename(DW,beginTime)='鏄熸湡鏃' + +--9. 鏌ヨ鍑轰竴娆′笂缃戞椂闂磋秴杩12灏忔椂鐨勭殑涓婄綉璁板綍锛岃姹傛樉绀轰笂缃戠殑鍗″彿锛岀敤鎴峰悕锛屾満鍣ㄥ彿锛屽紑濮嬫椂闂淬佺粨鏉熸椂闂村拰娑堣垂閲戦 +select tc.id 鍗″彿,tc.userName 鐢ㄦ埛鍚,tr.ComputerId 鏈哄櫒鍙,beginTime 寮濮嬫椂闂,endTime 缁撴潫鏃堕棿,tr.fee 娑堣垂閲戦 from tbl_record tr join tbl_card tc on tr.cardId = tc.id +where datediff(HH,beginTime,endTime)>12 + +--10. 鏌ヨ鍑烘秷璐归噾棰濇帓鍒楀墠涓夊悕(鏈楂)鐨勪笂缃戣褰曪紝瑕佹眰鏄剧ず涓婄綉鐨勫崱鍙凤紝鐢ㄦ埛鍚嶏紝鏈哄櫒鍙凤紝寮濮嬫椂闂淬佺粨鏉熸椂闂村拰娑堣垂閲戦 +select tc.id 鍗″彿,tc.userName 鐢ㄦ埛鍚,tr.ComputerId 鏈哄櫒鍙,beginTime 寮濮嬫椂闂,endTime 缁撴潫鏃堕棿, tr.fee 娑堣垂閲戦 from tbl_record tr join tbl_card tc on tr.cardId = tc.id +where tr.fee in (select distinct top 3 fee from tbl_record order by fee desc) order by fee desc + + diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\346\214\257\347\201\277/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\346\214\257\347\201\277/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8873bd3b9f747cfddfdaae13e4d0cf7162c6caf9 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\346\214\257\347\201\277/SQLQuery2.sql" @@ -0,0 +1,153 @@ + +--首先创建网吧计费系统的数据库,表结构和数据如2.bmp所示,表的说明如下: +create database WWW +go +use WWW +go +--表一为上网卡信息表(tbl_card) +--id: 卡号,主键 +--passWord:密码 +--balance:卡上的余额 +--userName:该上网卡所属的用户名 +create table tbl_card +( +ID nvarchar(10) primary key, +PassWord nvarchar(20) not null, +Balance decimal(10,1) not null, +UserName nvarchar(10) not null +) +insert into tbl_card 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,'张俊') +select * from tbl_card +--表2为网吧机器说明表(tbl_computer) +--id:机器编号,主键 +--onUse:该机器是否正在使用,0为未使用,1为正在使用 +--note:该机器的说明 +create table tbl_computer +( +ID varchar(10) primary key, +OnUse varchar(10) check(OnUse='1' or OnUse='0') not null, +Note nvarchar(10) not null +) +insert into tbl_computer values +('02',0,25555), +('03',1,55555), +('04',0,66666), +('05',1,88888), +('06',0,688878), +('B01',0,558558) +select * from tbl_computer +--表3为上网记录表(tbl_record): +--id:记录编号,主键 +--cardId:本次上网的卡号,外键 +--ComputerId:本次上网记录所使用的机器号,外键 +--beginTime:本次上网记录的开始时间 +--endTime:本次上网记录的结束时间 +--fee:本次上网的费用 +create table tbl_record +( +ID varchar(10) primary key, +CardID nvarchar(10) references tbl_card(ID), +ComputerID varchar(10) references tbl_computer(ID), +BeginTime datetime not null, +EndTime datetime not null, +Fee decimal(10,1) +) +insert into tbl_record values +('23','0078_RJV','B01','2007-07-15 19:00:00','2007-07-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',30), +('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_record +--请完成以下题目: + +--表一为上网卡信息表(tbl_card) +--id: 卡号,主键 +--passWord:密码 +--balance:卡上的余额 +--userName:该上网卡所属的用户名 + +--表2为网吧机器说明表(tbl_computer) +--id:机器编号,主键 +--onUse:该机器是否正在使用,0为未使用,1为正在使用 +--note:该机器的说明 + + +--表3为上网记录表(tbl_record): +--id:记录编号,主键 +--cardId:本次上网的卡号,外键 +--ComputerId:本次上网记录所使用的机器号,外键 +--beginTime:本次上网记录的开始时间 +--endTime:本次上网记录的结束时间 +--fee:本次上网的费用 + +select * from tbl_card + +select * from tbl_record + +select * from tbl_computer + +--1. 查询出用户名为 '张军' 的上网卡的上网记录,要求显示 卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 + +select TC.ID 卡号,TC.UserName 用户名,TR.ComputerID 机器编号,TR.BeginTime 开始时间,TR.EndTime 结束时间,TR.Fee 消费金额 +from tbl_record TR join tbl_card TC on TR.CardID = TC.ID where TC.UserName = '张军' order by TR.Fee DESC + +--2. 查询出每台机器上的 上网次数 和 消费的总金额 + +select ComputerID,COUNT(ComputerID) 上网次数,SUM(Fee) 总金额 from tbl_record group by ComputerID + +--3. 查询出所有已经使用过的上网卡的 消费总金额 + +select cardId, SUM(Fee) 总金额 from tbl_record group by cardId + +--4. 查询出从未消费过的上网卡的 卡号 和 用户名 + +select TC.ID 卡号,TC.UserName 用户名 from tbl_record TR +right join tbl_card TC on TR.CardID = TC.ID where TR.Fee is null group by TC.ID,TC.UserName + +--5. 将密码与用户名一样的上网卡信息查询出来 + +select * from tbl_card TC join tbl_card BC on TC.ID = BC.ID where TC.PassWord = BC.UserName + +--6. 查询出使用次数最多的机器号和使用次数 + +select TR.ComputerID 机器号,COUNT(*) 使用次数 from tbl_record TR +join tbl_computer LC on TR.ComputerID = LC.ID group by TR.ComputerID +having TR.ComputerID = (select top 1 ComputerID from tbl_record group by ComputerID order by COUNT (*) DESC) + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 + +select TC.ID 卡号,TC.UserName 用户名,TR.ComputerID 机器编号,TR.Fee 消费金额 from tbl_record TR +join tbl_card TC on TR.CardID = TC.ID where TC.ID like '%ABC'group by TC.ID,TC.UserName,TR.ComputerID,TR.Fee + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select TC.ID 卡号,TC.UserName 用户名,TR.ComputerID 机器编号,TR.BeginTime 开始时间,TR.EndTime 结束时间,TR.Fee 消费金额 +from tbl_record TR join tbl_card TC on TR.CardID = TC.ID +where DATENAME (DW,BeginTime)='星期六' or DATENAME (DW,BeginTime)='星期日' + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select TC.ID 卡号,TC.UserName 用户名,TR.ComputerID 机器编号,TR.BeginTime 开始时间,TR.EndTime 结束时间,TR.Fee 消费金额 +from tbl_record TR join tbl_card TC on TR.CardID = TC.ID +where DATEDIFF(HH,EndTime,BeginTime)>12 + +--10. 查询出消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + +select TC.ID 卡号,TC.UserName 用户名,TR.ComputerID 机器编号,TR.BeginTime 开始时间,TR.EndTime 结束时间,TR.Fee 消费金额 +from tbl_record TR join tbl_card TC on TR.CardID = TC.ID +where Fee not in (select top 3 Fee from tbl_record order by Fee desc)order by Fee desc + + + diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\230\256\345\273\272\351\234\226/SQLQuery5.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\230\256\345\273\272\351\234\226/SQLQuery5.sql" new file mode 100644 index 0000000000000000000000000000000000000000..679639ff97bb7d9fc60ee448547e96d6e36f8d52 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\230\256\345\273\272\351\234\226/SQLQuery5.sql" @@ -0,0 +1,103 @@ +use master +go + +create database TBL +on +( +name='TBL', +filename='D:\TBL.mdf', +size=5MB, +MAXsize=50MB, +filegrowth=50% +) + +log on +( +name='TBL_log', +filename='D:\TBL_log.ldf', +size=5MB, +maxsize=50MB, +filegrowth=50% +) +go + +use TBL +go + +create table Card +( + ID nvarchar(20) primary key , + PassWord nvarchar(10), + Balance int , + UserName nvarchar(10) +) + +create table Computer +( + ID nvarchar(20) primary key, + OnUse char(1) check(OnUse in (1,0)), + note text , +) + +create table Record +( + ID int primary key , + Cardld nvarchar(20) references Card(ID), + ComputerID nvarchar(20) references Computer(ID), + BeginTime datetime, + Endtime datetime, + Fee int +) + +insert into Card values +('0023_ABC','555',98,'张军'),('0025_bbd','abe',300,'朱俊'),('036__CCD','何柳',100,'何柳'), +('0045_YGR','0045_YGR',58,'证验'),('0078_RJV','55885fg',600,'校庆'),('0089','zhang',134,'张峻') + + +insert into Computer values +('02','0','25555'),('03','1','55555'),('04','0','66666'),('05','1','88888'), +('06','0','688878'),('B01','0','558558') + +insert into Record values +(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-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:0',6), +(47,'0023_ABC','03','2006-12-23 15:26:00','2006-12-23 22:55:00',50), +(48,'0023_ABC','03','2007-1-06 15:26:00','2007-1-06 22:55:0',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',3), +(65,'0025_bbd','02','2006-12-28 18:00:00','006-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 Card +select * from Computer +select * from Record +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 + Select R.Cardld 卡号,C.UserName 用户名,R.ComputerID 机器编号,R.BeginTime 开始时间,R.Endtime 结束时间,R.Fee 消费金额 from Record R + inner join Card C on R.Cardld=C.ID where UserName='张军' order by Fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 + Select ComputerID,count(ComputerID) 上网次数,sum(Fee) 总金额 from Record group by ComputerID +--3. 查询出所有已经使用过的上网卡的消费总金额 + Select Cardld 卡号,Sum(Fee) 消费总金额 from Record group by Cardld +--4. 查询出从未消费过的上网卡的卡号和用户名 + Select C.ID 卡号,C.UserName 用户名 from Record R right join Card C on R.Cardld=C.ID where fee is null or Fee='' + +--5. 将密码与用户名一样的上网卡信息查询出来 + Select A.* from Card A,Card B where A.ID=B.PassWord +--6. 查询出使用次数最多的机器号和使用次数 + Select C.ID 机器号,count(Cardld) 使用次数 from Record R inner join Computer C on R.ComputerID=C.ID group by C.ID + having C.ID=(Select top 1 ComputerID from Record group by ComputerID order by count(Cardld) desc ) +--7. 查询出卡号是以'ABC'结尾的用户卡号,名,上网的机器号和消费金额 + Select C.ID 用户卡号,C.UserName 用户名,R.ComputerID 机器号, sum(Fee) 消费总金额 from Card C inner join Record R on C.ID=R.Cardld where C.ID like '%ABC' group by C.ID,C.UserName,R.ComputerID +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + Select C.ID 卡号,C.UserName 用户名,R.ComputerID 机器号,R.BeginTime 开始时间,R.Endtime 结束时间,fee 消费金额 from Record R + inner join Card C on R.Cardld=C.ID where datename(DW,R.BeginTime)='星期六' or datename(DW,R.BeginTime)='星期日' +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + select R.Cardld 卡号,C.UserName 用户名,R.ComputerID 机器号, R.BeginTime 开始时间,R.Endtime 结束时间,fee 消费金额 from Record R + inner join Card C on R.Cardld=C.ID where datediff(hh,BeginTime,EndTime)>12 + --10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + Select top 3 R.Cardld 卡号,C.UserName 用户名,R.ComputerID 机器号, R.BeginTime 开始时间, R.Endtime 结束时间, fee 消费金额 from Record R + inner join Card C on R.Cardld=C.ID order by Fee desc + diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\256\207\347\277\224/SQLQuery10.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\256\207\347\277\224/SQLQuery10.sql" new file mode 100644 index 0000000000000000000000000000000000000000..030df65d82dd56b846f0b62669788540e1e3de82 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\256\207\347\277\224/SQLQuery10.sql" @@ -0,0 +1,143 @@ +use master +go + +create database InternetBar +on +( + name='InternetBar', + filename='D:\SqlWork\InternetBar\InternetBar.mdf', + size=5mb, + maxsize=20mb, + filegrowth=5mb +) +log on +( + name='InternetBar_log', + filename='D:\SqlWork\InternetBar\InternetBar.ldf', + size=5mb, + maxsize=20mb, + filegrowth=5mb +) + +use [InternetBar] +go + + +create table tbl_card +( + id varchar(10) primary key, + [passWord] nvarchar(20) check(len(passWord) between 2 and 20), + balance decimal(10,1), + userName nvarchar(20) +) +go + +create table tbl_computer +( + id varchar(10) primary key, + onUse int check(onUse in (0, 1)), + note text +) +go + +create table tbl_record +( + id varchar(10) primary key, + cardId varchar(10) references tbl_card(id), + ComputerId varchar(10) references tbl_computer(id), + beginTime smalldatetime, + endTime smalldatetime, + fee decimal(10,1) +) +go + + + +insert into tbl_card 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, '张峻') +go + +insert into tbl_computer values +('02', 0, '25555'), +('03', 1, '55555'), +('04', 0, '66666'), +('05', 1, '88888'), +('06', 0, '688878'), +('B01', 0, '558558') +go + +insert into tbl_record values +('23', '0078_RJV', 'B01', '2007-07-15 19:00:00', '2007-07-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', 30), +('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) +go + +select * from [dbo].[tbl_card] +select * from [dbo].[tbl_computer] +select * from [dbo].[tbl_record] + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 + + select tc.id 卡号, userName 用户名, ComputerId 机器编号, beginTime 开放时间, endTime 结束时间, fee 消费金额 + from tbl_card tc join tbl_record tr on tc.id = tr.cardId where tc.userName = '张军' order by tr.fee desc + +--2. 查询出每台机器上的上网次数和消费的总金额 + + select ComputerId 电脑编号, count(*)上网次数, sum(fee) 消费金额 from tbl_record tb group by ComputerId + +--3. 查询出所有已经使用过的上网卡的消费总金额 + + select cardId 当次上网卡号, sum(fee) 消费总金额 from tbl_record tr group by cardId + +--4. 查询出从未消费过的上网卡的卡号和用户名 + + select tc.id 卡号, tc.userName 用户名 from tbl_card tc left join tbl_record tr on tr.cardId = tc.id where tr.id is null + +--5. 将密码与用户名一样的上网卡信息查询出来 + + select * from tbl_card tc1 join tbl_card tc2 on (tc1.id = tc1.[passWord]) and (tc2.id = tc2.[passWord]) + +--6. 查询出使用次数最多的机器号和使用次数 + + select ComputerId 机器编号, count(*) 使用次数 from tbl_record tr group by ComputerId + having count(*) = + (select max(t0.使用次数) from (select ComputerId, count(*) 使用次数 from tbl_record group by ComputerId) t0) + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 + + select tc.id, tc.userName, tr.ComputerId, tr.fee from tbl_record tr join tbl_card tc on tr.cardId = tc.id where tr.cardId like '%ABC' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + + select tr.id, tc.userName, tr.ComputerId , tr.beginTime, tr.endTime, tr.fee from tbl_record tr join tbl_card tc on tr.cardId = tc.id + where datename(DW, tr.beginTime) in ( '星期日','星期六') + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + + select tr.id, tc.userName, tr.ComputerId , tr.beginTime, tr.endTime, tr.fee from tbl_record tr join tbl_card tc on tr.cardId = tc.id + where datediff(HH, tr.beginTime, tr.endTime) > 12 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + + select c.id,userName,Computerid,beginTime,endTime,fee from tbl_card c inner join tbl_record r on c.id=r.cardid + where fee not in(select top 3 fee from tbl_record order by fee desc ) + + + select c.id,userName,Computerid,beginTime,endTime,fee from tbl_card c inner join tbl_record r on c.id=r.cardid + except + select top 3 c.id,userName,Computerid,beginTime,endTime,fee from tbl_card c inner join tbl_record r on c.id=r.cardid order by fee desc +select * from tbl_card +select * from tbl_computer +select * from tbl_record diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\200\235\351\224\220/.keep" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\200\235\351\224\220/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\200\235\351\224\220/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\200\235\351\224\220/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..9b1a9f2f15d9d5a818a4db4cf100baf3a9da2beb --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\200\235\351\224\220/SQLQuery2.sql" @@ -0,0 +1,211 @@ +use master +go + +if exists(select * from sys.databases where name = 'cybercafe_charge_sys') + drop database cybercafe_charge_sys +go + +create database cybercafe_charge_sys +go + +use cybercafe_charge_sys +go + +create table tbl_card +( + id varchar(10) primary key, + [passWord] nvarchar(20) check(len(passWord) between 2 and 20), + balance decimal(10,1), + userName nvarchar(20) +) +go + +create table tbl_computer +( + id varchar(10) primary key, + onUse int check(onUse in (0, 1)), + note text +) +go + +create table tbl_record +( + id varchar(10) primary key, + cardId varchar(10) references tbl_card(id), + ComputerId varchar(10) references tbl_computer(id), + beginTime smalldatetime, + endTime smalldatetime, + fee decimal(10,1) +) +go + +insert into tbl_card 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, '张峻') +go + +insert into tbl_computer values +('02', 0, '25555'), +('03', 1, '55555'), +('04', 0, '66666'), +('05', 1, '88888'), +('06', 0, '688878'), +('B01', 0, '558558') +go + +insert into tbl_record values +('23', '0078_RJV', 'B01', '2007-07-15 19:00:00', '2007-07-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', 30), +('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) +go + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select + tc.id '卡号', tc.userName '用户名', tr.ComputerId '机器编号', tr.beginTime '开始时间', tr.endTime '结束时间', tr.fee '消费金额' +from + tbl_card tc +join + tbl_record tr +on + tc.id = tr.cardId +where + tc.userName = '张军' +order by + tr.fee desc + +--2. 查询出每台机器上的上网次数和消费的总金额 +select + ComputerId, count(*) '上网次数', sum(fee) '消费总金额' +from + tbl_record +group by + ComputerId + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select + cardId, sum(fee) '消费总金额' +from + tbl_record +group by + cardId + +--4. 查询出从未消费过的上网卡的卡号和用户名 +select + tc.id, tc.userName +from + tbl_card tc +left join + tbl_record tr +on + tr.cardId = tc.id +where + tr.id is null + +--5. 将密码与用户名一样的上网卡信息查询出来 +select + * +from + tbl_card t1 +join + tbl_card t2 +on + (t1.id = t2.id) and (t1.[passWord] = t2.userName) + + +--6. 查询出使用次数最多的机器号和使用次数 +-- 1 +select + ComputerId, count(*) '使用次数' +from + tbl_record +group by + ComputerId +having + count(*) = (select max(t.使用次数) from (select ComputerId, count(*) '使用次数' from tbl_record group by ComputerId) t) + +--2 +--create view v_count +--as +--select +-- ComputerId, count(*) '使用次数' +--from +-- tbl_record +--group by +-- ComputerId +--go + +--select +-- * +--from +-- v_count v1 +--where +-- v1.使用次数 = (select max(使用次数) from v_count) + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select + tc.id, tc.userName, tr.ComputerId, tr.fee +from + tbl_record tr +join + tbl_card tc +on + tr.cardId = tc.id +where + tr.cardId like '%ABC' + + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select + tc.id, tc.userName, tr.ComputerId, tr.beginTime, tr.endTime, tr.fee +from + tbl_record tr +join + tbl_card tc +on + tr.cardId = tc.id +where + DATENAME(dw, tr.beginTime) in ('星期六','星期日') + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select + tc.id, tc.userName, tr.ComputerId, tr.beginTime, tr.endTime, tr.fee +from + tbl_record tr +join + tbl_card tc +on + tr.cardId = tc.id +where + DATEDIFF(HH, tr.beginTime, tr.endTime) > 12 + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select + tc.id, tc.userName, tr.ComputerId, tr.beginTime, tr.endTime, tr.fee +from + tbl_record tr +join + tbl_card tc +on + tr.cardId = tc.id +where + tr.fee in ( + select + distinct top 3 fee + from + tbl_record + order by + fee desc + ) +order by + tr.fee desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\277\233\345\220\211/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\277\233\345\220\211/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..61940cef6d64c4643d8520225ad8bf90d68dd035 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\277\233\345\220\211/SQLQuery1.sql" @@ -0,0 +1,120 @@ +--棣栧厛鍒涘缓缃戝惂璁¤垂绯荤粺鐨勬暟鎹簱锛岃〃缁撴瀯鍜屾暟鎹2.bmp鎵绀猴紝琛ㄧ殑璇存槑濡備笅锛 +create database ajiji +go +use ajiji +go + +--琛ㄤ竴涓轰笂缃戝崱淇℃伅琛(tbl_card) +create table tbl_card +( +id varchar(20) primary key, --鍗″彿 +passWord char(18) not null, --瀵嗙爜 +balance money , --鍗′笂鐨勪綑棰 +userName nvarchar(10), --璇ヤ笂缃戝崱鎵灞炵殑鐢ㄦ埛鍚 +) + +--琛2涓虹綉鍚ф満鍣ㄨ鏄庤〃(tbl_computer) +create table tbl_computer +( +id varchar(20) primary key, --鏈哄櫒缂栧彿锛屼富閿 +onUse char(1) check (onUse in (1,0)),--璇ユ満鍣ㄦ槸鍚︽鍦ㄤ娇鐢紝0涓烘湭浣跨敤锛1涓烘鍦ㄤ娇鐢 +note text , --璇ユ満鍣ㄧ殑璇存槑 +) + +--琛3涓轰笂缃戣褰曡〃(tbl_record)锛 +create table tbl_record +( +id char(10) primary key, --璁板綍缂栧彿 +cardId varchar(20) references tbl_card(id), --鏈涓婄綉鐨勫崱鍙凤紝澶栭敭 +ComputerId varchar(20) references tbl_computer(id), --鏈涓婄綉璁板綍鎵浣跨敤鐨勬満鍣ㄥ彿锛屽閿 +beginTime datetime , --鏈涓婄綉璁板綍鐨勫紑濮嬫椂闂 +endTime datetime , --鏈涓婄綉璁板綍鐨勭粨鏉熸椂闂 +fee money , --鏈涓婄綉鐨勮垂鐢 +) + +insert into tbl_card 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 values +('02','0','25555'),('05','1','88888'), +('03','1','55555'),('06','0','688878'), +('04','0','66666'),('B01','0','558558') + +insert into tbl_record values +('23','0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20'), +('34','0025_bbd','03','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-1-6 15:26:00','2007-1-6 22:55:00','6'), +('55','0023_ABC','03','2006-7-21 15:26:00','2006-7-21 22:55:00','50'), +('64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','3'), +('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_record +select * from tbl_card +select * from tbl_computer +--璇峰畬鎴愪互涓嬮鐩細 +--1. 鏌ヨ鍑虹敤鎴峰悕涓 '寮犲啗' 鐨勪笂缃戝崱鐨 涓婄綉璁板綍锛岃姹傛樉绀 鍗″彿锛 鐢ㄦ埛鍚 锛屾満鍣ㄧ紪鍙 銆佸紑濮嬫椂闂淬佺粨鏉熸椂闂达紝鍜屾秷璐归噾棰濓紝骞舵寜娑堣垂閲戦闄嶅簭鎺掑垪 +select * from tbl_record r +join tbl_card c on r.cardId=c.id +join tbl_computer co on r.ComputerId=co.id +where userName='寮犲啗' order by fee desc + +--2. 鏌ヨ鍑 姣忓彴鏈哄櫒涓婄殑 涓婄綉娆℃暟 鍜 娑堣垂鐨勬婚噾棰 +select ComputerId ,count(ComputerId),sum(fee) from tbl_record r +join tbl_card c on r.cardId=c.id +join tbl_computer co on r.ComputerId=co.id +group by ComputerId + +--3. 鏌ヨ鍑 鎵鏈夊凡缁忎娇鐢ㄨ繃鐨勪笂缃戝崱鐨 娑堣垂鎬婚噾棰 +select cardId,sum(fee) from tbl_record r +join tbl_card c on r.cardId=c.id +where fee is not null +group by cardId +--4. 鏌ヨ鍑 浠庢湭娑堣垂杩囩殑涓婄綉鍗$殑 鍗″彿 鍜 鐢ㄦ埛鍚 +select c.id,userName from tbl_record r +right join tbl_card c on r.cardId=c.id +where fee is null +group by c.id,userName +--5. 灏嗗瘑鐮佷笌鐢ㄦ埛鍚嶄竴鏍风殑涓婄綉鍗′俊鎭煡璇㈠嚭鏉 +select t1.* from tbl_card t1 +join tbl_card t2 on t1.passWord=t2.userName +--6. 鏌ヨ鍑轰娇鐢ㄦ鏁版渶澶氱殑鏈哄櫒鍙峰拰浣跨敤娆℃暟 +select top 1 ComputerId,count(*) 浣跨敤娆℃暟 from tbl_record group by ComputerId order by 浣跨敤娆℃暟 DESC +--7. 鏌ヨ鍑哄崱鍙锋槸浠'ABC'缁撳熬鐨勫崱鍙凤紝鐢ㄦ埛鍚嶏紝涓婄綉鐨勬満鍣ㄥ彿鍜屾秷璐归噾棰 +select * from tbl_card c +join tbl_record r on c.id=r.cardId +where c.id like '%ABC' +--8. 鏌ヨ鍑烘槸鍛ㄥ叚銆佸懆澶╀笂缃戠殑璁板綍锛岃姹傛樉绀轰笂缃戠殑鍗″彿锛岀敤鎴峰悕锛屾満鍣ㄥ彿锛屽紑濮嬫椂闂淬佺粨鏉熸椂闂村拰娑堣垂閲戦 +select cardId 鍗″彿,userName 鐢ㄦ埛鍚,ComputerId 鏈哄櫒缂栧彿,beginTime 寮濮嬫椂闂,endTime 缁撴潫鏃堕棿,fee 娑堣垂閲戦 from tbl_record +join tbl_card on tbl_record.cardId = tbl_card.id +where DATENAME(DW,beginTime)='鏄熸湡鍏' or DATENAME(DW,beginTime)='鏄熸湡鏃' + +--9. 鏌ヨ鎴愪竴娆′笂缃戞椂闂磋秴杩12灏忔椂鐨勭殑涓婄綉璁板綍锛岃姹傛樉绀轰笂缃戠殑鍗″彿锛岀敤鎴峰悕锛屾満鍣ㄥ彿锛屽紑濮嬫椂闂淬佺粨鏉熸椂闂村拰娑堣垂閲戦 +select cardId 鍗″彿,userName 鐢ㄦ埛鍚,ComputerId 鏈哄櫒缂栧彿,beginTime 寮濮嬫椂闂,endTime 缁撴潫鏃堕棿,fee 娑堣垂閲戦 from tbl_record +join tbl_card on tbl_record.cardId = tbl_card.id +where datediff(HH,beginTime,endTime)>12 + +select * from tbl_record where endtime-beginTime>12 + +--10. 鏌ヨ鍑烘秷璐归噾棰濇帓鍒楀墠涓夊悕(鏈楂)鐨勪笂缃戣褰曪紝瑕佹眰鏄剧ず涓婄綉鐨勫崱鍙凤紝鐢ㄦ埛鍚嶏紝鏈哄櫒鍙凤紝寮濮嬫椂闂淬佺粨鏉熸椂闂村拰娑堣垂閲戦 +select cardId 鍗″彿,userName 鐢ㄦ埛鍚,ComputerId 鏈哄櫒缂栧彿,beginTime 寮濮嬫椂闂,endTime 缁撴潫鏃堕棿,fee 娑堣垂閲戦 +from tbl_record join tbl_card on tbl_record.cardId = tbl_card.id +where fee in (select top 3 fee from tbl_record order by fee DESC) +order by fee DESC + +--11. 鏌ヨ闄ゆ秷璐归噾棰濇帓鍒楀墠涓夊悕(鏈楂)鐨勪笂缃戣褰曪紝瑕佹眰鏄剧ず涓婄綉鐨勫崱鍙凤紝鐢ㄦ埛鍚嶏紝鏈哄櫒鍙凤紝寮濮嬫椂闂淬佺粨鏉熸椂闂村拰娑堣垂閲戦 +select cardId 鍗″彿,userName 鐢ㄦ埛鍚,ComputerId 鏈哄櫒缂栧彿,beginTime 寮濮嬫椂闂,endTime 缁撴潫鏃堕棿,fee 娑堣垂閲戦 from tbl_record +join tbl_card on tbl_record.cardId = tbl_card.id +where fee not in (select top 3 fee from tbl_record order by fee DESC) +order by fee DESC + +select * from tbl_record order by fee DESC + diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\251\267\350\212\263/.keep" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\251\267\350\212\263/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\251\267\350\212\263/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\251\267\350\212\263/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6c89833aa3a9d708f417c395c43d225eda99548b --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\251\267\350\212\263/SQLQuery2.sql" @@ -0,0 +1,171 @@ + +--首先创建网吧计费系统的数据库,表结构和数据如2.bmp所示,表的说明如下: +create database wangba +go +use wangba +go +--表一为上网卡信息表(tbl_card) +--id: 卡号,主键 +--passWord:密码 +--balance:卡上的余额 +--userName:该上网卡所属的用户名 +create table tbl_card +( +id_card char(30) primary key , +passWord nvarchar(20), +balance int, +userName nvarchar(50) +) +insert into tbl_card 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,'张峻') + + + +--表2为网吧机器说明表(tbl_computer) +--id:机器编号,主键 +--onUse:该机器是否正在使用,0为未使用,1为正在使用 +--note:该机器的说明 +create table tbl_computer +( +id_boot char(30) primary key, +onUse char(2) check(onUse='1' or onUse='0'), +note char(30) +) +insert into tbl_computer values ('02','0','2555'),('03','1','5555'),('04','0','66666'),('05','1','88888'), +('06','0','68878'),('B01','0','558558') + + + +--表3为上网记录表(tbl_record): +--id:记录编号,主键 +--cardId:本次上网的卡号,外键 +--ComputerId:本次上网记录所使用的机器号,外键 +--beginTime:本次上网记录的开始时间 +--endTime:本次上网记录的结束时间 +--fee:本次上网的费用 +create table tbl_record +( +id_record char(30) primary key , +cardId char(30) references tbl_card(id_card), +ComputerId char(30) references tbl_computer(id_boot), +beginTime datetime, +endTime datetime, +fee int +) +insert into tbl_record values +('23','0078_RJV','B01','2007-07-15 19:00:00','2007-07-15 21:00:00','20'), +('34','0025_bbd','02','2006-12-25 18:00:00','2007-07-15 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-16 15:26:00','2007-01-16 22:55:00','6'), +('55','0023_ABC','03','2007-07-21 15:26:00','2007-07-21 22:55:00','50'), +('64','0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00','3'), +('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') + + + + +--请完成以下题目: +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费 +--金额降序排列 +select id_card,userName,id_boot,beginTime,endTime,fee from tbl_card car inner join tbl_record rec on car.id_card=rec.cardId + inner join +tbl_computer com on com.id_boot=rec.ComputerId where userName='张军'order by fee desc + +select * from tbl_card +select * from tbl_computer +select * from tbl_record + +--2. 查询出每台机器上的上网次数和消费的总金额 +select ComputerId 电脑编号,count(*),sum(fee) from tbl_computer com inner join tbl_record rec on com.id_boot=rec.ComputerId group by ComputerId +--3. 查询出所有已经使用过的上网卡的消费总金额 +select id_card 上网卡编号, sum(fee) from tbl_record rec inner join tbl_card car on rec.cardId=car.id_card group by id_card +--4. 查询出从未消费过的上网卡的卡号和用户名 +select id_card 上网卡编号, userName from tbl_record rec right join tbl_card car on rec.cardId=car.id_card where fee is null group by id_card,userName +--5. 将密码与用户名一样的上网卡信息查询出来 +select A.* from tbl_card A INNER JOIN tbl_card B on A.id_card=B.id_card and A.passWord=B.userName and A.userName=B.passWord +--6. 查询出使用次数最多的机器号和使用次数 +select TOP 1 ComputerId,COUNT(*) 使用次数 from tbl_record group by ComputerId ORDER BY 使用次数 DESC +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select id_card,userName,ComputerId,fee from tbl_card car inner join tbl_record rec on car.id_card=rec.cardId where id_card like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select DATENAME(DW,GETDATE()) +select * from tbl_record where datename (dw,beginTime)='星期日'or datename(dw,beginTime)='星期六' +--select * from tbl_record where datename(dw,列名)='' +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select * from tbl_record where endTime-beginTime>12 + +select cardId 卡号,userName 用户名,ComputerId 机器编号,beginTime 开始时间,endTime 结束时间,fee 消费金额 from tbl_record +join tbl_card on tbl_record.cardId = tbl_card.id_card +where datediff(HH,BeginTime,Endtime)>12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 * from tbl_record order by fee desc + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git "a/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\274\272/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\274\272/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..6e51500ae71327ba7a30a0e0550e82121f84c3c3 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\274\272/SQLQuery1.sql" @@ -0,0 +1,109 @@ +use master +go +create database TBL +on +( + name='TBL', + filename='D:\SQL\TBL.mdf', + size=5MB, + maxsize=20MB, + filegrowth=10% +) +log on +( + name='TBL_log', + filename='D:\SQL\TBL_log.ldf', + size=5MB, + maxsize=20MB, + filegrowth=10% +) +go + +use TBL +go +create table TBL_Card +( + CardID char(30) primary key not null, + PassWord varchar(50) not null, + Balance money , + UserName nvarchar(20) +) + +create table TBL_Computer +( + CID char(30) primary key , + OnUse char(1) check(OnUse in(1,0)), + note text +) + +create table TBL_Record +( + RID int primary key , + CardID char(30) references TBL_Card(CardID), + CID char(30) references TBL_Computer(CID), + BeginTime datetime, + Endtime datetime, + fee money +) +go + +insert into TBL_Card values +('0023_ABC','555',98,'张军'), +('0025_bbd','abe',300,'朱俊'), +('0036_CCD','何柳',100,'何柳'), +('0045_YGR','00445_YGR',58,'证验'), +('0078_RJV','55885fg',600,'校庆'), +('0089_EDE','zhang',134,'张俊') + +insert into TBL_Computer values +('02','0','25555'), +('03','1','55555'), +('04','0','66666'), +('05','1','88888'), +('06','0','688878'), +('B01','0','558558') + +insert into TBL_Record values +(23,'0078_RJV','B01','2007-07-15 19:00:00','2007-07-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:25:00',50), +(46,'0023_ABC','03','2006-12-23 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',20), +(64,'0045_YGR','04','2006-12-24 18:00:00','2006-12-24 22:00:00',3), +(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) +go + +select * from TBL_Card +select * from TBL_Computer +select * from TBL_Record +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select C.CardID 卡号 , UserName 用户名 , CID 机器编号 , BeginTime 开始时间 , Endtime 结束时间 , fee 消费金额 from TBL_Record R inner join TBL_Card C on R.CardID=C.CardID where UserName='张军' order by fee DESC +--2. 查询出每台机器上的上网次数和消费的总金额 +select CID 机器编号 , COUNT(*) 上网次数 , SUM(fee) 总金额 from TBL_Record R group by CID +--3. 查询出所有已经使用过的上网卡的消费总金额 +select CardID 卡号 , SUM(fee) 总金额 from TBL_Record group by CardID +--4. 查询出从未消费过的上网卡的卡号和用户名 +select C.CardID 卡号 , UserName 用户名 , RID from TBL_Record R right join TBL_Card C on R.CardID=C.CardID group by C.CardID,UserName,RID having RID is null or RID='' +--5. 将密码与用户名一样的上网卡信息查询出来 +select A.* from TBL_Card A,TBL_Card B where A.PassWord=B.UserName +--6. 查询出使用次数最多的机器号和使用次数 +--select top 1 COUNT(*) from TBL_Record group by CID order by COUNT(*) DESC +select CID 机器号 , COUNT(*) 使用次数 from TBL_Record group by CID having COUNT(*)=(select top 1 COUNT(*) from TBL_Record group by CID order by COUNT(*) DESC) +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +select C.CardID 卡号 , UserName 用户名 , CID 机器编号 , fee 消费金额 from TBL_Record R inner join TBL_Card C on R.CardID=C.CardID where C.CardID like '%ABC' +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select C.CardID 卡号 , UserName 用户名 , CID 机器编号 , BeginTime 开始时间 , Endtime 结束时间 , fee 消费金额 from TBL_Record R inner join TBL_Card C on R.CardID=C.CardID where datename(weekday,BeginTime)='星期六' or datepart(weekday,BeginTime)=1 +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select C.CardID 卡号 , UserName 用户名 , CID 机器编号 , BeginTime 开始时间 , Endtime 结束时间 , fee 消费金额 from TBL_Record R inner join TBL_Card C on R.CardID=C.CardID where DATEDIFF(HH,BeginTime,Endtime)>12 +--10. 查询出消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select C.CardID 卡号 , UserName 用户名 , CID 机器编号 , BeginTime 开始时间 , Endtime 结束时间 , fee 消费金额 from TBL_Record R +inner join TBL_Card C on R.CardID=C.CardID +where RID not in (select top 3 RID from TBL_Record order by fee DESC) +group by C.CardID,UserName,CID,BeginTime,Endtime,fee +--11. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 RID from TBL_Record order by fee DESC +select C.CardID 卡号 , UserName 用户名 , CID 机器编号 , BeginTime 开始时间 , Endtime 结束时间 , fee 消费金额 from TBL_Record R inner join TBL_Card C on R.CardID=C.CardID where RID in (select top 3 RID from TBL_Record order by fee DESC) +group by C.CardID , UserName , CID , BeginTime , Endtime , fee \ No newline at end of file