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..54e3242ddba60e2083bdff8bd649adf22aac93cb --- /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..8effde72659b104d028102397bbff0738904db9b --- /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..77bb6cfe4b2ef0ba746a8642049c6bd556aeba4a --- /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..31b8c6d89e1aaba23e95e8e0fd7512f373bf0420 --- /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..8eb6244db5a92662d37f49d160015189ed686dbe --- /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..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/\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..bc933bccff548bf40dcf92381672e8d45176551e --- /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..88de6803133bb75f2265de431b16868d1a53698d --- /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..fd3626867d912df92a30a10e5a0762905477a9fc --- /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..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/\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..a2d371c99e14170dcf5da6f33754ceeca05e3bc6 --- /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..3fdcf7815dd230a27caf19c00f6d0aa6917530e2 --- /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..14ce2cc70bd04a4781d6e700f96465c734161480 --- /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..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/\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..06cfa1e8690d4d45db826eaca4ad6eb5c88fab93 --- /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..44605deff454521a756282fb7835183f33cc6d73 --- /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..360b72f24b4d9c1fce2fcf9b2f3e9fe535b20c1d --- /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..e6a0f8b5238ba028853598c676d8b7b0c80508a7 --- /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..1fd8c3ff4bbb0b84b66a9f56eb9ad871d2ff4a85 --- /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..4466051268405b9450f2b1df1ec08d479bbfb0d2 --- /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..734c04729f33c5ba64fe4f82ad5a08dd1df4a599 --- /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..eca5106bba6b4c7cb922d9ee350fa2ed8304b793 --- /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..6b52a651f9a88f8a972329aa34cb5d181992d577 --- /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..648e0a8ff56695e947956090c07274b467467114 --- /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..5aa21c380826310caaa66c33c1e95ed8d85fbb86 --- /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..2242e2dce3c253a6c9ad415f628fb50812217df1 --- /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..61e3b49d9a851183b4501d94c7835a9d4532d6ec --- /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..5aa21c380826310caaa66c33c1e95ed8d85fbb86 --- /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..9acc02cb19f43b44a1131776b181800c6059f5f2 --- /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\270\211\346\254\241\350\257\276\344\275\234\344\270\232/01.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/01.sql" new file mode 100644 index 0000000000000000000000000000000000000000..98e4aabd8bb572a130a7292578afb585574a8f7d --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/01.sql" @@ -0,0 +1,58 @@ +use master +go +create database GoodsDB +on +( +name = 'GoodsDB', +filename = 'D:\text.mdf', +size = 5mb, +maxsize = 50mb, +filegrowth = 10% +) +log on +( +name = 'GoodsDB_log', +filename = 'D:\text_log.ldf', +size = 5mb, +maxsize = 50mb, +filegrowth = 10% +) +use GoodsDB +go +create table GoodsType +( +TypeID int primary key identity(1,1) not null, +TypeName nvarchar(20) not null +) +create table GoodsInfo +( +GoodsID int primary key identity(1,1) not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20) not null, +GoodsMoney money not null, +TypeID int references GoodsType(TypeID) not null +) +insert into GoodsType values +('1.服装内衣'), +('2.鞋包配饰'), +('3.手机数码') +select * from GoodsType +insert into GoodsInfo values +('提花小西装', '红色', '菲曼琪', 300, 1), +('百搭短裤','绿色','哥弟',100,1), +( '无袖背心', '白色', '阿依莲', 700,1), +( '低帮休闲鞋', '红色', '菲曼琪', 900, 2), +('中跟单鞋', '绿色', '哥弟', 400, 2), +( '平底鞋', '白色',' 阿依莲',200,2), +( '迷你照相机', '红色','尼康', 500,3), +( '硬盘', '黑色','希捷', 600, 3), +( '显卡', '黑色', '技嘉', 800, 3) +select * from GoodsInfo +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName,GoodsColor ,max(GoodsMoney) from GoodsInfo group by GoodsName,GoodsColor +select * from GoodsType +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 类型编号,MAX(GoodsMoney) 最高价格,MIN(GoodsMoney) 最高价格,AVG(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..98e4aabd8bb572a130a7292578afb585574a8f7d --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/SQLQuery1.sql" @@ -0,0 +1,58 @@ +use master +go +create database GoodsDB +on +( +name = 'GoodsDB', +filename = 'D:\text.mdf', +size = 5mb, +maxsize = 50mb, +filegrowth = 10% +) +log on +( +name = 'GoodsDB_log', +filename = 'D:\text_log.ldf', +size = 5mb, +maxsize = 50mb, +filegrowth = 10% +) +use GoodsDB +go +create table GoodsType +( +TypeID int primary key identity(1,1) not null, +TypeName nvarchar(20) not null +) +create table GoodsInfo +( +GoodsID int primary key identity(1,1) not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20) not null, +GoodsMoney money not null, +TypeID int references GoodsType(TypeID) not null +) +insert into GoodsType values +('1.服装内衣'), +('2.鞋包配饰'), +('3.手机数码') +select * from GoodsType +insert into GoodsInfo values +('提花小西装', '红色', '菲曼琪', 300, 1), +('百搭短裤','绿色','哥弟',100,1), +( '无袖背心', '白色', '阿依莲', 700,1), +( '低帮休闲鞋', '红色', '菲曼琪', 900, 2), +('中跟单鞋', '绿色', '哥弟', 400, 2), +( '平底鞋', '白色',' 阿依莲',200,2), +( '迷你照相机', '红色','尼康', 500,3), +( '硬盘', '黑色','希捷', 600, 3), +( '显卡', '黑色', '技嘉', 800, 3) +select * from GoodsInfo +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName,GoodsColor ,max(GoodsMoney) from GoodsInfo group by GoodsName,GoodsColor +select * from GoodsType +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 类型编号,MAX(GoodsMoney) 最高价格,MIN(GoodsMoney) 最高价格,AVG(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\215\242\345\256\201/SQLQuery\347\273\203\344\271\2401.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\215\242\345\256\201/SQLQuery\347\273\203\344\271\2401.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b0523bcfe1ecd52fc2f49770cad61ce7206d7470 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\215\242\345\256\201/SQLQuery\347\273\203\344\271\2401.sql" @@ -0,0 +1,68 @@ +use master +go + +create database GoodsDB +on +( + name='GoodsDB', + filename='F:\GoodsDB.mdf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +LOG ON +( + name='GoodsDB_log', + filename='F:\GoodsDB_log.ldf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +go + +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) + +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) + +insert into GoodsType(TypeName) +values('服装内衣'),('鞋包配饰'),('手机数码') + +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) +values('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','白色','菲曼琪',900,2), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) + +select * from GoodsType +select * from GoodsInfo + +--查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称, GoodsColor 商品颜色,max(GoodsMoney) 最贵的商品价格 from GoodsInfo +GROUP BY GoodsName,GoodsColor,GoodsMoney order by max(GoodsMoney) desc + +--按商品类型编号 分组 查询商品 最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型编号,max(GoodsMoney)最高价格,min(GoodsMoney)最低价格,avg(GoodsMoney)平均价格 from GoodsInfo +group by TypeID order by max(GoodsMoney),min(GoodsMoney),avg(GoodsMoney) + +--查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\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\270\211\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\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\345\220\257\350\210\252/SQLQuery17.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\345\220\257\350\210\252/SQLQuery17.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e6faa4da7207cedf279e6415a24d13553cabb998 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\345\220\257\350\210\252/SQLQuery17.sql" @@ -0,0 +1,59 @@ +use master +go +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\GoodsDB.mdf', + size=10, + maxsize=100, + filegrowth=10% +) +log on +( + name='GoodsDB_log', + filename='D:\GoodsDB_log.mdf', + size=10, + maxsize=100, + filegrowth=10% +) +go +use GoodsDB +go +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) + +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) +insert into GoodsType values ('服装内衣'),('鞋包配饰'),('手机数码') +insert into GoodsInfo values ('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1),('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2),('中跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2),('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3),('显卡','黑色','技嘉',800,3) + +--3、查询价格最贵的商品名称,商品名称和商品价格,要求使用别名显示列名 + select GoodsName 商品名称,GoodsColor 商品名称,GoodsMoney 商品价格 from GoodsInfo group by GoodsName,GoodsColor,GoodsMoney + having GoodsMoney =(select max(GoodsMoney) from GoodsInfo) + + select top 1 GoodsName 商品名称,GoodsColor 商品名称,GoodsMoney 商品价格 from GoodsInfo group by GoodsName,GoodsColor,GoodsMoney + order by GoodsMoney desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + select TypeID,max(GoodsMoney),min(GoodsMoney),avg(GoodsMoney) from GoodsInfo + group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 + + select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\345\220\257\350\210\252/SQLQuery18.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\345\220\257\350\210\252/SQLQuery18.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b1e6ae56ac7734f96fed8df9a1bc0df632267e0e --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\345\220\257\350\210\252/SQLQuery18.sql" @@ -0,0 +1,57 @@ +use master +go +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5, + maxsize=50, + filegrowth=1 +)log on +( + name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5, + maxsize=50, + filegrowth=1 +) +go +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int primary key identity(1,1) not null, + type_name varchar(50) unique not null +) + +create table HOUSE +( + house_id int primary key identity(1,1) not null, + house_name varchar(50), + house_price float default(0), + type_id int references HOUSE_TYPE(type_id) +) + +insert into HOUSE_TYPE values ('小户型'),('经济型'),('别墅') +insert into HOUSE values ('1号房',500,1),('2号房',1000,2),('3号房',5000,3), +('4号房',600,1),('5号房',1200,2),('6号房',6000,3) + +--查询所有房屋信息 + select * from HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 + select * from HOUSE_TYPE where type_name like '%型%' + +--查询出房屋的名称和cc,并且按照租金降序排序 + select house_name 房屋名称,house_price 租金 from HOUSE order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 + select house_name 房屋名称,type_name 房屋类型 from HOUSE h inner join HOUSE_TYPE ht on h.type_id=ht.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 + select top 1 house_price 租金,house_name 房屋名称 from HOUSE order by house_price desc + +select * from HOUSE +select * from HOUSE_TYPE \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\345\220\257\350\210\252/SQLQuery19.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\345\220\257\350\210\252/SQLQuery19.sql" new file mode 100644 index 0000000000000000000000000000000000000000..52bafd0417ec9d498ef4a3700c46cb6eab645ac0 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\345\220\257\350\210\252/SQLQuery19.sql" @@ -0,0 +1,50 @@ +use master +go +create database StarManagerDB +on +( + name='StarManagerDB', + filename='D:\StarManagerDB.mdf', + size=5, + maxsize=50, + filegrowth=1 +) +log on +( + name='StarManagerDB_log', + filename='D:\StarManagerDB_log.ldf', + size=5, + maxsize=50, + filegrowth=1 +) +go +use StarManagerDB +go +create table StarType +( + T_NO int primary key identity(1,1) not null, + T_NAME nvarchar(20) +) + +create table StarInfo +( + S_NO int primary key identity(1,1) not null, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) + +) +insert into StarType values ('体育明星'),('IT明星'),('相声演员') + +insert into StarInfo values ('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',1),('蔡景现',40,'敲代码','中国',2) +,('马斯克',36,'造火箭','外星人',2),('郭德纲',50,'相声','中国',3),('黄铮',41,'拼多多','中国',2) + +--3、查询年龄最大的3个明星的 ,特技和籍贯信息,要求使用别名显示列名。 + select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo order by S_AGE desc +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 + select S_T_NO, count(*) 明星人数,avg(S_AGE) 平均年龄 from StarInfo group by S_T_NO having COUNT(*)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 + select top 1 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo order by S_AGE desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\350\257\232/\344\275\234\344\270\2321.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\350\257\232/\344\275\234\344\270\2321.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e80b49c98b6c0c6fb8d1963fb78f75bdcdd35283 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\350\257\232/\344\275\234\344\270\2321.sql" @@ -0,0 +1,59 @@ +use master +go +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\GoodsDB.mdf', + size=10, + maxsize=100, + filegrowth=10% +) +log on +( + name='GoodsDB_log', + filename='D:\GoodsDB_log.mdf', + size=10, + maxsize=100, + filegrowth=10% +) +go +use GoodsDB +go +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) + +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) +insert into GoodsType values ('服装内衣'),('鞋包配饰'),('手机数码') +insert into GoodsInfo values ('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1),('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2),('中跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2),('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3),('显卡','黑色','技嘉',800,3) + +--3、查询价格最贵的商品名称,商品名称和商品价格,要求使用别名显示列名 + select GoodsName 商品名称,GoodsColor 商品名称,GoodsMoney 商品价格 from GoodsInfo group by GoodsName,GoodsColor,GoodsMoney + having GoodsMoney =(select max(GoodsMoney) from GoodsInfo) + + select top 1 GoodsName 商品名称,GoodsColor 商品名称,GoodsMoney 商品价格 from GoodsInfo group by GoodsName,GoodsColor,GoodsMoney + order by GoodsMoney desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + select TypeID,max(GoodsMoney),min(GoodsMoney),avg(GoodsMoney) from GoodsInfo + group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 + + select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\350\257\232/\344\275\234\344\270\2322.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\350\257\232/\344\275\234\344\270\2322.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b1e6ae56ac7734f96fed8df9a1bc0df632267e0e --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\350\257\232/\344\275\234\344\270\2322.sql" @@ -0,0 +1,57 @@ +use master +go +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\HOUSE_DB.mdf', + size=5, + maxsize=50, + filegrowth=1 +)log on +( + name='HOUSE_DB_log', + filename='D:\HOUSE_DB_log.ldf', + size=5, + maxsize=50, + filegrowth=1 +) +go +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int primary key identity(1,1) not null, + type_name varchar(50) unique not null +) + +create table HOUSE +( + house_id int primary key identity(1,1) not null, + house_name varchar(50), + house_price float default(0), + type_id int references HOUSE_TYPE(type_id) +) + +insert into HOUSE_TYPE values ('小户型'),('经济型'),('别墅') +insert into HOUSE values ('1号房',500,1),('2号房',1000,2),('3号房',5000,3), +('4号房',600,1),('5号房',1200,2),('6号房',6000,3) + +--查询所有房屋信息 + select * from HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 + select * from HOUSE_TYPE where type_name like '%型%' + +--查询出房屋的名称和cc,并且按照租金降序排序 + select house_name 房屋名称,house_price 租金 from HOUSE order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 + select house_name 房屋名称,type_name 房屋类型 from HOUSE h inner join HOUSE_TYPE ht on h.type_id=ht.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 + select top 1 house_price 租金,house_name 房屋名称 from HOUSE order by house_price desc + +select * from HOUSE +select * from HOUSE_TYPE \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\350\257\232/\344\275\234\344\270\2323.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\350\257\232/\344\275\234\344\270\2323.sql" new file mode 100644 index 0000000000000000000000000000000000000000..52bafd0417ec9d498ef4a3700c46cb6eab645ac0 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\217\266\350\257\232/\344\275\234\344\270\2323.sql" @@ -0,0 +1,50 @@ +use master +go +create database StarManagerDB +on +( + name='StarManagerDB', + filename='D:\StarManagerDB.mdf', + size=5, + maxsize=50, + filegrowth=1 +) +log on +( + name='StarManagerDB_log', + filename='D:\StarManagerDB_log.ldf', + size=5, + maxsize=50, + filegrowth=1 +) +go +use StarManagerDB +go +create table StarType +( + T_NO int primary key identity(1,1) not null, + T_NAME nvarchar(20) +) + +create table StarInfo +( + S_NO int primary key identity(1,1) not null, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) + +) +insert into StarType values ('体育明星'),('IT明星'),('相声演员') + +insert into StarInfo values ('梅西',30,'射门','阿根廷',1),('科比',35,'过人','美国',1),('蔡景现',40,'敲代码','中国',2) +,('马斯克',36,'造火箭','外星人',2),('郭德纲',50,'相声','中国',3),('黄铮',41,'拼多多','中国',2) + +--3、查询年龄最大的3个明星的 ,特技和籍贯信息,要求使用别名显示列名。 + select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo order by S_AGE desc +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 + select S_T_NO, count(*) 明星人数,avg(S_AGE) 平均年龄 from StarInfo group by S_T_NO having COUNT(*)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 + select top 1 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo order by S_AGE desc \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\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\211\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\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\231\346\226\207\350\243\225/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\231\346\226\207\350\243\225/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..42a3c5e3fa02562feaccda1aba19651dad5733d3 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\255\231\346\226\207\350\243\225/SQLQuery1.sql" @@ -0,0 +1,69 @@ +use master +go + +create database GoodsDB +on +( + name = 'GoodsDB', + filename = 'D:\GoodsDB.mdf', + size = 10MB, + maxsize = 20MB, + filegrowth = 10% +) +log on +( + + name = 'GoodsDB_log', + filename = 'D:\GoodsDB_log.ldf', + size = 10MB, + maxsize = 20MB, + filegrowth = 10% +) +go + +use GoodsDB +go + +create table GoodsType +( + TypelID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) + +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypelID) +) + +insert into GoodsType values('服装内衣'),('鞋包配饰'),('手机数码') + +insert into GoodsInfo values +('提花小西装', '红色', '菲曼琪', 300 ,1), +('百搭短裤',' 绿色',' 哥弟', 100, 1), +('无袖背心',' 白色',' 阿依莲 ',700, 1), +('低帮休闲鞋',' 红色',' 菲曼琪', 900, 2), +( '中跟单鞋','绿色',' 哥弟', 400, 2), +('平底鞋',' 白色',' 阿依莲', 200 ,2), +('迷你照相机',' 红色',' 尼康', 500, 3), +('硬盘',' 黑色',' 希捷', 600, 3), +('显卡',' 黑色',' 技嘉', 800 ,3) + +select * from GoodsType +select * from GoodsInfo +--1.查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格,MAX(GoodsMoney)最贵的商品价格 from GoodsInfo group by GoodsName,GoodsColor,GoodsMoney order by GoodsMoney DESC + +--2.按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + +select TypelID 商品类型编号,MAX(GoodsMoney)最高价格,MIN(GoodsMoney)最低价格,AVG(GoodsMoney)平均价格 from GoodsInfo I inner join GoodsType T on I.TypeID = T.TypelID group by TypelID + +--3.查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + +select* from GoodsInfo I inner join GoodsType T on I.TypeID = T.TypelID where GoodsColor = '红色'AND GoodsMoney between 300 and 600 + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\256\213\345\230\211\347\202\234/\345\225\206\345\223\201\346\225\260\346\215\256.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\256\213\345\230\211\347\202\234/\345\225\206\345\223\201\346\225\260\346\215\256.sql" new file mode 100644 index 0000000000000000000000000000000000000000..281edc78c3256cc120a40750283480ae3989ccc2 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\256\213\345\230\211\347\202\234/\345\225\206\345\223\201\346\225\260\346\215\256.sql" @@ -0,0 +1,64 @@ +use master +go + +create database GoodDB +on +( + name = 'GoodDB', + filename = 'D:\GoodDB.mlf', + size = 5mb, + maxsize = 50mb , + filegrowth = 10% +) +log on +( + name = 'GoodDB_log', + filename = 'D:\GoodDB_log.mlf', + size = 5mb, + maxsize = 50mb , + filegrowth = 10% +) +go + +use GoodDB +go + +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) + +create table GoodInfo +( + GoodsID INT primary key identity(1,1) not null, + GoodsNAME nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) + +go + +insert into GoodsType values +('服装内衣'), +('鞋包配饰'), +('手机数码') +select * from GoodsType +insert into Goodinfo values +('提花小西装','红色','菲曼琪',300 , 1 ), +('百搭短裤','绿色','哥弟',100 ,1 ), +('无袖背心','白色','阿依莲',700 ,1), +('中跟单鞋','绿色','哥弟',400 ,2), +('平底鞋','白色','阿依莲',200 ,2 ), +('MINI照相机','红色','尼康',500 ,3), +('硬盘','黑色','希捷', 600,3), +('显卡','黑色','技嘉',800 , 3 ) +select * from GoodInfo + +select GoodsNAME,GoodsColor,GoodsMoney from GoodInfo where GoodsMoney = (select MAX(GoodsMoney) from GoodInfo) + +select typeid, MAX(GoodsMoney)最高价格,MIN(GoodsMoney)最低价格,AVG(GoodsMoney)平均价格 from GoodInfo group by TypeID + +select * from GoodInfo where GoodsColor = '红色' and (GoodsMoney between 300 and 600) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\262\263\345\220\257\345\215\216/01.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\262\263\345\220\257\345\215\216/01.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ba440880ca2107c2f0c3fd680acb3b49c9321ff6 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\262\263\345\220\257\345\215\216/01.sql" @@ -0,0 +1,86 @@ +use master +go +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\GoodsDB.mdf', + size=10, + maxsize=100, + filegrowth=10 +) +log on +( + name='GoodsDB_log', + filename='D:\GoodsDB_log.mdf', + size=10, + maxsize=100, + filegrowth=10 +) +go + +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) + +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) + +insert into + GoodsType +values + ('鏈嶈鍐呰。'),('闉嬪寘閰嶉グ'),('鎵嬫満鏁扮爜') + +insert into + GoodsInfo +values + ('鎻愯姳灏忚タ瑁','绾㈣壊','鑿叉浖鐞',300,1), + ('鐧炬惌鐭¥','缁胯壊','鍝ュ紵',100,1), + ('鏃犺鑳屽績','鐧借壊','闃夸緷鑾',700,1), + ('浣庡府浼戦棽闉','绾㈣壊','鑿叉浖鐞',900,2), + ('涓窡鍗曢瀷','缁胯壊','鍝ュ紵',400,2), + ('骞冲簳闉','鐧借壊','闃夸緷鑾',200,2), + ('杩蜂綘鐓х浉鏈','绾㈣壊','灏煎悍',500,3), + ('纭洏','榛戣壊','甯屾嵎',600,3), + ('鏄惧崱','榛戣壊','鎶鍢',800,3)\ + +go + +-- 3銆佹煡璇环鏍兼渶璐电殑鍟嗗搧鍚嶇О锛屽晢鍝佸悕绉板拰鍟嗗搧浠锋牸锛岃姹備娇鐢ㄥ埆鍚嶆樉绀哄垪鍚 +select top 1 + GoodsName 鍟嗗搧鍚嶇О,GoodsColor 鍟嗗搧鍚嶇О,GoodsMoney 鍟嗗搧浠锋牸 +from + GoodsInfo +group by + GoodsName,GoodsColor,GoodsMoney +order by + GoodsMoney +desc + +-- 4銆佹寜鍟嗗搧绫诲瀷缂栧彿鍒嗙粍鏌ヨ鍟嗗搧鏈楂樹环鏍硷紝鏈浣庝环鏍煎拰骞冲潎浠锋牸锛岃姹備娇鐢ㄥ埆鍚嶆樉绀哄垪鍚 +select + TypeID,max(GoodsMoney),min(GoodsMoney),avg(GoodsMoney) +from + GoodsInfo +group by + TypeID + +--5銆佹煡璇㈠晢鍝佷俊鎭墍鏈夊垪锛岃姹傚晢鍝侀鑹蹭负绾㈣壊锛屼环鏍煎湪300~600涔嬮棿 +select + * +from + GoodsInfo +where + GoodsColor='绾㈣壊' and GoodsMoney between 300 and 600 diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\346\230\214\351\276\231 (1)/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\346\230\214\351\276\231 (1)/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..17692f91caaea17326276634f31baf70497345a5 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\346\230\214\351\276\231 (1)/SQLQuery1.sql" @@ -0,0 +1,44 @@ +use master +go +create database GoodsDB +on +( + name='GoodsDB ', + filename='D:\SQLwork\GoodsDB.mdf', + Size=5MB, + maxsize=50MB, + filegrowth=10% + ) + log on +( +name='GoodsDB _log', + filename='D:\SQLwork\GoodsDB_log.ldf', + Size=5MB, + maxsize=50MB, + filegrowth=10% +) +go +create table GoodsType +( +TypeID int primary key identity(1,1) not null, +TypeName nvarchar(20) not null, +) +create table GoodsInfo +( +GoodsID int primary key identity(1,1) not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20), +GoodsMoney money not null, +TypeID int references GoodsType(TypeID), +) +insert into GoodsType (TypeName)values ('服装内衣'),('鞋包配饰'),('手机数码') +insert into GoodsInfo(GoodsName, GoodsColor, GoodsBrand, GoodsMoney,TypeID) values('提花小西装' ,'红色', '菲曼琪', 300, 1),(' 百搭短裤',' 绿色',' 哥弟', 100, 1),('无袖背心' ,'白色', '阿依莲', 700, 1),('低帮休闲鞋' ,'红色', '菲曼琪', 900, 2),('中跟单鞋 ' ,'绿色', '哥弟 ', 400 , 2),('平底鞋' ,'白色', ' 阿依莲', 200 , 2),('迷你照相机' ,'红色', '尼康', 500 , 3),('硬盘' ,'黑色', '希捷', 600 , 3),(' 显卡' ,'黑色', '技嘉', 800 , 3) +select * from GoodsType +select * from GoodsInfo +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select top 1 GoodsName 商品名称, GoodsColor 商品颜色,GoodsMoney 商品价格 ,max(GoodsMoney)最贵价格 from GoodsInfo group by GoodsName , GoodsColor ,GoodsMoney order by GoodsMoney desc +select TypeID , max (GoodsMoney) 最高价格, min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +select * from GoodsInfo A inner join GoodsType B on A.TypeID=B.TypeID where GoodsColor='红色' and GoodsMoney between 300 and 600 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\351\223\255\350\275\251/SQLQuerylx1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\351\223\255\350\275\251/SQLQuerylx1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..52e5e5cd2d2b9cc8c2acfbf8666ede0cf594038c --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\274\240\351\223\255\350\275\251/SQLQuerylx1.sql" @@ -0,0 +1,131 @@ +use master +go +create database GoodsDB + +on +( +name = 'GoodsDB', +filename ='D:\GoodsDB.mdf', +size=10, +maxsize=15, +filegrowth=10% + +) +log on +( +name = 'GoodsDB_log', +filename ='D:\GoodsDB_log.ldf', +size=10, +maxsize=15, +filegrowth=10% + +) +go +use GoodsDB +go +create table GoodsType +( +TypeID int primary key identity (1,1) not null, +TypeName nvarchar(20) not null + +) +create table GoodsInfo +( +GoodsID int primary key identity (1,1) not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20), +GoodsMoney money not null, +TypeID int references GoodsType(TypeID) +) +go +insert into GoodsType values +('服装内衣'), +('鞋包配饰'), +('手机数码') + +insert into GoodsInfo values +('提花小西装','红色','菲曼琪','300','1'), +('百搭短裤','绿色','哥弟','100','1'), +('无袖背心','白色','阿依莲','700','1'), +('低帮休闲鞋','红色','菲曼琪','900','2'), +('中跟单鞋','绿色','哥弟','400','2'), +('平底鞋','白色','阿依莲','200','2'), +('迷你照相机','红色','尼康','500','3'), +('硬盘','黑色','希捷','600','3'), +('显卡','黑色','技嘉','800','3') + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select * from GoodsInfo +select * from GoodsType + +select GoodsName 商品名称, GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo l +inner join GoodsType t on l.TypeID=t.TypeID group by GoodsName, GoodsColor,GoodsMoney +having GoodsMoney=(select max(GoodsMoney)from GoodsInfo) + + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select GoodsBrand 类型编号,max(GoodsMoney) 最高价格, min(GoodsMoney)最低价格, avg(GoodsMoney) 平均价格 from GoodsInfo group by GoodsBrand + + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git "a/\347\254\254\345\215\201\344\270\211\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\211\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\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\345\211\221\351\230\263/review1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\345\211\221\351\230\263/review1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..d3255fce63392fef43606b316e92a11fefb1f670 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\276\220\345\211\221\351\230\263/review1.sql" @@ -0,0 +1,55 @@ +use master +go +create database GoodsDB +on +( +name = 'GoodsDB', +filename = 'D:\GoodsDB.mdf', +size = 5, +maxsize = 10, +filegrowth = 10% +) +log on +( +name = 'GoodsDB_log', +filename = 'D:\GoodsDB_log.ldf', +size = 5, +maxsize = 10, +filegrowth = 10% +) +use GoodsDB +go +create table GoodsType +( +TypeID int primary key identity(1,1), +TypeName nvarchar(20) not null, +) +create table GoodsInfo +( +GoodsID int primary key identity(1,1), +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20) , +GoodsMoney money not null, +TypeID int references GoodsType(TypeID) +) +insert into GoodsType values('服装内衣'), +('鞋包配饰'), +('手机数码') +insert into GoodsInfo values('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','百搭短裤','百搭短裤',100,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) +select * from GoodsType +select * from GoodsInfo +--查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo where GoodsMoney=(select MAX(GoodsMoney) from GoodsInfo) +--按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 类别编号,MAX(GoodsMoney) 最高价格,MIN(GoodsMoney) 最低价格,AVG(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +--查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\233\271\346\226\207\346\226\214/SQLQuery13.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\233\271\346\226\207\346\226\214/SQLQuery13.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7c19ac7b5530f9533f12751278e7b6ea6db27446 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\233\271\346\226\207\346\226\214/SQLQuery13.sql" @@ -0,0 +1,61 @@ +use master +go +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\SQL\GoodsDB_mdf', + size =5MB, + maxsize= 50MB, + filegrowth=10% +) +log on +( + name='GoodsDB_log', + filename='D:\SQL\GoodsDB_log', + size =5MB, + maxsize= 50MB, + filegrowth=10% +) +use GoodsDB +go +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) +insert into GoodsType values +('服装内衣'), +('鞋包配饰'), +('手机码数') +go +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) +insert into GoodsInfo values +('提花小西装',' 红色',' 菲曼琪',' 300',' 1'), +('百搭短裤',' 绿色',' 哥弟',' 100',' 1'), +('无袖背心',' 白色',' 阿依莲', '700',' 1'), +('低帮休闲鞋',' 红色',' 菲曼琪',' 900',' 2'), +('中跟单鞋',' 绿色',' 哥弟',' 400', '2'), +('平底鞋',' 白色', '阿依莲', '200' ,'2'), +('迷你照相机',' 红色',' 尼康', '500', '3'), +('硬盘', '黑色', '希捷', '600 ','3'), +('显卡',' 黑色',' 技嘉', '800','3') +select * from GoodsType +select * from GoodsInfo +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodSName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo +where GoodsMoney IN(select max(GoodsMoney) from GoodsInfo) +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select GoodsInfo.TypeID 商品类型编号, max(GoodsMoney)最高价格,min(GoodsMoney)最低价格,avg(GoodsMoney)平均价格 from GoodsType +inner join GoodsInfo on GoodsInfo.TypeID=GoodsType.TypeID group by GoodsInfo.TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色'and GoodsMoney between 300 and 600 + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\233\276\344\273\216\346\235\234/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\233\276\344\273\216\346\235\234/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..02beb02bba8dd3e242527626b10b94be3294e509 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\233\276\344\273\216\346\235\234/SQLQuery1.sql" @@ -0,0 +1,36 @@ +use master +go +create database GoodsDB +go +use GoodsDB +create table GoodsType +( +TypeID int not null primary key identity(1,1), +TypeName nvarchar(20) not null +) +create table GoodsInfo +( +GoodsID int not null primary key identity(1,1), +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20), +GoodsMoney money not null, +TypeID int references GoodsType(TypeID) +) +insert into GoodsType(TypeName) values +('服装内衣'), +('鞋包配饰'), +('手机数码') +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) values +('百搭短裤','绿色','哥弟',100,1), +('提花小西装','红色','菲曼琪',300 ,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('无袖背心','白色','阿依莲',700,1), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo order by GoodsMoney desc +select TypeID,max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +select * from GoodsInfo where GoodsColor='红色'and GoodsMoney between 300 and 600 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\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\211\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\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\350\213\261\347\276\244/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\350\213\261\347\276\244/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e310c5738357b57a5d4ccc629e15f3c9ad72f0e9 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\235\216\350\213\261\347\276\244/SQLQuery1.sql" @@ -0,0 +1,99 @@ +--复习一 +use master +go +--1、创建商品数据库(GoodsDB),然后建立两张表,GoodsType(商品类型表),GoodsInfo(商品信息表),表结构分别如下: +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\GoodsDB.mdf', + size=5MB, + maxsize=5MB, + filegrowth=10% +) +log on +( + name='GoodsDB_log', + filename='D:\GoodsDB_log.ldf', + size=5MB, + maxsize=5MB, + filegrowth=10% +) +go + +--商品类型表(GoodsType) +--字段名 说明 类型 长度 可否为空 约束 +--TypeID 商品类型编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--TypeName 商品类型名称 nvarchar 20 否 +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity(1,1), + TypeName nvarchar(20) not null +) + +--2、使用插入语句为两张表添加数据 +--商品类型表(GoodsType) +--商品类型编号 商品类型名称 +--1 服装内衣 +--2 鞋包配饰 +--3 手机数码 +insert into GoodsType values ('服装内衣'),('鞋包配饰'),('手机数码') + +select * from GoodsType + + +--商品信息表(GoodsInfo) +--字段名 说明 类型 长度 可否为空 约束 +--GoodsID 商品编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--GoodsName 商品名称 nvarchar 20 否 +--GoodsColor 商品颜色 nvarchar 20 否 +--GoodsBrand 商品品牌 nvarchar 20 +--GoodsMoney 商品价格 money 否 +--TypeID 商品类型编号 int 外键,参照GoodsType中的TypeID + +create table GoodsInfo +( + GoodsID int primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) + +--商品信息表(GoodsInfo) +--商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 +--1 提花小西装 红色 菲曼琪 300 1 +--2 百搭短裤 绿色 哥弟 100 1 +--3 无袖背心 白色 阿依莲 700 1 +--4 低帮休闲鞋 红色 菲曼琪 900 2 +--5 中跟单鞋 绿色 哥弟 400 2 +--6 平底鞋 白色 阿依莲 200 2 +--7 迷你照相机 红色 尼康 500 3 +--8 硬盘 黑色 希捷 600 3 +--9 显卡 黑色 技嘉 800 3 +insert into GoodsInfo values ( '提花小西装', '红色',' 菲曼琪', 300 ,1), +('百搭短裤',' 绿色',' 哥弟', 100, 1), +('无袖背心',' 白色',' 阿依莲', 700 ,1), +('低帮休闲鞋',' 红色',' 菲曼琪 ',900, 2), +('中跟单鞋',' 绿色',' 哥弟 ',400 ,2), +('平底鞋',' 白色',' 阿依莲', 200 ,2), +('迷你照相机 ','红色',' 尼康', 500 ,3), +('硬盘',' 黑色',' 希捷 ',600 ,3), +('显卡',' 黑色',' 技嘉', 800, 3) + +select * from GoodsType +select * from GoodsInfo + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo +where GoodsMoney=(select top 1 GoodsMoney from GoodsInfo order by GoodsMoney desc) + +--4、按商品类型编号分组 查询商品最高价格, 最低价格 和 平均价格,要求使用别名显示列名 +select TypeID 商品类型编号,max(GoodsMoney) 最高价格,MIN(GoodsMoney) 最低价格,AVG(GoodsMoney) 平均价格 from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\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\211\346\254\241\344\275\234\344\270\232.sql" "b/\347\254\254\345\215\201\344\270\211\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\211\346\254\241\344\275\234\344\270\232.sql" new file mode 100644 index 0000000000000000000000000000000000000000..797c73118271438a95c1f59d5779668b7ae43864 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\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\211\346\254\241\344\275\234\344\270\232.sql" @@ -0,0 +1,100 @@ +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\test\GoodsDB.mdf', + size=5, + maxsize=10, + filegrowth=5 +) + +log on +( + name='GoodsDB_log', + filename='D:\test\GoodsDB_log.mdf', + size=5, + maxsize=10, + filegrowth=5 +) + +go +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) + +go +create table GoodsInfo +( + GoodsID int not null primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) + +insert into GoodsType values +('服装内衣'), +('鞋包配饰'), +('手机数码') + + +insert into GoodsInfo values +('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1), +( '无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select * from GoodsInfo where GoodsID in (select top 1 GoodsID from GoodsInfo order by GoodsMoney desc) +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo order by GoodsMoney desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + +select max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格,TypeName 商品类型 from GoodsInfo A +inner join GoodsType B on A.TypeID=B.TypeID +group by A.TypeID,TypeName + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 + +--6、查询每类商品的平均价格,要显示商品的类别名称和平均价格。 + +select TypeName 商品类型,avg(GoodsMoney) 平均价格 from GoodsInfo A +inner join GoodsType B +on A.TypeID=B.TypeID +group by A.TypeID,TypeName +--7、查询每种颜色的商品数 + +select GoodsColor 商品颜色,count(*) 商品数量 from GoodsInfo group by GoodsColor + +--8、给商品类型表添加一个“备注”字段,并给所有记录的备注都填上“我是商品类型信息备注” + +alter table GoodsType +add 备注 varchar(40) default('我是商品类型信息备注') + +--9、在商品信息表中删除商品类型的外键约束 + +alter table GoodsInfo +drop FK__GoodsInfo__TypeI__1273C1CD + +--10、删除所有商品类型记录 + +truncate table GoodsType + + + + + + diff --git "a/\347\254\254\345\215\201\344\270\211\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\211\346\254\241\344\275\234\344\270\2322.sql" "b/\347\254\254\345\215\201\344\270\211\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\211\346\254\241\344\275\234\344\270\2322.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a2e56140588c47243b61670b354f4f2fdd496817 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\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\211\346\254\241\344\275\234\344\270\2322.sql" @@ -0,0 +1,111 @@ +create database HOUSE_DB +on +( + name=HOUSE_DB, + filename='D:\HOUSE_DB.mdf', + size=5MB, + maxsize=10MB, + filegrowth=1MB +) +log on +( + name=HOUSE_DB_log, + filename='D:\HOUSE_DB_log.ldf', + size=5MB, + maxsize=10MB, + filegrowth=1MB +) +go + +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int primary key identity(1,1) not null, + type_name varchar(50) unique not null +) + +create table HOUSE +( + house_id int primary key identity(1,1), + house_name varchar(50) not null, + house_price float not null default(0), + type_id int not null foreign key references HOUSE_TYPE(type_id) +) + +--3、添加表数据 +--HOUSE_TYPE表中添加3条数据,例如:小户型、经济型、别墅 +insert into HOUSE_TYPE(type_name) +select '小户型' union +select '经济型' union +select '别墅' +--HOUSE表中添加至少3条数据,不能全都为同一类型 +insert into HOUSE(house_name,house_price,type_id) +select '小房子123',1,1 union +select '中房子',6.6,1 union +select '大房子',9.9,1 + + + + +--4、添加查询 + +--查询所有房屋信息 + +select * from HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 + +select * from HOUSE_TYPE where type_name like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 + +select house_name 房屋名,house_price 租金 from HOUSE order by house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 + +select house_name 房屋名,type_name 房屋类型 from HOUSE A +inner join HOUSE_TYPE B +on A.type_id=B.type_id + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 + +select top 1 house_name 房屋名,house_price 租金 from HOUSE order by house_price desc + +--查询每种房屋类型的房屋数量 + +select type_name 房屋类型, count(A.type_id) 房屋数量 from HOUSE A +inner join HOUSE_TYPE B +on A.type_id=B.type_id +group by A.type_id,type_name + +--修改房屋类型表的类型名称字段,把数据类型改成nvarchar(30) + +alter table HOUSE_TYPE +drop UQ__HOUSE_TY__543C4FD90A3F90CA +go +alter table HOUSE_TYPE +alter column type_name varchar(30) +go +alter table HOUSE_TYPE +add constraint UQ_HOUSE_TYPE_type_name unique(type_name) + +--给房屋信息表新增一个“地址”字段,请选择合适的数据类型。 + +alter table HOUSE +add 地址 varchar(100) + +--把房屋价格的默认值0改为100 + +alter table HOUSE +drop DF__HOUSE__house_pri__1367E606 +go +alter table HOUSE +add constraint DF_HOUSE_house_price default(100) for house_price + +--查询出名字最长的房屋信息 +select * from HOUSE +where len(house_name)= +(select max(len(house_name)) from HOUSE) + diff --git "a/\347\254\254\345\215\201\344\270\211\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\211\346\254\241\344\275\234\344\270\2323.sql" "b/\347\254\254\345\215\201\344\270\211\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\211\346\254\241\344\275\234\344\270\2323.sql" new file mode 100644 index 0000000000000000000000000000000000000000..8fd3463b670b667a339f19c7eb74b3b3e26816c8 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\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\211\346\254\241\344\275\234\344\270\2323.sql" @@ -0,0 +1,115 @@ +create database StarManagerDB +on +( + name=StarManagerDB, + filename='D:\StarManagerDB.mdf', + size=5MB, + maxsize=10MB, + filegrowth=10% +) +log on +( + name=StarManagerDB_log, + filename='D:\StarManagerDB_log.ldf', + size=5MB, + maxsize=10MB, + filegrowth=10% +) +go + +use StarManagerDB +go + +create table StarType +( + T_NO int primary key identity(1,1), + T_NAME nvarchar(20) +) +go + +create table StarInfo +( + S_NO int primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int foreign key references StarType(T_NO) +) +go + +insert into StarType(T_NAME) +select '体育明星' union +select 'IT明星' union +select '相声演员' + +insert into StarInfo(S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) +select '梅西',30,'射门','阿根廷',1 union +select '科比',35,'过人','美国',1 union +select '蔡景现',40,'敲代码','中国',2 union +select '马斯克',36,'造火箭','外星人',2 union +select '郭德纲',50,'相声','中国',3 union +select '黄铮',41,'拼多多','中国',2 + + + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 + +select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍S_AGE贯 from StarInfo order by S_AGE desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 + +select T_NAME 明星类型,count(*) 明星人数,avg(S_AGE) 平均年龄 from StarInfo A +inner join StarType B +on A.S_T_NO=B.T_NO +group by A.S_T_NO,T_NAME +having count(*)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 + +select top 1 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo +where S_T_NO=(select T_NO from StarType where T_NAME='体育明星') +order by S_AGE desc + +--6、请统计每种明星类型的人数 + +select T_NAME 类型, count(*) 人数 from StarInfo A +inner join StarType B +on A.S_T_NO=B.T_NO + group by S_T_NO,T_NAME + + +--7、给明星表增加一个“年收入”字段,并设置默认值为10000000(1千万) + +alter table StarInfo +add 年收入 money not null default(10000000) + +--8、查询每个国家的明星人数。 + +select S_NATIVE 国家, count(*) 人数 from StarInfo group by S_NATIVE + +--9、搜索所有明星信息、明星类型名称,按籍贯排序,若籍贯一样,按年龄降序排序。 + +select A.*,T_NAME from StarInfo A +inner join StarType B +on A.S_T_NO=B.T_NO +order by S_NATIVE,S_AGE + +--10、搜索名字为两个字,且最后一个字为“西”的明星信息、明星类型名称 + +select A.* ,T_NAME from StarInfo A +inner join StarType B +on A.S_T_NO=B.T_NO +where S_NAME like '_西' + +--11、给两个表都新增一个字段“创建时间”,默认值是系统当前时间。 + +alter table StarInfo +add 创建时间 date default(getdate()) + +alter table StarType +add 创建时间 date default(getdate()) + +insert into StarInfo values +('杨瑞',19,'敲代码','中国人',2,default) + diff --git "a/\347\254\254\345\215\201\344\270\211\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\211\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\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\200\235\345\260\271/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\200\235\345\260\271/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..bf65451d58185790a878158e481dc7f237b3af00 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\200\235\345\260\271/SQLQuery1.sql" @@ -0,0 +1,84 @@ +use master +go + +create database StarManagerDB +go + +use StarManagerDB +go + +create table StarType +( + T_NO int not null primary key identity, + T_NAME nvarchar(20) +) +go + +create table StarInfo +( + S_NO int not null primary key identity, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) +go + +insert into StarType values +('体育明星'), +('IT明星'), +('相声演员') +go + +insert into StarInfo values +('梅西', 30, '射门', '阿根廷', 1), +('科比', 35, '过人', '美国', 1), +('蔡景现', 40, '敲代码', '中国', 2), +('马斯克', 36, '造火箭', '外星人', 2), +('郭德纲', 50, '相声', '中国', 3), +('黄铮', 41, '拼多多', '中国', 2) +go + + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select + top 3 S_NAME '姓名', S_HOBBY '特技', S_NATIVE '籍贯信息' +from + StarInfo +order by + S_AGE desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select + count(*) '人数', avg(S_AGE) '平均年龄' +from + StarInfo +group by + S_T_NO +having + count(*) > 2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select + si.S_NAME '姓名', si.S_HOBBY '特技', si.S_NATIVE '籍贯信息' +from + StarInfo si +join + StarType st +on + si.S_T_NO = st.T_NO +where + st.T_NAME = '体育明星' and si.S_AGE = + ( + select + max(S_AGE) + from + StarInfo si + join + StarType st + on + si.S_T_NO = st.T_NO + where + st.T_NAME = '体育明星' + ) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\200\235\345\260\271/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\200\235\345\260\271/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..de618aebd25f99303d4467acac81b36bb7a267cd --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\200\235\345\260\271/SQLQuery3.sql" @@ -0,0 +1,65 @@ +Use master +go +CREATE DATABASE GoodsDB +on +(name='GoodsDB', +filename='D:\GoodsDB.mdf', +size=10mb, +maxsize=100mb, +filegrowth=10% +) +log on +(name='GoodsDB_log', +filename='D:\GoodsDB_log.ldf', +size=10mb, +maxsize=100mb, +filegrowth=10% +) +go +use GoodsDB +go +create table GoodsType +( TypeID int primary key identity, + TypeName nvarchar(20) not null + ) + go +create table GoodsInfo +( GoodsID int primary key identity, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) , + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) + ) + go +insert into GoodsType values + +('服装内衣'), +('鞋包配饰'), +('手机数码') +go +insert into GoodsInfo values + +('提花小西装', '红色 ','菲曼琪 ',300 ,1), +('百搭短裤 ','绿色', '哥弟', 100, 1), +('无袖背心' ,'白色',' 阿依莲', 700 ,1), +('低帮休闲鞋' ,'红色', '菲曼琪' ,900, 2), +('中跟单鞋', '绿色' ,'哥弟 ',400, 2), +('平底鞋' ,'白色 ','阿依莲' ,200, 2), +('迷你照相机' ,'红色' ,'尼康' ,500 ,3), +('硬盘 ','黑色 ','希捷', 600, 3), +('显卡', '黑色 ','技嘉', 800 ,3) +go + + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo where GoodsMoney = (select max(GoodsMoney) from GoodsInfo) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + +select TypeID 类别编号,MAX(GoodsMoney) 最高价格,MIN(GoodsMoney) 最低价格,AVG(GoodsMoney) 平均价格 from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\200\235\345\260\271/SQLQuery4.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\200\235\345\260\271/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..21d69457434ede93c90129934aef7ac3cd4cdd32 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\200\235\345\260\271/SQLQuery4.sql" @@ -0,0 +1,85 @@ +use master +go +CREATE database HOUSE_DB +on +(name='HOUSE_DB', +filename='D:\HOUSE_DB.mdf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +log on +(name='HOUSE_DB_log', +filename='D:\HOUSE_DB_log.ldf', +size=5mb, +maxsize=50mb, +filegrowth=1mb +) +go +use HOUSE_DB +go +create table HOUSE_TYPE +(type_id int primary key identity, + +type_name varchar(50) unique not null +) +go +create table HOUSE +(house_id int primary key identity, + +house_name varchar(50) not null, +house_price float default (0) not null, + +type_id int references HOUSE_TYPE(type_id) +) + +go + +insert into HOUSE_TYPE values +('小户型'), +('经济型'), +('别墅') +go + +insert into HOUSE values +('dog',3000,1), +('cat',4000,2), +('horse',5000,3) +go + +--查询所有房屋信息 +select * from HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 +select + * +from + HOUSE_TYPE +where + [type_name] like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select + house_name, house_price +from + HOUSE +order by + house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select + h.house_name, ht.[type_name] +from + HOUSE h +join + HOUSE_TYPE ht +on + h.[type_id] = ht.[type_id] + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select + house_price, house_name +from + HOUSE +where + house_price = (select max(house_price) from HOUSE) diff --git "a/\347\254\254\345\215\201\344\270\211\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\211\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\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\345\263\260/\345\244\215\344\271\240\351\242\2301.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\345\263\260/\345\244\215\344\271\240\351\242\2301.sql" new file mode 100644 index 0000000000000000000000000000000000000000..f9a046ff7f9f46440bd4459d7b0c72fe370119da --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\236\227\346\265\267\345\263\260/\345\244\215\344\271\240\351\242\2301.sql" @@ -0,0 +1,61 @@ +use master +go +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\sql\GoodsDB.mdf', + size=10MB, + maxsize=50MB, + filegrowth=10% +) +log on +( + name='GoodsDB_log', + filename='D:\sql\GoodsDB_log.ldf', + size=10MB, + maxsize=50MB, + filegrowth=10% +) +go +use GoodsDB +go +create table GoodsType +( + TypeID int primary key not null identity(1,1), + TypeName nvarchar(20) not null, +) +create table GoodsInfo +( + GoodsID int primary key not null identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20) not null, + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) +insert into GoodsType(TypeName) +select '服装内衣'union +select '鞋包服饰'union +select '手机数码' + +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) +select'提花小西装','红色','菲曼琪','300','1'union +select '百搭短裤','绿色','哥弟','100','1'union +select'无袖背心','白色','阿依莲','700','1'union +select'低帮休闲鞋','红色','菲曼琪','900','2'union +select'中跟单鞋','白色','哥弟','400','2'union +select'平底鞋','白色','阿依莲','200','2'union +select'迷你照相机','红色','尼康','500','3'union +select'硬盘','黑色','希捷','600','3'union +select'显卡','黑色','技嘉','800','3' + +select * from GoodsType +select * from GoodsInfo +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select GoodsType.TypeID,max(GoodsMoney)最高价格,min(GoodsMoney)最低价格,AVG(GoodsMoney)平均价格 from GoodsInfo inner join GoodsType on GoodsInfo.TypeID=GoodsType.TypeID group by GoodsType.TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsType inner join GoodsInfo on GoodsType.TypeID= GoodsInfo.TypeID where GoodsColor='红色' and GoodsMoney between 300 and 600 + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\242\201\344\270\226\350\261\252/4.1a1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\242\201\344\270\226\350\261\252/4.1a1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a2f9cc05101db82dcd7d9877283bda2ae3f1c473 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\242\201\344\270\226\350\261\252/4.1a1.sql" @@ -0,0 +1,54 @@ +use master +go +create database GoodsDB +on +( + name='GoodsDB.mdf', + filename='D:\GoodsDB.mdf', + size=5, + maxsize=15, + filegrowth=15% +) +log on +( + name='GoodsDB_log.ldf', + filename='D:\GoodsDB_log.ldf', + size=5, + maxsize=15, + filegrowth=15% +) +go +use GoodsDB +go +create table GoodsType +( + TypeID int primary key identity(1,1), + TypeName nvarchar(20) not null +) +go +create table GoodsInfo +( + GoodsID int primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) +insert into GoodsType (TypeName)values ('服装内衣'), +('鞋包配饰'),('手机数码') +insert into GoodsInfo values ('提花小西装', '红色' ,'菲曼琪', '300' ,'1'), +('百搭短裤', '绿色',' 哥弟', '100', 1),('无袖背心', '白色',' 阿依莲',' 700', 1), +('低帮休闲鞋',' 红色',' 菲曼琪',' 900', 2), +('中跟单鞋',' 绿色 ','哥弟',' 400', 2),('平底鞋',' 白色 ','阿依莲',' 200', 2), +('迷你照相机',' 红色',' 尼康',' 500', 3), +('硬盘',' 黑色 ','希捷',' 600', 3),('显卡',' 黑色',' 技嘉', '800', 3) +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,max(GoodsMoney) 最贵的商品价格 from GoodsInfo +group by GoodsName,GoodsColor order by max(GoodsMoney) desc +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型编号,max(GoodsMoney) 商品最高价格, +min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 + from GoodsInfo group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and (GoodsMoney between 300 and 600) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\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\211\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\211\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/SQLQuery1sql.sql" "b/\347\254\254\345\215\201\344\270\211\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/SQLQuery1sql.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7d6a83c3c2cc6edee56685ce9cb36aaec43b5ed7 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\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/SQLQuery1sql.sql" @@ -0,0 +1,66 @@ +use master +go + +create database GoodsDB +on +( +name='GoodsDB', +filename='D:\GoodsDB.mdf', +size=10MB, +maxsize=50MB, +filegrowth=10% +) +log on +( +name='GoodsDB_log', +filename='D:\GoodsDB_log.ldf', +size=10MB, +maxsize=50MB, +filegrowth=10% +) +go + +create table GoodsType +( +TypeID int primary key identity(1,1), +TypeName nvarchar(20) not null +) +go + +create table GoodsInfo +( +GoodsID int primary key identity(1,1), +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20), +GoodMoney money not null, +TypeID int references GoodsType(Typeid) +) +go + +insert into GoodsType values('服装内衣'), +('鞋包配饰'), +('手机数码') + +insert into Goodsinfo values +('提花小西装', '红色' ,'菲曼琪', 300, 1), +('百搭短裤', '绿色', '哥弟', 100, 1), +('无袖背心', '白色',' 阿依莲', 700, 1), +('低帮休闲鞋', '红色', '菲曼琪', 900, 2), +('中跟单鞋', '绿色', '哥弟', 400, 2), +('平底鞋', '白色', '阿依莲', 200, 2), +('迷你照相机', '红色', '尼康', 500, 3), +('硬盘',' 黑色', '希捷', 600, 3), +('显卡', '黑色', '技嘉', 800, 3) + +select * from GoodsType +select * from GoodsInfo +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称,GoodsColor 商品颜色,GoodMoney 商品价格 from GoodsInfo where GoodMoney=(select max(GoodMoney)from GoodsInfo) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 类型编号,max(GoodMoney)最高价格,min(GoodMoney)最低价格,AVG(GoodMoney) 平均价格 from GoodsInfo group by TypeID + + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色'and GoodMoney between 300 and 600 diff --git "a/\347\254\254\345\215\201\344\270\211\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\211\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..a7432bf19094a7fafaaf5db8edd516829b03756e --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\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,62 @@ +use master +go +create database GoodDB +on +( + name='GoodDB', + filename='D:\SQL\GoodDB.mdf', + size=5MB, + maxsize=20MB, + filegrowth=10% +) +log on +( + name='GoodDB_log', + filename='D:\SQL\GoodDB_log.mdf', + size=5MB, + maxsize=20MB, + filegrowth=10% +) +go + +use GoodDB +go +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null, +) + +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) +go + +insert into GoodsType values +('服装内衣'), +('鞋包配饰'), +('手机数码') + +insert into GoodsInfo values +('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) + +--查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称 , GoodsColor 商品颜色 , GoodsMoney 商品价格 from GoodsInfo group by GoodsName,GoodsColor,GoodsMoney having GoodsMoney=(select MAX(GoodsMoney) from GoodsInfo) +--按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select GoodsBrand 商品类型 , MAX(GoodsMoney) 最高价格 , MIN(GoodsMoney) 最低价格 , AVG(GoodsMoney) 平均价格 from GoodsInfo group by GoodsBrand +--查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' AND GoodsMoney>=300 AND GoodsMoney<=600 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\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\211\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\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\346\231\266\345\251\267/\345\244\215\344\271\240\351\242\230\344\270\200\344\273\243\347\240\201.txt" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\346\231\266\345\251\267/\345\244\215\344\271\240\351\242\230\344\270\200\344\273\243\347\240\201.txt" new file mode 100644 index 0000000000000000000000000000000000000000..a34494c436628e5102a958f82d4c48cb752df43f --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\346\275\230\346\231\266\345\251\267/\345\244\215\344\271\240\351\242\230\344\270\200\344\273\243\347\240\201.txt" @@ -0,0 +1,100 @@ +--复习一 +use master +go +--1、创建商品数据库(GoodsDB),然后建立两张表,GoodsType(商品类型表),GoodsInfo(商品信息表),表结构分别如下: +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\GoodsDB.mdf', + size=5MB, + maxsize=5MB, + filegrowth=10% +) +log on +( + name='GoodsDB_log', + filename='D:\GoodsDB_log.ldf', + size=5MB, + maxsize=5MB, + filegrowth=10% +) +go + +--商品类型表(GoodsType) +--字段名 说明 类型 长度 可否为空 约束 +--TypeID 商品类型编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--TypeName 商品类型名称 nvarchar 20 否 +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity(1,1), + TypeName nvarchar(20) not null +) + +--2、使用插入语句为两张表添加数据 +--商品类型表(GoodsType) +--商品类型编号 商品类型名称 +--1 服装内衣 +--2 鞋包配饰 +--3 手机数码 +insert into GoodsType values ('服装内衣'),('鞋包配饰'),('手机数码') + +select * from GoodsType + + +--商品信息表(GoodsInfo) +--字段名 说明 类型 长度 可否为空 约束 +--GoodsID 商品编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--GoodsName 商品名称 nvarchar 20 否 +--GoodsColor 商品颜色 nvarchar 20 否 +--GoodsBrand 商品品牌 nvarchar 20 +--GoodsMoney 商品价格 money 否 +--TypeID 商品类型编号 int 外键,参照GoodsType中的TypeID + +create table GoodsInfo +( + GoodsID int primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) + +--商品信息表(GoodsInfo) +--商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 +--1 提花小西装 红色 菲曼琪 300 1 +--2 百搭短裤 绿色 哥弟 100 1 +--3 无袖背心 白色 阿依莲 700 1 +--4 低帮休闲鞋 红色 菲曼琪 900 2 +--5 中跟单鞋 绿色 哥弟 400 2 +--6 平底鞋 白色 阿依莲 200 2 +--7 迷你照相机 红色 尼康 500 3 +--8 硬盘 黑色 希捷 600 3 +--9 显卡 黑色 技嘉 800 3 +insert into GoodsInfo values ( '提花小西装', '红色',' 菲曼琪', 300 ,1), +('百搭短裤',' 绿色',' 哥弟', 100, 1), +('无袖背心',' 白色',' 阿依莲', 700 ,1), +('低帮休闲鞋',' 红色',' 菲曼琪 ',900, 2), +('中跟单鞋',' 绿色',' 哥弟 ',400 ,2), +('平底鞋',' 白色',' 阿依莲', 200 ,2), +('迷你照相机 ','红色',' 尼康', 500 ,3), +('硬盘',' 黑色',' 希捷 ',600 ,3), +('显卡',' 黑色',' 技嘉', 800, 3) + +select * from GoodsType +select * from GoodsInfo + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo +where GoodsMoney=(select top 1 GoodsMoney from GoodsInfo order by GoodsMoney desc) + +--4、按商品类型编号分组 查询商品最高价格, 最低价格 和 平均价格,要求使用别名显示列名 +select TypeID 商品类型编号,max(GoodsMoney) 最高价格 ,min(GoodsMoney)最低价格,avg(GoodsMoney)平均价格 from GoodsInfo +group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\347\204\246\346\230\216\345\220\233/.keep" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\347\204\246\346\230\216\345\220\233/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\347\204\246\346\230\216\345\220\233/4.1a1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\347\204\246\346\230\216\345\220\233/4.1a1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..a2f9cc05101db82dcd7d9877283bda2ae3f1c473 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\347\204\246\346\230\216\345\220\233/4.1a1.sql" @@ -0,0 +1,54 @@ +use master +go +create database GoodsDB +on +( + name='GoodsDB.mdf', + filename='D:\GoodsDB.mdf', + size=5, + maxsize=15, + filegrowth=15% +) +log on +( + name='GoodsDB_log.ldf', + filename='D:\GoodsDB_log.ldf', + size=5, + maxsize=15, + filegrowth=15% +) +go +use GoodsDB +go +create table GoodsType +( + TypeID int primary key identity(1,1), + TypeName nvarchar(20) not null +) +go +create table GoodsInfo +( + GoodsID int primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) +insert into GoodsType (TypeName)values ('服装内衣'), +('鞋包配饰'),('手机数码') +insert into GoodsInfo values ('提花小西装', '红色' ,'菲曼琪', '300' ,'1'), +('百搭短裤', '绿色',' 哥弟', '100', 1),('无袖背心', '白色',' 阿依莲',' 700', 1), +('低帮休闲鞋',' 红色',' 菲曼琪',' 900', 2), +('中跟单鞋',' 绿色 ','哥弟',' 400', 2),('平底鞋',' 白色 ','阿依莲',' 200', 2), +('迷你照相机',' 红色',' 尼康',' 500', 3), +('硬盘',' 黑色 ','希捷',' 600', 3),('显卡',' 黑色',' 技嘉', '800', 3) +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,max(GoodsMoney) 最贵的商品价格 from GoodsInfo +group by GoodsName,GoodsColor order by max(GoodsMoney) desc +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型编号,max(GoodsMoney) 商品最高价格, +min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 + from GoodsInfo group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and (GoodsMoney between 300 and 600) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\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\211\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..6acbed69db092445b6b8f6e43a6a541218672e6a --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\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,53 @@ +use master +go + +create database GoodsDB +go + +use GoodsDB +go + +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) + +create table GoodsInfo +( + GoodeID int not null primary key identity(1,1), + GoodsNme nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) + +insert into GoodsType(TypeName) values +('服饰内衣'), +('鞋包配饰'), +('手机数码') + +insert into GoodsInfo(GoodsNme,GoodsColor,GoodsBrand,GoodsMoney,TypeID) values +('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋',' 红色', '菲曼琪', 900, 2), +('中跟单鞋' ,'绿色' ,'哥弟' ,400 ,2), +('平底鞋', '白色' ,'阿依莲' ,200, 2), +('迷你照相机' ,'红色' ,'尼康' ,500 ,3), +('硬盘', '黑色', '希捷', 600, 3), +('显卡',' 黑色',' 技嘉', 800, 3) + +select * from GoodsType + +select * from GoodsInfo + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsNme 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo order by GoodsMoney desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类别编号,max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 diff --git "a/\347\254\254\345\215\201\344\270\211\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\211\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\211\346\254\241\350\257\276\344\275\234\344\270\232/\347\250\213\346\226\207\345\220\233/\345\244\215\344\271\240\351\242\2301.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\347\250\213\346\226\207\345\220\233/\345\244\215\344\271\240\351\242\2301.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b58c668faeb77a43a5e225f550ca446f99fcfd8a --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\347\250\213\346\226\207\345\220\233/\345\244\215\344\271\240\351\242\2301.sql" @@ -0,0 +1,80 @@ +use master +go + +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\test\GoodsDB.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log on +( + name='GoodsDB_log', + filename='D:\test\GoodsDB_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +go + +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity not null , + TypeName nvarchar(20) not null , +) +go + +create table GoodsInfo +( + GoodsId int primary key identity not null , + GoodsName nvarchar(20) not null , + GoodsColor nvarchar(20) not null , + GoodsBrand nvarchar(20) , + GoodsMoney money not null , + TypeID int references GoodsType(TypeID) +) +go + +insert GoodsType values +('服装内衣'), +('鞋包配饰'), +('手机数码') +go + +insert GoodsInfo values +('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('中跟单鞋','绿色','阿依莲',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) +go + +select top 1 +GoodsName as 商品名称, +GoodsColor as 商品颜色, +GoodsMoney as 商品价格 +from GoodsInfo +order by GoodsMoney desc +go + +select +max(GoodsMoney) as 最高价格, +min(GoodsMoney) as 最低价格, +avg(GoodsMoney) as 平均价格 +from GoodsInfo +group by TypeID +go + +select * from GoodsInfo +where GoodsMoney >= 300 and GoodsMoney <= 600 and GoodsColor = '红色' + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\347\250\213\346\226\207\345\220\233/\345\244\215\344\271\240\351\242\2302.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\347\250\213\346\226\207\345\220\233/\345\244\215\344\271\240\351\242\2302.sql" new file mode 100644 index 0000000000000000000000000000000000000000..bbc57216c0ac1adf98db07fa2643e49943ac7313 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\347\250\213\346\226\207\345\220\233/\345\244\215\344\271\240\351\242\2302.sql" @@ -0,0 +1,71 @@ +use master +go + +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\test\HOUSE_DB.mdf', + size=5, + maxsize=50, + filegrowth=1 +) +log on +( + name='HOUSE_DB_log', + filename='D:\test\HOUSE_DB_log.ldf', + size=5, + maxsize=50, + filegrowth=1 +) +go + +use HOUSE_DB +go + +create table HOUSE_TYPE +( + type_id int primary key identity not null , + type_name varchar(50) not null unique +) +go + +create table HOUSE +( + house_id int primary key identity not null , + house_name varchar(50) not null , + house_price float not null , + type_id int references HOUSE_TYPE(type_id) +) +go + +insert HOUSE_TYPE values +('小户型'),('经济型'),('别墅') +go + +insert HOUSE values +('asd',2000,1), +('dsa',1500,3), +('das',1800,2) +go + +select * from HOUSE +go + +select B.* from HOUSE A +inner join HOUSE_TYPE B on A.type_id = B.type_id +where type_name like '%型' +go + +select house_name,house_price from HOUSE +order by house_price desc +go + + +select A.*,B.type_name from HOUSE A +inner join HOUSE_TYPE B on A.type_id = B.type_id +go + +select top 1 house_name,house_price from HOUSE +order by house_price desc +go \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\347\250\213\346\226\207\345\220\233/\345\244\215\344\271\240\351\242\2303.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\347\250\213\346\226\207\345\220\233/\345\244\215\344\271\240\351\242\2303.sql" new file mode 100644 index 0000000000000000000000000000000000000000..3c3ef987b4e8418896f3d20f18c72ae8249f6000 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\347\250\213\346\226\207\345\220\233/\345\244\215\344\271\240\351\242\2303.sql" @@ -0,0 +1,70 @@ +use master +go + +create database StarManagerDB +on +( + name='StarManagerDB', + filename='D:\test\StarManagerDB.mdf', + size=5, + maxsize=50, + filegrowth=10% +) +log on +( + name='StarManagerDB_log', + filename='D:\test\StarManagerDB_log.ldf', + size=5, + maxsize=50, + filegrowth=10% +) +go + +use StarManagerDB +go + +create table StarType +( + T_NO int primary key identity not null , + T_NAME nvarchar(20) , +) +go + +create table StarInfo +( + S_NO int primary key identity not null , + S_NAME nvarchar(20) not null , + S_AGE int not null , + S_HOBBY nvarchar(20) , + S_NATIVE nvarchar(20) default('中国大陆') , + S_T_NO int references StarType(T_NO) +) +go + +insert StarType values +('体育明星'), +('IT明星'), +('相声演员') +go + +insert StarInfo values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) +go + +select top 3 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo +order by S_AGE desc +go + +select S_T_NO as 类型,count(*) as 人数,avg(S_Age) as 平均年龄 from StarInfo +group by S_T_NO +go + +select top 1 S_NAME as 姓名,S_AGE as 年龄,S_HOBBY as 特技,S_NATIVE as 籍贯信息 from StarInfo +where S_T_NO = 1 +order by S_AGE desc + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\347\273\203\346\226\207\346\265\251/\347\273\203\344\271\240\351\242\2301.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\347\273\203\346\226\207\346\265\251/\347\273\203\344\271\240\351\242\2301.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0b761054187f96dd0429382f7c5b3088a2773245 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\347\273\203\346\226\207\346\265\251/\347\273\203\344\271\240\351\242\2301.sql" @@ -0,0 +1,71 @@ +Use master +go + +create database GoodsDB +on +( +name='GoodsDB', +filename='D:\GoodsDB.mdf', +size=5MB, +maxsize=50MB, +filegrowth=10% + +) +log on +( +name='GoodsDB_log', +filename='D:\GoodsDB_loh.ldf', +size=5MB, +Maxsize=50MB, +filegrowth=10% +) +go + +use GoodsDB +go + +Create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) + +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null , + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) + +) + +insert into GoodsType values +('服装内衣'),('鞋包配饰'),('手机数码') + +insert into GoodsInfo values +('提花小西装','红色','菲曼琪',300,1),('百搭短裤','绿色','哥弟',100,1),('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2),('中跟单鞋','绿色','哥弟',400,2),('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3),('硬盘','黑色','希捷',600,3),('显卡','黑色','技嘉',800,3) +--1 提花小西装 红色 菲曼琪 300 1 +--2 百搭短裤 绿色 哥弟 100 1 +--3 无袖背心 白色 阿依莲 700 1 +--4 低帮休闲鞋 红色 菲曼琪 900 2 +--5 中跟单鞋 绿色 哥弟 400 2 +--6 平底鞋 白色 阿依莲 200 2 +--7 迷你照相机 红色 尼康 500 3 +--8 硬盘 黑色 希捷 600 3 +--9 显卡 黑色 技嘉 800 3 + + + +Select * from GoodsType +Select * from GoodsInfo +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + +Select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格, max(GoodsMoney) from GoodsInfo group by GoodsName,GoodsColor,GoodsMoney order by GoodsMoney Desc +Select TypeID, max(GoodsMoney)最高价格,min(GoodsMoney)最低价格,AVG(GoodsMoney)平均价格 from GoodsInfo group by TypeID +Select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\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\211\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..6acbed69db092445b6b8f6e43a6a541218672e6a --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\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,53 @@ +use master +go + +create database GoodsDB +go + +use GoodsDB +go + +create table GoodsType +( + TypeID int not null primary key identity(1,1), + TypeName nvarchar(20) not null +) + +create table GoodsInfo +( + GoodeID int not null primary key identity(1,1), + GoodsNme nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) + +insert into GoodsType(TypeName) values +('服饰内衣'), +('鞋包配饰'), +('手机数码') + +insert into GoodsInfo(GoodsNme,GoodsColor,GoodsBrand,GoodsMoney,TypeID) values +('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋',' 红色', '菲曼琪', 900, 2), +('中跟单鞋' ,'绿色' ,'哥弟' ,400 ,2), +('平底鞋', '白色' ,'阿依莲' ,200, 2), +('迷你照相机' ,'红色' ,'尼康' ,500 ,3), +('硬盘', '黑色', '希捷', 600, 3), +('显卡',' 黑色',' 技嘉', 800, 3) + +select * from GoodsType + +select * from GoodsInfo + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsNme 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo order by GoodsMoney desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类别编号,max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 diff --git "a/\347\254\254\345\215\201\344\270\211\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\270\211\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..e78e71aafe52197443858c2c61cfe56a9b024e0c --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\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,63 @@ +use master +go + +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='D:\SQL\HOUSE_DB_mdf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) +log on +( + name='HOUSE_DB_log', + filename='D:\SQL\HOUSE_DB_log_ldf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) +use HOUSE_DB +go + +create table HOUSE_TYPE +( + typeid int not null primary key identity(1,1), + typename varchar(50) not null unique +) + +create table HOUSE +( + houseid int not null primary key identity(1,1), + housename varchar(50) not null , + houseprice float not null default(0), + typeid int not null references HOUSE_TYPE(typeid) +) +insert into HOUSE_TYPE(typename) values +('小户型'), +('经济型'), +('别墅') + +insert into HOUSE(housename,houseprice,typeid) values +('俩室一厅',4500,1), +('三室俩厅',6500,2), +('套房',10000,3) + +--查询所有房屋信息 +select * from HOUSE_TYPE + +select * from HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 +select * from HOUSE_TYPE where typename like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select housename,houseprice from HOUSE order by houseprice desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select housename,typename from HOUSE h inner join HOUSE_TYPE t on h.typeid=t.typeid + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select top 1 housename,houseprice from HOUSE order by houseprice desc + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\347\277\201\346\231\266\350\276\211/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\347\277\201\346\231\266\350\276\211/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..dc99f8a472ebea8c60fd1a25885cd00bb74e5a39 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\347\277\201\346\231\266\350\276\211/SQLQuery3.sql" @@ -0,0 +1,52 @@ +use master +go + +create database StarManagerDB +go + +use StarManagerDB +go + +create table StarType +( + T_NO int not null primary key identity(1,1), + T_NAME nvarchar(20) +) + +create table StarInfo +( + S_NO int not null primary key identity(1,1), + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) + +insert into StarType(T_NAME) values +('体育明星'), +('IT明星'), +('相声演员') + +insert into StarInfo(S_NAME,S_AGE,S_HOBBY,S_NATIVE,S_T_NO) values +('梅西',30,'射门','阿根廷',1), +('科比',35,'过人','美国',1), +('蔡景现',40,'敲代码','中国',2), +('马斯克',36,'造火箭','外星人',2), +('郭德纲',50,'相声','中国',3), +('黄铮',41,'拼多多','中国',2) + +select * from StarType + +select * from StarInfo + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select top 3 S_NAME 姓名,S_HOBBY 特技,S_NATIVE 籍贯 from StarInfo order by S_AGE desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select S_T_NO 明星类别编号,count(S_T_NO) 明星人数,avg(S_AGE) 明星平均年龄 from StarInfo group by S_T_NO having count(S_T_NO)>2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select top 1 S_NAME,S_HOBBY,S_NATIVE from StarType s inner join StarInfo t on s.T_NO=t.S_T_NO where T_NAME='体育明星' order by S_AGE desc + + diff --git "a/\347\254\254\345\215\201\344\270\211\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\211\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\211\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\211\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..831c090bd4eefc5c510f3ef5342fd573651854aa --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\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,69 @@ +use master +go +create database GoodsDB +on +( + name='GoodsDB', + filename='D:\sql\GoodsDB.mdf', + size=5MB, + maxsize=500MB, + filegrowth=5MB +) +log on +( + name='GoodsDB_log', + filename='D:\sql\GoodsDB_log.ldf', + size=5MB, + maxsize=500MB, + filegrowth=5MB +) +go +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity (1,1) not null, --商品类型编号 + TypeName nvarchar(20) not null --商品类型名称 +) + +create table GoodsInfo +( + GoodsID int primary key identity (1,1) not null, --商品编号 + GoodsName nvarchar(20) not null, --商品名称 + GoodsColor nvarchar(20) not null, --商品颜色 + GoodsBrand nvarchar(20), --商品品牌 + GoodsMoney money not null, --商品价格 + TypeID int references GoodsType(TypeID) --商品类型编号 +) + +--set IDENTITY_INSERT GoodsType on +insert into GoodsType(TypeID,TypeName) values +(1,'服装内衣'), +(2,'鞋码配饰'), +(3,'手机数码') + +--alter table GoodsInfo drop constraint FK__GoodsInfo__TypeI__1273C1CD +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) values +('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) + +select * from GoodsType +select * from GoodsInfo + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 商品名称, GoodsColor 商品颜色, GoodsMoney 商品价格 from GoodsInfo order by GoodsMoney desc + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select T.TypeID 商品类型编号, max(GoodsMoney) 最高价格, min(GoodsMoney) 最低价格, AVG(GoodsMoney) 平均价格 from +GoodsInfo I inner join GoodsType T on I.TypeID=T.TypeID group by T.TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and (GoodsMoney>=300 and GoodsMoney<=600) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\346\264\245\345\215\216/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\346\264\245\345\215\216/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..17692f91caaea17326276634f31baf70497345a5 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\203\241\346\264\245\345\215\216/SQLQuery2.sql" @@ -0,0 +1,44 @@ +use master +go +create database GoodsDB +on +( + name='GoodsDB ', + filename='D:\SQLwork\GoodsDB.mdf', + Size=5MB, + maxsize=50MB, + filegrowth=10% + ) + log on +( +name='GoodsDB _log', + filename='D:\SQLwork\GoodsDB_log.ldf', + Size=5MB, + maxsize=50MB, + filegrowth=10% +) +go +create table GoodsType +( +TypeID int primary key identity(1,1) not null, +TypeName nvarchar(20) not null, +) +create table GoodsInfo +( +GoodsID int primary key identity(1,1) not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20), +GoodsMoney money not null, +TypeID int references GoodsType(TypeID), +) +insert into GoodsType (TypeName)values ('服装内衣'),('鞋包配饰'),('手机数码') +insert into GoodsInfo(GoodsName, GoodsColor, GoodsBrand, GoodsMoney,TypeID) values('提花小西装' ,'红色', '菲曼琪', 300, 1),(' 百搭短裤',' 绿色',' 哥弟', 100, 1),('无袖背心' ,'白色', '阿依莲', 700, 1),('低帮休闲鞋' ,'红色', '菲曼琪', 900, 2),('中跟单鞋 ' ,'绿色', '哥弟 ', 400 , 2),('平底鞋' ,'白色', ' 阿依莲', 200 , 2),('迷你照相机' ,'红色', '尼康', 500 , 3),('硬盘' ,'黑色', '希捷', 600 , 3),(' 显卡' ,'黑色', '技嘉', 800 , 3) +select * from GoodsType +select * from GoodsInfo +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select top 1 GoodsName 商品名称, GoodsColor 商品颜色,GoodsMoney 商品价格 ,max(GoodsMoney)最贵价格 from GoodsInfo group by GoodsName , GoodsColor ,GoodsMoney order by GoodsMoney desc +select TypeID , max (GoodsMoney) 最高价格, min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +select * from GoodsInfo A inner join GoodsType B on A.TypeID=B.TypeID where GoodsColor='红色' and GoodsMoney between 300 and 600 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\213\217\346\231\272\351\276\231/.keep" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\213\217\346\231\272\351\276\231/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\213\217\346\231\272\351\276\231/SQLQuery1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\213\217\346\231\272\351\276\231/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..42a3c5e3fa02562feaccda1aba19651dad5733d3 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\213\217\346\231\272\351\276\231/SQLQuery1.sql" @@ -0,0 +1,69 @@ +use master +go + +create database GoodsDB +on +( + name = 'GoodsDB', + filename = 'D:\GoodsDB.mdf', + size = 10MB, + maxsize = 20MB, + filegrowth = 10% +) +log on +( + + name = 'GoodsDB_log', + filename = 'D:\GoodsDB_log.ldf', + size = 10MB, + maxsize = 20MB, + filegrowth = 10% +) +go + +use GoodsDB +go + +create table GoodsType +( + TypelID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) + +create table GoodsInfo +( + GoodsID int primary key identity(1,1) not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypelID) +) + +insert into GoodsType values('服装内衣'),('鞋包配饰'),('手机数码') + +insert into GoodsInfo values +('提花小西装', '红色', '菲曼琪', 300 ,1), +('百搭短裤',' 绿色',' 哥弟', 100, 1), +('无袖背心',' 白色',' 阿依莲 ',700, 1), +('低帮休闲鞋',' 红色',' 菲曼琪', 900, 2), +( '中跟单鞋','绿色',' 哥弟', 400, 2), +('平底鞋',' 白色',' 阿依莲', 200 ,2), +('迷你照相机',' 红色',' 尼康', 500, 3), +('硬盘',' 黑色',' 希捷', 600, 3), +('显卡',' 黑色',' 技嘉', 800 ,3) + +select * from GoodsType +select * from GoodsInfo +--1.查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格,MAX(GoodsMoney)最贵的商品价格 from GoodsInfo group by GoodsName,GoodsColor,GoodsMoney order by GoodsMoney DESC + +--2.按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + +select TypelID 商品类型编号,MAX(GoodsMoney)最高价格,MIN(GoodsMoney)最低价格,AVG(GoodsMoney)平均价格 from GoodsInfo I inner join GoodsType T on I.TypeID = T.TypelID group by TypelID + +--3.查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + +select* from GoodsInfo I inner join GoodsType T on I.TypeID = T.TypelID where GoodsColor = '红色'AND GoodsMoney between 300 and 600 + diff --git "a/\347\254\254\345\215\201\344\270\211\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\211\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..b2da2acb3f0f382f0156ff8e0f9df799b6430339 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\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,70 @@ +create database GoodsDB +on +( +name='GoodsDB', +filename='D:\SQL\GoodsDB.mdf', +size=5mb, +maxsize=50mb, +filegrowth=10% +) +log on +( +name='GoodsDB_log', +filename='D:\SQL\GoodsDB_log.mdf', +size=5mb, +maxsize=50mb, +filegrowth=10% +) +go + +use GoodsDB +go + +create table GoodsType +( +TypeID int primary key identity not null, +TypeName nvarchar(20) not null +) +go + +create table GoodsInfo +( +GoodsID int primary key identity not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20), +GoodsMoney money not null, +TypeID int references GoodsType(TypeID) +) + +insert into GoodsType values +('服装内衣'), +('鞋包配饰'), +('手机数码') +go + +insert into GoodsInfo values +('提花小西装',' 红色',' 菲曼琪', 300, 1), +('百搭短裤', '绿色', '哥弟' ,100 ,1), +('无袖背心', '白色', '阿依莲' ,700 ,1), +('低帮休闲鞋', '红色', '菲曼琪' ,900 ,2), +('平底鞋', '白色', '阿依莲' ,200, 2), +('迷你照相机', '红色', '尼康' ,500 ,3), +('硬盘' ,'黑色', '希捷' ,600 ,3), +('显卡', '黑色', '技嘉' ,800 ,3) +go + +select * from GoodsType +select * from GoodsInfo +go + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo where GoodsMoney=(select MAX(GoodsMoney) from GoodsInfo) +go + +--4、按商品类型编号分组查询商品最高价格,最高价格和平均价格,要求使用别名显示列名 +select TypeID 类型编号,MAX(GoodsMoney) 最高价格,MIN(GoodsMoney) 最高价格,AVG(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +go + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\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\211\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\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\350\245\277\346\266\233/SQLQuery4.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\350\245\277\346\266\233/SQLQuery4.sql" new file mode 100644 index 0000000000000000000000000000000000000000..5516440fac64a3fa1840139323aa24df95a1d03f --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\224\241\350\245\277\346\266\233/SQLQuery4.sql" @@ -0,0 +1,71 @@ +create database GoodsDB +on +( + name=GoodsDB, + filename='D:\GoodsDB.mdf', + size=5, + maxsize=15, + filegrowth=10 +) +log on +( + NAME=GoodsDB_LOG, + FILENAME='D:\GoodsDB_LOG.LDF', + SIZE=5, + MAXSIZE=15, + FILEGROWTH=10 +) +GO +USE GOODSDB +go +create table goodstype +( + typeid int primary key identity(1,1) not null, + typename nvarchar(20) not null +) +create table goodsinfo +( + goodsid int primary key identity(1,1) not null, + goodsname nvarchar(20) not null, + goodscolor nvarchar(20) not null, + goodsbrand nvarchar(20), + goodsmoney money, + typeid int references goodstype(typeid) +) +--商品类型表(GoodsType) +--商品类型编号 商品类型名称 +--1 服装内衣 +--2 鞋包配饰 +--3 手机数码 + insert into goodstype values('服装内衣'),('鞋包配饰'),('手机数码') + select * from goodstype +--商品信息表(GoodsInfo) +--商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 +--1 提花小西装 红色 菲曼琪 300 1 +--2 '百搭短裤','绿色',' 哥弟', 100 1 +--3 '无袖背心','白色','阿依莲',700 ,1 +--4 '低帮休闲鞋',' 红色 ','菲曼琪', 900, 2 +--5 '中跟单鞋',' 绿色',' 哥弟', 400, 2 +--6 '平底鞋',' 白色',' 阿依莲', 200 ,2 +--7 '迷你照相机',' 红色 ','尼康',500, 3 +--8 '硬盘',' 黑色',' 希捷',' 600 ,3 +--9 '显卡',' 黑色 ','技嘉',' 800, 3 + insert into goodsinfo values('提花小西装','红色','菲曼琪',300,1), + ('百搭短裤','绿色','哥弟',100,1), + ('无袖背心','白色','阿依莲',700,1), + ('低帮休闲鞋','红色','菲曼琪',900,2), + ('中跟单鞋','绿色','哥弟',400,2), + ('平底鞋','白色','阿依莲',200,2), + ('迷你照相机','红色','尼康',500,3), + ('硬盘','黑色','希捷',600 ,3), + ('显卡','黑色','技嘉',800,3) + + select * from goodsinfo +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + select top 1 goodsname 商品名称,goodscolor 商品颜色,goodsmoney 商品价格 from goodsinfo + order by goodsmoney desc +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + select typeid,MAX(goodsmoney)最高价格,MIN(goodsmoney)最低价格,AVG(goodsmoney)平均价格 from goodsinfo + group by typeid +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + select * from goodsinfo where goodscolor='红色' and (goodsmoney between 300 and 600) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\226\233\351\207\221\345\201\245/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\226\233\351\207\221\345\201\245/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..ed6318f1f8ae3fe4723a8bb2e39bb0fb02722581 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\226\233\351\207\221\345\201\245/SQLQuery2.sql" @@ -0,0 +1,47 @@ +create database GoodDB +go + +use GoodDB +go + +create table GoodsType +( +TypeID int primary key identity(1,1) not null, +TypeName nvarchar(20) not null +) +go +create table GoodsInfo +( +GoodsID int primary key identity(1,1) not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20)not null, +GoodsBrand nvarchar(20), +GoodsMoney money not null, +TypeID int references GoodsType(TypeID) +) +go + +insert into GoodsType values +('内服装衣'),('鞋包配饰'),('手机数码') +select * from GoodsType + +insert into GoodsInfo values +( '提花小西装' ,'红色' ,'菲曼琪', 300, 1), +( '百搭短裤' ,'绿色' ,'哥弟' ,100 ,1), +( '无袖背心', '白色', '阿依莲', 700 ,1), +( '低帮休闲鞋' ,'红色',' 菲曼琪' ,900 ,2), +( '中跟单鞋' ,'绿色', '哥弟', 400 ,2), +('平底鞋' ,'白色', '阿依莲' ,200 ,2), +('迷你照相机', '红色', '尼康', 500 ,3), +('硬盘' ,'黑色', '希捷' ,600, 3), +('显卡', '黑色',' 技嘉' ,800, 3) +select * from GoodsInfo + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo order by GoodsMoney desc + +--4、按商品类型编号分组查询商品 最高价格, 最低价格和平均价格,要求使用别名显示列名 +select TypeID 编号,max(GoodsMoney)最高价格,min(GoodsMoney)最低价格,avg(GoodsMoney)平均价格 from GoodsInfo group by TypeID +select * from GoodsInfo +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor like '红色' and GoodsMoney between 300 and 600 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\242\201\345\235\232/SQLQueryC1.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\242\201\345\235\232/SQLQueryC1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..b9174c846c88a06a97b790e485634480bbbb698b --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\242\201\345\235\232/SQLQueryC1.sql" @@ -0,0 +1,46 @@ +use master +go +create database GoodsDB +go +create table GoodsType --商品类型表 +( + TypeID int PRIMARY KEY identity(1,1) not null, --商品类型编号 + TypeName nvarchar(20) not null --商品类型名称 +) +create table GoodsInfo --商品信息表 +( + GoodsID int PRIMARY KEY identity(1,1) not null, --商品编号 + GoodsName nvarchar(20) not null, --商品名称 + GoodsColor nvarchar(20) not null, --商品颜色 + GoodsBrand nvarchar(20), --商品品牌 + GoodsMoney money not null, --商品价格 + TypeID int REFERENCES GoodsType(TypeID) --商品类型编号 +) +insert into GoodsType(TypeName) values +('服装内衣'), +('鞋包配饰'), +('手机数码') + +insert into GoodsInfo values +('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) + +select*from GoodsType --商品类型表 +select*from GoodsInfo --商品信息表 + +--查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo + +--按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeName 商品类型,GoodsMoney 商品价格 from GoodsType T inner join GoodsInfo I on T.TypeID=I.TypeID +group by TypeName + +--查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select*from GoodsInfo where GoodsColor='红色' and GoodsMoney>300 and GoodsMoney<600 diff --git "a/\347\254\254\345\215\201\344\270\211\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\270\211\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\270\211\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\211\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..78944baa72624da270e6dcff77b729b01b8c02a3 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\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,56 @@ +use master +go +create database GoodsDB +on +( +name='GoodsDB', +filename='D:\GoodsDB.mdf', +size=5, +maxsize=50, +filegrowth=10% +) +log on +( +name='GoodsDB_log', +filename='D:\GoodsDB_log.ldf', +size=5, +maxsize=50, +filegrowth=10% +) +go +use GoodsDB +go +create table GoodsType--商品类型表 +( +TypeID int not null primary key identity(1,1),--商品类型编号 +TypeName nvarchar(20) not null--商品类型名称 +) +create table GoodsInfo +( +GoodsID int not null primary key identity(1,1),--商品编号 +GoodsName nvarchar(20) not null,--商品名称 +GoodsColor nvarchar(20) not null,--商品颜色 +GoodsBrand nvarchar(20),--商品品牌 +GoodsMoney money not null,--商品价格 +TypeID int references GoodsType(TypeID)--商品类型编号 +) +go +insert into GoodsType(TypeName) values ('服装内衣'),('鞋包配饰'),('手机数码') +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID)values +('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) +select * from GoodsType +select * from GoodsInfo +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo order by GoodsMoney desc +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型编号,MAX(GoodsMoney)最高价格,MIN(GoodsMoney)最低价格,AVG(GoodsMoney)平均价格 from GoodsInfo group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\260\255\350\277\233/Sql13.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\260\255\350\277\233/Sql13.sql" new file mode 100644 index 0000000000000000000000000000000000000000..24737c32266b484194e3fc359f0257b61f70bd2c --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\260\255\350\277\233/Sql13.sql" @@ -0,0 +1,69 @@ +--1、创建商品数据库(GoodsDB),然后建立两张表,GoodsType(商品类型表),GoodsInfo(商品信息表),表结构分别如下: +--商品类型表(GoodsType) +create database GoodsDB +go + +--商品类型表(GoodsType) +--字段名 说明 类型 长度 可否为空 约束 +--TypeID 商品类型编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--TypeName 商品类型名称 nvarchar 20 否 + +use GoodsDB +create table GoodsType +( +TypeID int primary key identity (1,1) not null, +TypeName nvarchar(20) not null +) + +create table GoodsInfo +--商品信息表(GoodsInfo) +--字段名 说明 类型 长度 可否为空 约束 +--GoodsID 商品编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--GoodsName 商品名称 nvarchar 20 否 +--GoodsColor 商品颜色 nvarchar 20 否 +--GoodsBrand 商品品牌 nvarchar 20 +--GoodsMoney 商品价格 money 否 +--TypeID 商品类型编号 int 外键,参照GoodsType中的TypeID + +( +GoodsID int primary key identity (1,1) not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20), +GoodsMoney money not null, +TypeID int references GoodsType(TypeID) +) + +--2、使用插入语句为两张表添加数据 +--商品类型表(GoodsType) +--商品类型编号 商品类型名称 +--1 服装内衣 +--2 鞋包配饰 +--3 手机数码 +insert into GoodsType values ('服装内衣'),('鞋包配饰'),('手机数码') + +--商品信息表(GoodsInfo) +--商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 +--1 提花小西装 红色 菲曼琪 300 1 +--2 百搭短裤 绿色 哥弟 100 1 +--3 无袖背心 白色 阿依莲 700 1 +--4 低帮休闲鞋 红色 菲曼琪 900 2 +--5 中跟单鞋 绿色 哥弟 400 2 +--6 平底鞋 白色 阿依莲 200 2 +--7 迷你照相机 红色 尼康 500 3 +--8 硬盘 黑色 希捷 600 3 +--9 显卡 黑色 技嘉 800 3 +insert into GoodsInfo values ('提花小西装','红色','菲曼琪','300','1'),('百搭短裤','红色','哥弟','100','1'), +('无袖背心','白色','阿依莲','700','1'),('低帮休闲鞋','红色','菲曼琪','900','2'),('中跟单鞋','绿色','哥弟','400','2') +,('平底鞋','白色','阿依莲','200','1'),('迷你照相机','红色','尼康','500','3') +,('硬盘','黑色','希捷','600','3'),('显卡','黑色','技嘉','800','3') + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo group by GoodsMoney,GoodsColor,GoodsName +order by GoodsMoney desc +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 类型编号,max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 from GoodsInfo +group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>='300' and GoodsMoney<='600' \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\265\265\345\256\266\351\275\220/Sql13.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\265\265\345\256\266\351\275\220/Sql13.sql" new file mode 100644 index 0000000000000000000000000000000000000000..24737c32266b484194e3fc359f0257b61f70bd2c --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\265\265\345\256\266\351\275\220/Sql13.sql" @@ -0,0 +1,69 @@ +--1、创建商品数据库(GoodsDB),然后建立两张表,GoodsType(商品类型表),GoodsInfo(商品信息表),表结构分别如下: +--商品类型表(GoodsType) +create database GoodsDB +go + +--商品类型表(GoodsType) +--字段名 说明 类型 长度 可否为空 约束 +--TypeID 商品类型编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--TypeName 商品类型名称 nvarchar 20 否 + +use GoodsDB +create table GoodsType +( +TypeID int primary key identity (1,1) not null, +TypeName nvarchar(20) not null +) + +create table GoodsInfo +--商品信息表(GoodsInfo) +--字段名 说明 类型 长度 可否为空 约束 +--GoodsID 商品编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--GoodsName 商品名称 nvarchar 20 否 +--GoodsColor 商品颜色 nvarchar 20 否 +--GoodsBrand 商品品牌 nvarchar 20 +--GoodsMoney 商品价格 money 否 +--TypeID 商品类型编号 int 外键,参照GoodsType中的TypeID + +( +GoodsID int primary key identity (1,1) not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20), +GoodsMoney money not null, +TypeID int references GoodsType(TypeID) +) + +--2、使用插入语句为两张表添加数据 +--商品类型表(GoodsType) +--商品类型编号 商品类型名称 +--1 服装内衣 +--2 鞋包配饰 +--3 手机数码 +insert into GoodsType values ('服装内衣'),('鞋包配饰'),('手机数码') + +--商品信息表(GoodsInfo) +--商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 +--1 提花小西装 红色 菲曼琪 300 1 +--2 百搭短裤 绿色 哥弟 100 1 +--3 无袖背心 白色 阿依莲 700 1 +--4 低帮休闲鞋 红色 菲曼琪 900 2 +--5 中跟单鞋 绿色 哥弟 400 2 +--6 平底鞋 白色 阿依莲 200 2 +--7 迷你照相机 红色 尼康 500 3 +--8 硬盘 黑色 希捷 600 3 +--9 显卡 黑色 技嘉 800 3 +insert into GoodsInfo values ('提花小西装','红色','菲曼琪','300','1'),('百搭短裤','红色','哥弟','100','1'), +('无袖背心','白色','阿依莲','700','1'),('低帮休闲鞋','红色','菲曼琪','900','2'),('中跟单鞋','绿色','哥弟','400','2') +,('平底鞋','白色','阿依莲','200','1'),('迷你照相机','红色','尼康','500','3') +,('硬盘','黑色','希捷','600','3'),('显卡','黑色','技嘉','800','3') + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo group by GoodsMoney,GoodsColor,GoodsName +order by GoodsMoney desc +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 类型编号,max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 from GoodsInfo +group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>='300' and GoodsMoney<='600' \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\277\252\345\212\233\344\272\232\345\260\224/Sql13.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\277\252\345\212\233\344\272\232\345\260\224/Sql13.sql" new file mode 100644 index 0000000000000000000000000000000000000000..24737c32266b484194e3fc359f0257b61f70bd2c --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\350\277\252\345\212\233\344\272\232\345\260\224/Sql13.sql" @@ -0,0 +1,69 @@ +--1、创建商品数据库(GoodsDB),然后建立两张表,GoodsType(商品类型表),GoodsInfo(商品信息表),表结构分别如下: +--商品类型表(GoodsType) +create database GoodsDB +go + +--商品类型表(GoodsType) +--字段名 说明 类型 长度 可否为空 约束 +--TypeID 商品类型编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--TypeName 商品类型名称 nvarchar 20 否 + +use GoodsDB +create table GoodsType +( +TypeID int primary key identity (1,1) not null, +TypeName nvarchar(20) not null +) + +create table GoodsInfo +--商品信息表(GoodsInfo) +--字段名 说明 类型 长度 可否为空 约束 +--GoodsID 商品编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--GoodsName 商品名称 nvarchar 20 否 +--GoodsColor 商品颜色 nvarchar 20 否 +--GoodsBrand 商品品牌 nvarchar 20 +--GoodsMoney 商品价格 money 否 +--TypeID 商品类型编号 int 外键,参照GoodsType中的TypeID + +( +GoodsID int primary key identity (1,1) not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20), +GoodsMoney money not null, +TypeID int references GoodsType(TypeID) +) + +--2、使用插入语句为两张表添加数据 +--商品类型表(GoodsType) +--商品类型编号 商品类型名称 +--1 服装内衣 +--2 鞋包配饰 +--3 手机数码 +insert into GoodsType values ('服装内衣'),('鞋包配饰'),('手机数码') + +--商品信息表(GoodsInfo) +--商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 +--1 提花小西装 红色 菲曼琪 300 1 +--2 百搭短裤 绿色 哥弟 100 1 +--3 无袖背心 白色 阿依莲 700 1 +--4 低帮休闲鞋 红色 菲曼琪 900 2 +--5 中跟单鞋 绿色 哥弟 400 2 +--6 平底鞋 白色 阿依莲 200 2 +--7 迷你照相机 红色 尼康 500 3 +--8 硬盘 黑色 希捷 600 3 +--9 显卡 黑色 技嘉 800 3 +insert into GoodsInfo values ('提花小西装','红色','菲曼琪','300','1'),('百搭短裤','红色','哥弟','100','1'), +('无袖背心','白色','阿依莲','700','1'),('低帮休闲鞋','红色','菲曼琪','900','2'),('中跟单鞋','绿色','哥弟','400','2') +,('平底鞋','白色','阿依莲','200','1'),('迷你照相机','红色','尼康','500','3') +,('硬盘','黑色','希捷','600','3'),('显卡','黑色','技嘉','800','3') + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo group by GoodsMoney,GoodsColor,GoodsName +order by GoodsMoney desc +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 类型编号,max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 from GoodsInfo +group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>='300' and GoodsMoney<='600' \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\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\211\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..724dfa2302943e9b1268cd92f9afccd777b9a8a9 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\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,67 @@ +use master +go + +create database GoodsDB +on +( +name = 'GoodsDB', +filename = 'D:\GoodsDB.mdf', +size = 5, +maxsize = 10, +filegrowth = 10% +) +log on +( +name = 'GoodsDB_log', +filename = 'D:\GoodsDB_log.ldf', +size = 5, +maxsize = 10, +filegrowth = 10% +) +go + +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity(1,1) not null, + TypeName nvarchar(20) not null +) + +create table GoodsInfo +( + GoodsID int primary key identity(1,1), + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) + +insert into GoodsType(TypeName) values +('服装内衣'), +('服装内衣'), +('手机数码') + +insert into GoodsInfo(GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID) values +('提花小西装','红色','菲曼琪',300,1), +('百搭短裤','绿色','哥弟',100,1), +('无袖背心','白色','阿依莲',700,1), +('低帮休闲鞋','红色','菲曼琪',900,2), +('中跟单鞋','绿色','哥弟',400,2), +('平底鞋','白色','阿依莲',200,2), +('迷你照相机','红色','尼康',500,3), +('硬盘','黑色','希捷',600,3), +('显卡','黑色','技嘉',800,3) + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodSName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo +where GoodsMoney in(select max(GoodsMoney) from GoodsInfo) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 商品类型,sum(GoodsMoney)最高价格,min(GoodsMoney)最低价格,avg(GoodsMoney)平均价格 from GoodsInfo +group by TypeID +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 + diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\346\214\257\347\201\277/SQLQuery\345\244\215\344\271\240\351\242\2301.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\346\214\257\347\201\277/SQLQuery\345\244\215\344\271\240\351\242\2301.sql" new file mode 100644 index 0000000000000000000000000000000000000000..05be5cfca8d0fef98993f72381ef58cff4c5d041 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\203\255\346\214\257\347\201\277/SQLQuery\345\244\215\344\271\240\351\242\2301.sql" @@ -0,0 +1,65 @@ +create database GoodsDB +go +--(商品数据库) +use GoodsDB +go +--商品类型表 +create table GoodsType +( +--商品类型编号 +TypeID int primary key identity(1,1) not null, +--商品类型名称 +TypeName nvarchar(20) not null +) + +--商品信息表 +create table GoodsInfo +( +--商品编号 +GoodsID int primary key identity(1,1) not null, +--商品名称 +GoodsName nvarchar(20) not null, +--商品颜色 +GoodsColor nvarchar(20) not null, +--商品品牌 +GoodsBrand nvarchar(20), +--商品价格 +GoodsMoney money not null, +--商品类型编号 +TypeID int references GoodsType(TypeID) +) + +insert into GoodsType values +('服装内衣'), +('鞋包配饰'), +('手机数码') + +insert into GoodsInfo values +('提花小西装','红色',' 菲曼琪',300,1), +('百搭短裤','绿色',' 哥弟',100,1), +('无袖背心','白色',' 阿依莲',700,1), +('低帮休闲鞋','红色',' 菲曼琪',900,2), +('中跟单鞋','绿色',' 哥弟',400,2), +('平底鞋','白色',' 阿依莲',200,2), +('迷你照相机','红色',' 尼康',500,3), +('硬盘','黑色',' 希捷',600,3), +('显卡','黑色',' 技嘉 ',800,3) + +select * from GoodsType +select * from GoodsInfo + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 + +select GoodsName 商品名称,GoodsColor 商品颜色,MAX(GoodsMoney) 商品价格 from GoodsInfo group by GoodsName,GoodsColor + having MAX(GoodsMoney)=(select top 1 GoodsMoney from GoodsInfo order by GoodsMoney desc) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 + +select TypeID 商品类型编号, MAX(GoodsMoney) 最高价格,MIN(GoodsMoney) 最低价格,AVG(GoodsMoney)平均价格 from GoodsInfo group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 + +select GoodsID 商品编号,GoodsName 商品名称,GoodsColor 商品颜色,GoodsBrand 商品品牌,GoodsMoney 商品价格,TypeID 商品类型编号 from GoodsInfo +where GoodsColor='红色' +group by GoodsID,GoodsName,GoodsColor,GoodsBrand,GoodsMoney,TypeID +having GoodsMoney>=300 and GoodsMoney<=600 diff --git "a/\347\254\254\345\215\201\344\270\211\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\270\211\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..864623daa7ea27aa68be18dffa73a01577a1ade5 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\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\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\256\207\347\277\224/Sql13.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\256\207\347\277\224/Sql13.sql" new file mode 100644 index 0000000000000000000000000000000000000000..24737c32266b484194e3fc359f0257b61f70bd2c --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\345\256\207\347\277\224/Sql13.sql" @@ -0,0 +1,69 @@ +--1、创建商品数据库(GoodsDB),然后建立两张表,GoodsType(商品类型表),GoodsInfo(商品信息表),表结构分别如下: +--商品类型表(GoodsType) +create database GoodsDB +go + +--商品类型表(GoodsType) +--字段名 说明 类型 长度 可否为空 约束 +--TypeID 商品类型编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--TypeName 商品类型名称 nvarchar 20 否 + +use GoodsDB +create table GoodsType +( +TypeID int primary key identity (1,1) not null, +TypeName nvarchar(20) not null +) + +create table GoodsInfo +--商品信息表(GoodsInfo) +--字段名 说明 类型 长度 可否为空 约束 +--GoodsID 商品编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--GoodsName 商品名称 nvarchar 20 否 +--GoodsColor 商品颜色 nvarchar 20 否 +--GoodsBrand 商品品牌 nvarchar 20 +--GoodsMoney 商品价格 money 否 +--TypeID 商品类型编号 int 外键,参照GoodsType中的TypeID + +( +GoodsID int primary key identity (1,1) not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20), +GoodsMoney money not null, +TypeID int references GoodsType(TypeID) +) + +--2、使用插入语句为两张表添加数据 +--商品类型表(GoodsType) +--商品类型编号 商品类型名称 +--1 服装内衣 +--2 鞋包配饰 +--3 手机数码 +insert into GoodsType values ('服装内衣'),('鞋包配饰'),('手机数码') + +--商品信息表(GoodsInfo) +--商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 +--1 提花小西装 红色 菲曼琪 300 1 +--2 百搭短裤 绿色 哥弟 100 1 +--3 无袖背心 白色 阿依莲 700 1 +--4 低帮休闲鞋 红色 菲曼琪 900 2 +--5 中跟单鞋 绿色 哥弟 400 2 +--6 平底鞋 白色 阿依莲 200 2 +--7 迷你照相机 红色 尼康 500 3 +--8 硬盘 黑色 希捷 600 3 +--9 显卡 黑色 技嘉 800 3 +insert into GoodsInfo values ('提花小西装','红色','菲曼琪','300','1'),('百搭短裤','红色','哥弟','100','1'), +('无袖背心','白色','阿依莲','700','1'),('低帮休闲鞋','红色','菲曼琪','900','2'),('中跟单鞋','绿色','哥弟','400','2') +,('平底鞋','白色','阿依莲','200','1'),('迷你照相机','红色','尼康','500','3') +,('硬盘','黑色','希捷','600','3'),('显卡','黑色','技嘉','800','3') + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo group by GoodsMoney,GoodsColor,GoodsName +order by GoodsMoney desc +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 类型编号,max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 from GoodsInfo +group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>='300' and GoodsMoney<='600' \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\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\211\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\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\200\235\351\224\220/50.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\200\235\351\224\220/50.sql" new file mode 100644 index 0000000000000000000000000000000000000000..983d38c5cab2f7264da92c2a02d834513f86f30f --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\200\235\351\224\220/50.sql" @@ -0,0 +1,1033 @@ +create database ClassicDb + +GO + + + +use ClassicDb + +GO + + + +create table StudentInfo + +( + + Id int PRIMARY key not null IDENTITY, + + StudentCode nvarchar(80), + + StudentName nvarchar(80), + + Birthday date not null, + + Sex nvarchar(2), + + ClassId int not null + +) + + + +GO + + + +-- select * from StudentInfo + + + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('01' , '赵雷' , '1990-01-01' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('02' , '钱电' , '1990-12-21' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('03' , '孙风' , '1990-12-20' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('04' , '李云' , '1990-12-06' , 'm',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('05' , '周梅' , '1991-12-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('06' , '吴兰' , '1992-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('07' , '郑竹' , '1989-01-01' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('09' , '张三' , '2017-12-20' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('10' , '李四' , '2017-12-25' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('11' , '李四' , '2012-06-06' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('12' , '赵六' , '2013-06-13' , 'f',1) + +insert into StudentInfo (StudentCode,StudentName,Birthday,Sex,ClassId) values('13' , '孙七' , '2014-06-01' , 'f',1) + + + + + +GO + + + + + +CREATE TABLE Teachers + +( + + Id int PRIMARY key not null IDENTITY, + + TeacherName nvarchar(80) + +) + + + +go + +-- select * from Teachers + + + +insert into Teachers (TeacherName) values('张三') + +insert into Teachers (TeacherName) values('李四') + +insert into Teachers (TeacherName) values('王五') + + + +GO + + + +create table CourseInfo + +( + + Id int PRIMARY key not null IDENTITY, + + CourseName NVARCHAR(80) not null, + + TeacherId int not null + +) + + + +go + +-- select * from CourseInfo + + + +insert into CourseInfo (CourseName,TeacherId) values( '语文' , 2) + +insert into CourseInfo (CourseName,TeacherId) values( '数学' , 1) + +insert into CourseInfo (CourseName,TeacherId) values( '英语' , 3) + + + +GO + + + +create table StudentCourseScore + +( + + Id int PRIMARY key not null IDENTITY, + + StudentId int not null, + + CourseId int not null, + + Score int not null + +) + +go + +-- select * from StudentCourseScore + + + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 2 , 90) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='01') , 3 , 99) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 1 , 70) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 2 , 60) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='02') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 1 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 2 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='03') , 3 , 80) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 1 , 50) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 2 , 30) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='04') , 3 , 20) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 1 , 76) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='05') , 2 , 87) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 1 , 31) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='06') , 3 , 34) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 2 , 89) + +insert into StudentCourseScore (StudentId,CourseId,Score) values((select Id from StudentInfo where StudentCode='07') , 3 , 98) +go + + +--select * from StudentCourseScore order by StudentId offset 6 rows fetch next 7 rows only + +-- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 +select + si.*, sc1.Score '数学', sc2.Score '语文' +from + StudentCourseScore sc1 +join + StudentCourseScore sc2 +on + sc1.StudentId = sc2.StudentId and sc1.CourseId = 2 and sc2.CourseId = 1 +join + StudentInfo si +on + si.Id = sc1.StudentId +where + sc1.Score > sc2.Score + +select * from StudentCourseScore where CourseId in (1,2) +select * from CourseInfo + + +-- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 +select + sc1.StudentId,sc1.CourseId,sc1.Score,sc2.CourseId,sc2.Score +from + StudentCourseScore sc1 +left join + StudentCourseScore sc2 +on + sc1.StudentId = sc2.StudentId +where + sc1.CourseId = 1 and sc2.CourseId = 2 + + +-- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) +select + sc1.StudentId, sc1.CourseId, c1.CourseName, sc1.Score, sc2.CourseId, c2.CourseName, sc2.Score +from + StudentCourseScore sc1 +left join + StudentCourseScore sc2 +on + sc1.StudentId = sc2.StudentId and sc2.CourseId = 1 +left join + CourseInfo c1 +on + c1.Id = sc1.CourseId +left join + CourseInfo c2 +on + c2.Id = sc2.CourseId +where + sc1.CourseId = 2 + +-- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 +select + sc1.StudentId, sc1.CourseId, c1.CourseName, sc1.Score, sc2.CourseId, c2.CourseName, sc2.Score +from + StudentCourseScore sc1 +left join + StudentCourseScore sc2 +on + sc1.StudentId = sc2.StudentId and sc2.CourseId = 2 +left join + CourseInfo c1 +on + c1.Id = sc1.CourseId +left join + CourseInfo c2 +on + c2.Id = sc2.CourseId +where + sc1.CourseId = 1 and sc2.Id is null + +-- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 +select + sc.StudentId '学生编号',si.StudentName '学生姓名', avg(Score) '平均成绩' +from + StudentCourseScore sc +join + StudentInfo si +on + sc.StudentId = si.Id +group by + sc.StudentId,si.StudentName +having + avg(score) > 60 + + +-- 3.查询在 成绩 表存在成绩的学生信息 +select + distinct si.* +from + StudentInfo si +join + StudentCourseScore sc +on + si.Id = sc.StudentId + + +-- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) +select + si.StudentName, si.Id, count(sc.CourseId) '课程总数', sum(score) '总成绩' +from + StudentInfo si +left join + StudentCourseScore sc +on + si.Id = sc.StudentId +group by + si.Id, si.StudentName + + +-- 4.1 查有成绩的学生信息 +select + distinct si.* +from + StudentInfo si +join + StudentCourseScore sc +on + si.Id = sc.StudentId + + +-- 5.查询「李」姓老师的数量 +select + count(*) '数量' +from + Teachers +where + TeacherName like '李%' + +-- 6.查询学过「张三」老师授课的同学的信息 +select + si.*, t.TeacherName +from + StudentInfo si +join + StudentCourseScore sc +on + si.Id = sc.StudentId +join + CourseInfo ci +on + ci.Id = sc.CourseId +join + Teachers t +on + ci.TeacherId = t.Id +where + t.TeacherName = '张三' + +select * from CourseInfo +select * from StudentInfo +select * from Teachers +select * from StudentCourseScore +-- 7.查询没有学全所有课程的同学的信息 +select + * +from + StudentInfo +where + Id in (select sc.StudentId from StudentCourseScore sc group by sc.StudentId having count(distinct CourseId) < 3) + +-- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 +select + * +from + StudentInfo +where + id in (select distinct StudentId + from StudentCourseScore + where CourseId in (select CourseId from StudentCourseScore sc join StudentInfo si on si.Id = sc.StudentId where si.StudentCode = 01) + ) and StudentCode <> 01 + +-- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 +select + * +from + StudentInfo +where + id in (select + StudentId + from + StudentCourseScore + where + CourseId in (select CourseId from StudentCourseScore) + group by + StudentId + having + count(CourseId) = (select count(CourseId) from StudentCourseScore where StudentId = 1) and sum(CourseId) = (select sum(CourseId) from StudentCourseScore where StudentId = 1) + ) + and id <> 1 +-- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 +select + * +from + StudentInfo +where + id not in(select + si.Id + from + StudentInfo si + join + StudentCourseScore sc + on + si.Id = sc.StudentId + join + CourseInfo ci + on + ci.Id = sc.CourseId + join + Teachers t + on + ci.TeacherId = t.Id + where + t.TeacherName = '张三' + ) + + +-- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 +select + si.Id,si.StudentName, avg(Score) '平均成绩' +from + StudentCourseScore sc +join + StudentInfo si +on + sc.StudentId = si.Id +where + sc.StudentId in (select + StudentId + from + StudentCourseScore + where + Score < 60 + group by + StudentId + having + count(*) >= 2 + ) +group by + si.Id,si.StudentName + +-- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 +select + si.* +from + StudentCourseScore sc +join + CourseInfo ci +on + sc.CourseId = ci.Id +join + StudentInfo si +on + si.Id = sc.StudentId +where + sc.score < 60 and ci.CourseName = '数学' + +-- 13.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 +select + sc.StudentId, sc.CourseId, sc.score, a.avg_s +from + StudentCourseScore sc +join + (select StudentId s_id, avg(score) avg_s from StudentCourseScore group by StudentId) a +on + a.s_id = sc.StudentId +order by + a.avg_s desc + +-- 14.查询各科成绩最高分、最低分和平均分: +select + ci.Id, ci.CourseName, max(score) '最高分', min(score) '最低分', avg(score) '平均分' +from + StudentCourseScore sc +join + CourseInfo ci +on + ci.Id = sc.CourseId +group by + ci.Id, ci.CourseName + +-- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 + +/* + + 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90 + + + + 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列 + + + + 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺 + +*/ +go +create view v_s1 +as +select + a.课程号, cast(round(cast(a.人数 as float) / cast(b.人数 as float) * 100, 1) as varchar) + '%' '及格率' +from + (select CourseId '课程号', count(*) '人数' from StudentCourseScore where score >= 60 group by CourseId) a +join + (select CourseId '课程号', count(*) '人数' from StudentCourseScore group by CourseId) b +on + a.课程号 = b.课程号 +go + +create view v_s2 +as +select + a.课程号, cast(round(cast(a.人数 as float) / cast(b.人数 as float) * 100, 1) as varchar) + '%' '中等率' +from + (select CourseId '课程号', count(*) '人数' from StudentCourseScore where score >= 70 and score <=80 group by CourseId) a +join + (select CourseId '课程号', count(*) '人数' from StudentCourseScore group by CourseId) b +on + a.课程号 = b.课程号 +go + +create view v_s3 +as +select + a.课程号, cast(round(cast(a.人数 as float) / cast(b.人数 as float) * 100, 1) as varchar) + '%' '优良率' +from + (select CourseId '课程号', count(*) '人数' from StudentCourseScore where score >= 80 and score <=90 group by CourseId) a +join + (select CourseId '课程号', count(*) '人数' from StudentCourseScore group by CourseId) b +on + a.课程号 = b.课程号 +go + +create view v_s4 +as +select + a.课程号, cast(round(cast(a.人数 as float) / cast(b.人数 as float) * 100, 1) as varchar) + '%' '优秀率' +from + (select CourseId '课程号', count(*) '人数' from StudentCourseScore where score >= 90 group by CourseId) a +join + (select CourseId '课程号', count(*) '人数' from StudentCourseScore group by CourseId) b +on + a.课程号 = b.课程号 +go + +select + ci.Id '课程号', ci.CourseName, count(*) '选修人数', max(score) '最高分', min(score) '最低分', avg(Score) '平均分', isnull(v_s1.及格率, '0%') '及格率', isnull(v_s2.中等率, '0%') '中等率', isnull(v_s3.优良率, '0%') '优良率', isnull(v_s4.优秀率, '0%') '优秀率' +from + StudentCourseScore sc +left join + CourseInfo ci +on + sc.CourseId = ci.Id +left join + v_s1 +on + v_s1.课程号 = ci.Id +left join + v_s2 +on + v_s2.课程号 = ci.Id +left join + v_s3 +on + v_s3.课程号 = ci.Id +left join + v_s4 +on + v_s4.课程号 = ci.Id +group by + ci.Id, ci.CourseName, v_s1.及格率, v_s2.中等率, v_s3.优良率, v_s4.优秀率 + +--优化 +select + ci.Id '课程号', ci.CourseName, count(*) '选修人数', max(score) '最高分', min(score) '最低分', avg(Score) '平均分', + cast(round((cast(count(case when Score >= 60 then 1 else null end) as float)/cast(count(Score) as float))*100,1) as varchar) + '%' '及格率', + cast(round((cast(count(case when Score between 70 and 80 then 1 else null end) as float)/cast(count(Score) as float))*100,1) as varchar) + '%' '中等率', + cast(round((cast(count(case when Score between 80 and 90 then 1 else null end) as float)/cast(count(Score) as float))*100,1) as varchar) + '%' '优良率', + cast(round((cast(count(case when Score >= 90 then 1 else null end) as float)/cast(count(Score) as float))*100,1) as varchar) + '%' '优秀率' +from + StudentCourseScore sc +left join + CourseInfo ci +on + sc.CourseId = ci.Id +group by + ci.Id, ci.CourseName + +-- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 +select + sc1.CourseId, sc1.StudentId, sc1.Score, count(distinct sc2.Score) + 1 '排名' +from + StudentCourseScore sc1 +left join + StudentCourseScore sc2 +on + sc1.CourseId = sc2.CourseId and sc1.Score < sc2.Score +group by + sc1.CourseId, sc1.StudentId, sc1.Score +order by + sc1.CourseId, Score desc + +-- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 +select + a.StudentID, a.总成绩, count(b.总成绩) + 1 '排名' +from + (select StudentId 'StudentID', sum(Score) '总成绩' from StudentCourseScore s1 group by StudentId) a +left join + (select StudentId 'StudentID', sum(Score) '总成绩' from StudentCourseScore s1 group by StudentId) b +on + a.总成绩 < b.总成绩 +group by + a.StudentID, a.总成绩 +order by + a.总成绩 desc + +-- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 +select + a.StudentID, a.总成绩, count(distinct b.总成绩) + 1 '排名' +from + (select StudentId 'StudentID', sum(Score) '总成绩' from StudentCourseScore s1 group by StudentId) a +left join + (select StudentId 'StudentID', sum(Score) '总成绩' from StudentCourseScore s1 group by StudentId) b +on + a.总成绩 < b.总成绩 +group by + a.StudentID, a.总成绩 +order by + a.总成绩 desc + +-- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 +select + s.CourseId, c.CourseName, count(*) '课程人数' , + cast(round((cast(count(case when s.Score between 85 and 100 then 1 else null end) as float)/cast(count(*) as float))*100, 1) as varchar) + '%' '[100-85]', + cast(round((cast(count(case when s.Score between 70 and 85 then 1 else null end) as float)/cast(count(*) as float))*100, 1) as varchar) + '%' '[70-85]', + cast(round((cast(count(case when s.Score between 60 and 70 then 1 else null end) as float)/cast(count(*) as float))*100, 1) as varchar) + '%' '[60-70]', + cast(round((cast(count(case when s.Score between 0 and 60 then 1 else null end) as float)/cast(count(*) as float))*100, 1) as varchar) + '%' '[0-60]' +from + StudentCourseScore s +join + CourseInfo c +on + s.CourseId = c.Id +group by + s.CourseId, c.CourseName + +-- 18.查询各科成绩前三名的记录 +select + s1.CourseId, s1.StudentId, s1.Score, count(distinct s2.Score) + 1 '名次' +from + StudentCourseScore s1 +left join + StudentCourseScore s2 +on + s1.CourseId = s2.CourseId and s1.Score < s2.Score +group by + s1.CourseId, s1.StudentId, s1.Score +having + (count(distinct s2.Score) + 1) between 1 and 3 +order by + s1.CourseId, s1.Score desc + +-- 19.查询每门课程被选修的学生数 +select + s.CourseId, c.CourseName, count(distinct s.StudentId) '学生数' +from + StudentCourseScore s +join + CourseInfo c +on + s.CourseId = c.Id +group by + CourseId, CourseName + +-- 20.查询出只选修两门课程的学生学号和姓名 +select + si.Id, si.StudentName +from + StudentCourseScore sc +join + StudentInfo si +on + si.Id = sc.StudentId +group by + si.Id,si.StudentName +having + count(distinct sc.CourseId) = 2 + +-- 21.查询男生、女生人数 +select + case when sex='f' then '女' else '男' end '性别', count(*) '人数' +from + StudentInfo +group by + Sex + +-- 22.查询名字中含有「风」字的学生信息 +select + * +from + StudentInfo +where + StudentName like '%风%' + +-- 23.查询同名同性学生名单,并统计同名人数 +select + s1.*,a.人数 +from + StudentInfo s1 +join + StudentInfo s2 +on + s1.Id <> s2.Id and s1.StudentName = s2.StudentName and s1.Sex = s2.Sex +join + (select + s1.StudentName, count(*) '人数' + from + StudentInfo s1 + join + StudentInfo s2 + on + s1.Id <> s2.Id and s1.StudentName = s2.StudentName and s1.Sex = s2.Sex + group by + s1.StudentName + ) a +on + a.StudentName = s1.StudentName + + +-- 24.查询 1990 年出生的学生名单 +select + * +from + StudentInfo +where + DATENAME(yy,Birthday) = '1990' + +-- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 +select + c.Id, c.CourseName, avg(Score) +from + StudentCourseScore sc +join + CourseInfo c +on + sc.CourseId = c.Id +group by + c.Id, c.CourseName +order by + avg(Score) desc,c.Id asc + +-- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 +select + s.Id, s.StudentName, avg(Score) +from + StudentCourseScore sc +join + StudentInfo s +on + sc.StudentId = s.Id +group by + s.Id, s.StudentName +having + avg(score) >= 85 + +-- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 +select + si.StudentName, score +from + StudentCourseScore s +join + CourseInfo c +on + s.CourseId = c.Id +join + StudentInfo si +on + si.Id = s.StudentId +where + c.CourseName = '数学' and Score < 60 + + +-- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) +select + si.Id, si.StudentName, c.CourseName, sc.Score +from + StudentInfo si +left join + StudentCourseScore sc +on + si.Id = sc.StudentId +left join + CourseInfo c +on + c.Id = sc.CourseId + +-- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 $ +select + si.StudentName, c.CourseName, sc.Score +from + StudentInfo si +join + StudentCourseScore sc +on + si.Id = sc.StudentId +join + CourseInfo c +on + c.Id = sc.CourseId +join + (select StudentId, CourseId from StudentCourseScore where Score > 70) a +on + si.Id = a.StudentId and sc.CourseId = a.CourseId + +-- 30.查询不及格的课程 +select + s.*, c.CourseName +from + StudentCourseScore s +join + CourseInfo c +on + s.CourseId = c.Id +where + score < 60 + +-- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 +select + id, StudentName +from + StudentInfo +where + id in (select + StudentId + from + StudentCourseScore + where + CourseId = 1 and Score > 80 + ) + +-- 32.求每门课程的学生人数 +select + CourseId, count(distinct StudentId) '人数' +from + StudentCourseScore +group by + CourseId + +-- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 +select + top 1 si.*, sc.CourseId, c.CourseName, sc.Score +from + StudentCourseScore sc +join + CourseInfo c +on + sc.CourseId = c.Id +join + Teachers t +on + t.Id = c.TeacherId +join + StudentInfo si +on + si.Id = sc.StudentId +where + t.TeacherName = '张三' +order by + sc.Score desc + +--34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 +select + si.*, sc.CourseId, c.CourseName, sc.Score +from + StudentCourseScore sc +join + CourseInfo c +on + sc.CourseId = c.Id +join + Teachers t +on + t.Id = c.TeacherId +join + StudentInfo si +on + si.Id = sc.StudentId +where + t.TeacherName = '张三' and + sc.Score = ( + select + max(score) + from + StudentCourseScore sc + join + CourseInfo c + on + sc.CourseId = c.Id + join + Teachers t + on + t.Id = c.TeacherId + join + StudentInfo si + on + si.Id = sc.StudentId + where + t.TeacherName = '张三' + ) + +-- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 +select + s1.StudentId, s1.CourseId, s2.StudentId, s2.CourseId, s2.Score +from + StudentCourseScore s1 +join + StudentCourseScore s2 +on + s1.CourseId <> s2.CourseId and s1.Score = s2.Score + +-- 36.查询每门功成绩最好的前两名 +select + s1.CourseId, s1.StudentId, s1.Score +from + StudentCourseScore s1 +left join + StudentCourseScore s2 +on + s1.CourseId = s2.CourseId and s1.Score < s2.Score +group by + s1.CourseId, s1.StudentId, s1.Score +having + (count(s2.Score) + 1) between 1 and 2 +order by + s1.CourseId, s1.Score desc + +--2 +select * from + (select top 2 * from StudentCourseScore where CourseId = 1 order by Score desc) a +union +select * from + (select top 2 * from StudentCourseScore where CourseId = 2 order by Score desc) b +union +select * from + (select top 2 * from StudentCourseScore where CourseId = 3 order by Score desc) c +order by + CourseId + +-- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 +select + CourseId, count(distinct StudentId) +from + StudentCourseScore +group by + CourseId +having + count(distinct StudentId) > 5 + + +-- 38.检索至少选修两门课程的学生学号 +select + StudentId +from + StudentCourseScore +group by + StudentId +having + count(distinct CourseId) > 2 + +-- 39.查询选修了全部课程的学生信息 +select + * +from + StudentInfo +where + Id in (select + StudentId + from + StudentCourseScore + group by + StudentId + having + count(distinct CourseId) = (select count(distinct Id) from CourseInfo) + ) + +-- 40.查询各学生的年龄,只按年份来算 +select + *,DATEDIFF(yy,Birthday,GETDATE()) '年龄' +from + StudentInfo + +-- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 $ +select + *,(DATEDIFF(yy,Birthday,GETDATE()) - (case when (DATEPART(dy,GETDATE()) < DATEPART(dy,Birthday)) then 1 else 0 end)) '年龄' +from + StudentInfo + +-- 42.查询本周过生日的学生 $ +select + * +from + StudentInfo +where + dateadd(yy,datediff(yy,Birthday,GETDATE()),Birthday) + between + cast(getdate() - (DATEPART(DW,GETDATE()) - 2) as date) and + cast((getdate() - (DATEPART(DW,GETDATE()) - 2) + 6) as date) + +-- 43.查询下周过生日的学生 +select + * +from + StudentInfo +where + dateadd(yy,datediff(yy,Birthday,GETDATE()),Birthday) + between + cast((getdate() - (DATEPART(DW,GETDATE()) - 2) + 7) as date) and + cast((getdate() - (DATEPART(DW,GETDATE()) - 2) + 14) as date) + +select (getdate() - (DATEPART(DW,GETDATE()) - 2) + 6) +-- 44.查询本月过生日的学生 +select + * +from + StudentInfo +where + DATEPART(mm,GETDATE()) = DATEPART(mm,Birthday) + +-- 45.查询下月过生日的学生 +select + * +from + StudentInfo +where + DATEPART(mm,GETDATE()) + 1 = DATEPART(mm,Birthday) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\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\211\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..f1e1c5403409b4fc1b1a32e0ad599c9e446020ad --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\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,73 @@ +use master +go + +if exists(select * from sys.databases where name = 'GoodsDB') + drop database GoodsDB +go + +create database GoodsDB +go + +use GoodsDB +go + +create table GoodsType +( + TypeID int primary key identity not null, + TypeName nvarchar(20) check(len(TypeName) <= 20) +) +go + +create table GoodsInfo +( + GoodsID int primary key identity not null, + GoodsName nvarchar(20) not null, + GoodsColor nvarchar(20) not null, + GoodsBrand nvarchar(20), + GoodsMoney money not null, + TypeID int references GoodsType(TypeID) +) +go + +insert into GoodsType values +('服装内衣'), +('鞋包配饰'), +('手机数码') +go + +insert into GoodsInfo values +('提花小西装', '红色', '菲曼琪', 300, 1), +('百搭短裤', '绿色', '哥弟', 100, 1), +('无袖背心', '白色', '阿依莲', 700, 1), +('低帮休闲鞋', '红色', '菲曼琪', 900, 2), +('中跟单鞋', '绿色', '哥弟', 400, 2), +('平底鞋', '白色', '阿依莲', 200, 2), +('迷你照相机', '红色', '尼康', 500, 3), +('硬盘', '黑色', '希捷', 600, 3), +('显卡', '黑色', '技嘉', 800, 3), +('显卡', '黑色', '技嘉', 800.2, 3) +go + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select + GoodsName '商品名称', GoodsColor '商品颜色', GoodsMoney '商品价格' +from + GoodsInfo +where + GoodsMoney = (select max(GoodsMoney) from GoodsInfo) + +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select + TypeID '商品类型编号', max(GoodsMoney) '最高价格', min(GoodsMoney) '最低价格', round(avg(GoodsMoney), 2) '平均价格' +from + GoodsInfo +group by + TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select + * +from + GoodsInfo +where + (GoodsColor = '红色') and (GoodsMoney between 300 and 600) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\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\211\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..7b5bf5db13fd7bcefa2c6d0aa9327a9f35ab5f3b --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\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,86 @@ +create database HOUSE_DB +on +( + name='HOUSE_DB', + filename='d:\HOUSE_DB.mdf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) +log on +( + name='HOUSE_DB_log', + filename='d:\HOUSE_DB.ldf', + size=5mb, + maxsize=50mb, + filegrowth=1mb +) +go + +use HOUSE_DB +go + +create table HOUSE_TYPE +( + [type_id] int not null primary key identity, + [type_name] varchar(50) not null unique +) +go + +create table HOUSE +( + house_id int not null primary key identity, + house_name varchar(50) not null, + house_price float not null default(0), + [type_id] int not null references HOUSE_TYPE([type_id]) +) +go + +insert into HOUSE_TYPE values +('小户型'), +('经济型'), +('别墅') +go + +insert into HOUSE values +('dog',3000,1), +('cat',4000,2), +('horse',5000,3) +go + +--查询所有房屋信息 +select * from HOUSE + +--使用模糊查询包含”型“字的房屋类型信息 +select + * +from + HOUSE_TYPE +where + [type_name] like '%型%' + +--查询出房屋的名称和租金,并且按照租金降序排序 +select + house_name, house_price +from + HOUSE +order by + house_price desc + +--使用连接查询,查询信息,显示房屋名称和房屋类型名称 +select + h.house_name, ht.[type_name] +from + HOUSE h +join + HOUSE_TYPE ht +on + h.[type_id] = ht.[type_id] + +--查询所有房屋中月租最高的房屋,显示最高的租金和房屋名称 +select + house_price, house_name +from + HOUSE +where + house_price = (select max(house_price) from HOUSE) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\200\235\351\224\220/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\200\235\351\224\220/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..bf65451d58185790a878158e481dc7f237b3af00 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\346\200\235\351\224\220/SQLQuery3.sql" @@ -0,0 +1,84 @@ +use master +go + +create database StarManagerDB +go + +use StarManagerDB +go + +create table StarType +( + T_NO int not null primary key identity, + T_NAME nvarchar(20) +) +go + +create table StarInfo +( + S_NO int not null primary key identity, + S_NAME nvarchar(20) not null, + S_AGE int not null, + S_HOBBY nvarchar(20), + S_NATIVE nvarchar(20) default('中国大陆'), + S_T_NO int references StarType(T_NO) +) +go + +insert into StarType values +('体育明星'), +('IT明星'), +('相声演员') +go + +insert into StarInfo values +('梅西', 30, '射门', '阿根廷', 1), +('科比', 35, '过人', '美国', 1), +('蔡景现', 40, '敲代码', '中国', 2), +('马斯克', 36, '造火箭', '外星人', 2), +('郭德纲', 50, '相声', '中国', 3), +('黄铮', 41, '拼多多', '中国', 2) +go + + +--3、查询年龄最大的3个明星的姓名,特技和籍贯信息,要求使用别名显示列名。 +select + top 3 S_NAME '姓名', S_HOBBY '特技', S_NATIVE '籍贯信息' +from + StarInfo +order by + S_AGE desc + +--4、按明星类型编号分类查询明星人数,明星平均年龄,显示明星人数大于2的分组信息,要求使用别名显示列名。 +select + count(*) '人数', avg(S_AGE) '平均年龄' +from + StarInfo +group by + S_T_NO +having + count(*) > 2 + +--5、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +select + si.S_NAME '姓名', si.S_HOBBY '特技', si.S_NATIVE '籍贯信息' +from + StarInfo si +join + StarType st +on + si.S_T_NO = st.T_NO +where + st.T_NAME = '体育明星' and si.S_AGE = + ( + select + max(S_AGE) + from + StarInfo si + join + StarType st + on + si.S_T_NO = st.T_NO + where + st.T_NAME = '体育明星' + ) \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\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\211\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\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\277\233\345\220\211/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\277\233\345\220\211/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..c35d456e1334e01b4974532c539581dbff01f9aa --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\231\210\350\277\233\345\220\211/SQLQuery2.sql" @@ -0,0 +1,69 @@ +--1、创建商品数据库(GoodsDB),然后建立两张表,GoodsType(商品类型表),GoodsInfo(商品信息表),表结构分别如下: +--商品类型表(GoodsType) +create database GoodsDB +go + +--商品类型表(GoodsType) +--字段名 说明 类型 长度 可否为空 约束 +--TypeID 商品类型编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--TypeName 商品类型名称 nvarchar 20 否 + +use GoodsDB +create table GoodsType +( +TypeID int primary key identity (1,1) not null, +TypeName nvarchar(20) not null +) + +create table GoodsInfo +--商品信息表(GoodsInfo) +--字段名 说明 类型 长度 可否为空 约束 +--GoodsID 商品编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--GoodsName 商品名称 nvarchar 20 否 +--GoodsColor 商品颜色 nvarchar 20 否 +--GoodsBrand 商品品牌 nvarchar 20 +--GoodsMoney 商品价格 money 否 +--TypeID 商品类型编号 int 外键,参照GoodsType中的TypeID + +( +GoodsID int primary key identity (1,1) not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20), +GoodsMoney money not null, +TypeID int references GoodsType(TypeID) +) + +--2、使用插入语句为两张表添加数据 +--商品类型表(GoodsType) +--商品类型编号 商品类型名称 +--1 服装内衣 +--2 鞋包配饰 +--3 手机数码 +insert into GoodsType values ('服装内衣'),('鞋包配饰'),('手机数码') + +--商品信息表(GoodsInfo) +--商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 +--1 提花小西装 红色 菲曼琪 300 1 +--2 百搭短裤 绿色 哥弟 100 1 +--3 无袖背心 白色 阿依莲 700 1 +--4 低帮休闲鞋 红色 菲曼琪 900 2 +--5 中跟单鞋 绿色 哥弟 400 2 +--6 平底鞋 白色 阿依莲 200 2 +--7 迷你照相机 红色 尼康 500 3 +--8 硬盘 黑色 希捷 600 3 +--9 显卡 黑色 技嘉 800 3 +insert into GoodsInfo values ('提花小西装','红色','菲曼琪','300','1'),('百搭短裤','红色','哥弟','100','1'), +('无袖背心','白色','阿依莲','700','1'),('低帮休闲鞋','红色','菲曼琪','900','2'),('中跟单鞋','绿色','哥弟','400','2') +,('平底鞋','白色','阿依莲','200','1'),('迷你照相机','红色','尼康','500','3') +,('硬盘','黑色','希捷','600','3'),('显卡','黑色','技嘉','800','3') + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo group by GoodsMoney,GoodsColor,GoodsName +order by GoodsMoney desc +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 类型编号,max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 from GoodsInfo +group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>='300' and GoodsMoney<='600' diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\251\345\233\275\346\226\207/.keep" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\251\345\233\275\346\226\207/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\251\345\233\275\346\226\207/SQLQuery1(5).sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\251\345\233\275\346\226\207/SQLQuery1(5).sql" new file mode 100644 index 0000000000000000000000000000000000000000..b2da2acb3f0f382f0156ff8e0f9df799b6430339 --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\237\251\345\233\275\346\226\207/SQLQuery1(5).sql" @@ -0,0 +1,70 @@ +create database GoodsDB +on +( +name='GoodsDB', +filename='D:\SQL\GoodsDB.mdf', +size=5mb, +maxsize=50mb, +filegrowth=10% +) +log on +( +name='GoodsDB_log', +filename='D:\SQL\GoodsDB_log.mdf', +size=5mb, +maxsize=50mb, +filegrowth=10% +) +go + +use GoodsDB +go + +create table GoodsType +( +TypeID int primary key identity not null, +TypeName nvarchar(20) not null +) +go + +create table GoodsInfo +( +GoodsID int primary key identity not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20), +GoodsMoney money not null, +TypeID int references GoodsType(TypeID) +) + +insert into GoodsType values +('服装内衣'), +('鞋包配饰'), +('手机数码') +go + +insert into GoodsInfo values +('提花小西装',' 红色',' 菲曼琪', 300, 1), +('百搭短裤', '绿色', '哥弟' ,100 ,1), +('无袖背心', '白色', '阿依莲' ,700 ,1), +('低帮休闲鞋', '红色', '菲曼琪' ,900 ,2), +('平底鞋', '白色', '阿依莲' ,200, 2), +('迷你照相机', '红色', '尼康' ,500 ,3), +('硬盘' ,'黑色', '希捷' ,600 ,3), +('显卡', '黑色', '技嘉' ,800 ,3) +go + +select * from GoodsType +select * from GoodsInfo +go + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo where GoodsMoney=(select MAX(GoodsMoney) from GoodsInfo) +go + +--4、按商品类型编号分组查询商品最高价格,最高价格和平均价格,要求使用别名显示列名 +select TypeID 类型编号,MAX(GoodsMoney) 最高价格,MIN(GoodsMoney) 最高价格,AVG(GoodsMoney) 平均价格 from GoodsInfo group by TypeID +go + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney between 300 and 600 \ No newline at end of file diff --git "a/\347\254\254\345\215\201\344\270\211\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\211\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\211\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\270\211\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..c35d456e1334e01b4974532c539581dbff01f9aa --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\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,69 @@ +--1、创建商品数据库(GoodsDB),然后建立两张表,GoodsType(商品类型表),GoodsInfo(商品信息表),表结构分别如下: +--商品类型表(GoodsType) +create database GoodsDB +go + +--商品类型表(GoodsType) +--字段名 说明 类型 长度 可否为空 约束 +--TypeID 商品类型编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--TypeName 商品类型名称 nvarchar 20 否 + +use GoodsDB +create table GoodsType +( +TypeID int primary key identity (1,1) not null, +TypeName nvarchar(20) not null +) + +create table GoodsInfo +--商品信息表(GoodsInfo) +--字段名 说明 类型 长度 可否为空 约束 +--GoodsID 商品编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--GoodsName 商品名称 nvarchar 20 否 +--GoodsColor 商品颜色 nvarchar 20 否 +--GoodsBrand 商品品牌 nvarchar 20 +--GoodsMoney 商品价格 money 否 +--TypeID 商品类型编号 int 外键,参照GoodsType中的TypeID + +( +GoodsID int primary key identity (1,1) not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20), +GoodsMoney money not null, +TypeID int references GoodsType(TypeID) +) + +--2、使用插入语句为两张表添加数据 +--商品类型表(GoodsType) +--商品类型编号 商品类型名称 +--1 服装内衣 +--2 鞋包配饰 +--3 手机数码 +insert into GoodsType values ('服装内衣'),('鞋包配饰'),('手机数码') + +--商品信息表(GoodsInfo) +--商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 +--1 提花小西装 红色 菲曼琪 300 1 +--2 百搭短裤 绿色 哥弟 100 1 +--3 无袖背心 白色 阿依莲 700 1 +--4 低帮休闲鞋 红色 菲曼琪 900 2 +--5 中跟单鞋 绿色 哥弟 400 2 +--6 平底鞋 白色 阿依莲 200 2 +--7 迷你照相机 红色 尼康 500 3 +--8 硬盘 黑色 希捷 600 3 +--9 显卡 黑色 技嘉 800 3 +insert into GoodsInfo values ('提花小西装','红色','菲曼琪','300','1'),('百搭短裤','红色','哥弟','100','1'), +('无袖背心','白色','阿依莲','700','1'),('低帮休闲鞋','红色','菲曼琪','900','2'),('中跟单鞋','绿色','哥弟','400','2') +,('平底鞋','白色','阿依莲','200','1'),('迷你照相机','红色','尼康','500','3') +,('硬盘','黑色','希捷','600','3'),('显卡','黑色','技嘉','800','3') + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo group by GoodsMoney,GoodsColor,GoodsName +order by GoodsMoney desc +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 类型编号,max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 from GoodsInfo +group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>='300' and GoodsMoney<='600' diff --git "a/\347\254\254\345\215\201\344\270\211\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\211\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..24737c32266b484194e3fc359f0257b61f70bd2c --- /dev/null +++ "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\351\273\204\345\274\272/SQLQuery1.sql" @@ -0,0 +1,69 @@ +--1、创建商品数据库(GoodsDB),然后建立两张表,GoodsType(商品类型表),GoodsInfo(商品信息表),表结构分别如下: +--商品类型表(GoodsType) +create database GoodsDB +go + +--商品类型表(GoodsType) +--字段名 说明 类型 长度 可否为空 约束 +--TypeID 商品类型编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--TypeName 商品类型名称 nvarchar 20 否 + +use GoodsDB +create table GoodsType +( +TypeID int primary key identity (1,1) not null, +TypeName nvarchar(20) not null +) + +create table GoodsInfo +--商品信息表(GoodsInfo) +--字段名 说明 类型 长度 可否为空 约束 +--GoodsID 商品编号 int 否 主键约束,自增约束,标识种子和标识增量都是1 +--GoodsName 商品名称 nvarchar 20 否 +--GoodsColor 商品颜色 nvarchar 20 否 +--GoodsBrand 商品品牌 nvarchar 20 +--GoodsMoney 商品价格 money 否 +--TypeID 商品类型编号 int 外键,参照GoodsType中的TypeID + +( +GoodsID int primary key identity (1,1) not null, +GoodsName nvarchar(20) not null, +GoodsColor nvarchar(20) not null, +GoodsBrand nvarchar(20), +GoodsMoney money not null, +TypeID int references GoodsType(TypeID) +) + +--2、使用插入语句为两张表添加数据 +--商品类型表(GoodsType) +--商品类型编号 商品类型名称 +--1 服装内衣 +--2 鞋包配饰 +--3 手机数码 +insert into GoodsType values ('服装内衣'),('鞋包配饰'),('手机数码') + +--商品信息表(GoodsInfo) +--商品编号 商品名称 商品颜色 商品品牌 商品价格 商品类型编号 +--1 提花小西装 红色 菲曼琪 300 1 +--2 百搭短裤 绿色 哥弟 100 1 +--3 无袖背心 白色 阿依莲 700 1 +--4 低帮休闲鞋 红色 菲曼琪 900 2 +--5 中跟单鞋 绿色 哥弟 400 2 +--6 平底鞋 白色 阿依莲 200 2 +--7 迷你照相机 红色 尼康 500 3 +--8 硬盘 黑色 希捷 600 3 +--9 显卡 黑色 技嘉 800 3 +insert into GoodsInfo values ('提花小西装','红色','菲曼琪','300','1'),('百搭短裤','红色','哥弟','100','1'), +('无袖背心','白色','阿依莲','700','1'),('低帮休闲鞋','红色','菲曼琪','900','2'),('中跟单鞋','绿色','哥弟','400','2') +,('平底鞋','白色','阿依莲','200','1'),('迷你照相机','红色','尼康','500','3') +,('硬盘','黑色','希捷','600','3'),('显卡','黑色','技嘉','800','3') + +--3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 +select top 1 GoodsName 商品名称,GoodsColor 商品颜色,GoodsMoney 商品价格 from GoodsInfo group by GoodsMoney,GoodsColor,GoodsName +order by GoodsMoney desc +--4、按商品类型编号分组查询商品最高价格,最低价格和平均价格,要求使用别名显示列名 +select TypeID 类型编号,max(GoodsMoney) 最高价格,min(GoodsMoney) 最低价格,avg(GoodsMoney) 平均价格 from GoodsInfo +group by TypeID + +--5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 +select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>='300' and GoodsMoney<='600' \ 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/.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/\344\275\234\344\270\2321.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\234\344\270\2321.sql" new file mode 100644 index 0000000000000000000000000000000000000000..e6101929d426a5dff02af47756ea5a62809dd8cb --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\234\344\270\2321.sql" @@ -0,0 +1,135 @@ +use master +go +create database TBL +on +( + name='TBL', + filename='D:\TBL.mdf', + size=5MB, + maxsize=20MB, + filegrowth=10% +) +log on +( + name='TBL_log', + filename='D:\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 TC.CardID 卡号,UserName 用户名,CID 机器编,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.CID 机器编号 , COUNT(*) 次数 , SUM(fee) 总金额 from TBL_Computer TC + inner join TBL_Record TR on TC.CID=TR.CID group by TC.CID +--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.CID 机器编号,COUNT(*) 使用次数 from TBL_Computer TC + inner join TBL_Record TR on TC.CID=TR.CID + group by TC.CID + having TC.CID=(select top 1 CID from TBL_Record group by CID order by COUNT(*) DESC) +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 + select TC.CardID 卡号 , UserName 用户名 , CID 机器编号 , 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,CID +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + select TC.CardID 上网卡号,TC.UserName 用户名,TR.CID 机器号,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.CID,BeginTime,Endtime + having DATENAME(dw,BeginTime)='星期六' or DATEPART(dw,BeginTime)=1 +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + select TC.CardID 上网卡号,TC.UserName 用户名,TR.CID 机器号,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.CID,BeginTime,Endtime + having datediff(hh,Endtime,BeginTime)>=12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + select top 3 TC.CardID 上网卡号,TC.UserName 用户名,TR.CID 机器号,BeginTime 开始时间,Endtime 结束时间,fee 消费金额 + from TBL_Card TC inner join TBL_Record TR + on TC.CardID=TR.CardID + order by fee desc + + select TC.CardID,userName,TR.CID, 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 + + \ 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/\344\275\234\344\270\2322.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\234\344\270\2322.sql" new file mode 100644 index 0000000000000000000000000000000000000000..cfff09b0c0ca477b650c1528e573b35b858d6161 --- /dev/null +++ "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\344\275\234\344\270\2322.sql" @@ -0,0 +1,134 @@ +use master +go +create database TBL +on +( + name='TBL', + filename='D:\TBL.mdf', + size=5MB, + maxsize=20MB, + filegrowth=10% +) +log on +( + name='TBL_log', + filename='D:\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 TC.CardID 卡号,UserName 用户名,CID 机器编,BeginTime 开始时间,Endtime 结束时间,fee 消费金额 from TBL_Card TC + inner join TBL_Record TR on TC.CardID=TR.CardID + order by fee desc +--2. 查询出每台机器上的上网次数和消费的总金额 + select TC.CID 机器编号 , COUNT(*) 次数 , SUM(fee) 总金额 from TBL_Computer TC + inner join TBL_Record TR on TC.CID=TR.CID group by TC.CID +--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.CID 机器编号,COUNT(*) 使用次数 from TBL_Computer TC + inner join TBL_Record TR on TC.CID=TR.CID + group by TC.CID + having TC.CID=(select top 1 CID from TBL_Record group by CID order by COUNT(*) DESC) +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 + select TC.CardID 卡号 , UserName 用户名 , CID 机器编号 , 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,CID +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + select TC.CardID 上网卡号,TC.UserName 用户名,TR.CID 机器号,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.CID,BeginTime,Endtime + having DATENAME(dw,BeginTime)='星期六' or DATEPART(dw,BeginTime)=1 +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + select TC.CardID 上网卡号,TC.UserName 用户名,TR.CID 机器号,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.CID,BeginTime,Endtime + having datediff(hh,Endtime,BeginTime)>=12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 + select top 3 TC.CardID 上网卡号,TC.UserName 用户名,TR.CID 机器号,BeginTime 开始时间,Endtime 结束时间,fee 消费金额 + from TBL_Card TC inner join TBL_Record TR + on TC.CardID=TR.CardID + order by fee desc + + select TC.CardID,userName,TR.CID, 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 + + \ 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\215\242\345\256\201/.keep" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\215\242\345\256\201/.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\215\242\345\256\201/SQLQuery2.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\215\242\345\256\201/SQLQuery2.sql" new file mode 100644 index 0000000000000000000000000000000000000000..bbb23b8ae1b1133198702fcdc127928813b39636 --- /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\215\242\345\256\201/SQLQuery2.sql" @@ -0,0 +1,117 @@ +use master +go + +create database wangba +on +( + name='wangba', + filename='F:\wangba.mdf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +LOG ON +( + name='wangba_log', + filename='F:\wangba_log.ldf', + size=5mb, + maxsize=100mb, + filegrowth=10% +) +go + +use wangba +go + +create table tbl_card +( + ID varchar(30) primary key not null, + PassWard nvarchar(50), + Balance money, + userName nvarchar(20) +) + +create table tbl_computer +( + ID char(30) primary key not null, + OnUse char(1) check(OnUse in(1,0)), + note text +) + +create table tbl_record +( + ID int primary key not null, + cardId varchar(30) references tbl_card(ID), + ComputerId char(30) references tbl_computer(ID), + BeginTime datetime, + EndTime datetime, + fee money +) + +insert into tbl_card(ID,PassWard,Balance,userName) +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) + +select * from tbl_card +select * from tbl_record +select * from tbl_computer + +--1. 查询出用户名为'张军'的上网卡的上网记录,要求显示卡号,用户名,机器编号、开始时间、结束时间,和消费金额,并按消费金额降序排列 +select A.ID,A.userName,B.ComputerId,B.BeginTime,B.EndTime,B.fee from tbl_card A inner join tbl_record B on A.ID = B.cardId where userName='张军' + +--2. 查询出每台机器上的上网次数 和 消费的总金额 +select ComputerId,COUNT(*)上网次数,SUM(fee)消费总金额 from tbl_record GROUP BY ComputerId + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select ComputerId, SUM(fee)消费总金额 from tbl_record GROUP BY ComputerId + +--4. 查询出从未消费过的上网卡的卡号 和 用户名 +select B.ID,B.userName from tbl_card B left join tbl_record A on A.cardId = B.ID WHERE A.ID is null + +--5. 将密码与用户名一样的上网卡信息查询出来 +select * from tbl_card A JOIN tbl_card B ON (A.userName = B.PassWard) AND (A.userName = B.PassWard) + +--6. 查询出使用 次数最多的机器号 和 使用次数 +select top 1 COUNT(ID) 使用次数, ComputerId from tbl_record group by ComputerId ORDER BY COUNT(ID) desc + +--7. 查询出卡号是以'ABC'结尾的卡号,用户名,上网的机器号和消费金额 +SELECT A.ID,A.userName,B.ComputerId, B.fee FROM tbl_card A +JOIN tbl_record B ON A.ID = B.cardId +WHERE B.cardId like '%abc' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select A.ID,A.userName,B.ComputerId,B.BeginTime,B.EndTime,B.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 A.ID,A.userName,B.ComputerId,B.BeginTime,B.EndTime,B.fee from tbl_card A inner join tbl_record B ON A.ID = B.cardId +WHERE DATEDIFF(HH,BeginTime,EndTime)>12 + +--10. 查询出消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select A.ID,A.userName,B.ComputerId,B.BeginTime,B.EndTime,B.fee from tbl_card A inner join tbl_record B ON A.ID = B.cardId +where B.ID not in (select top 3 ID from tbl_record order by fee desc) +group by A.ID,A.userName,B.ComputerId,B.BeginTime,B.EndTime,B.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/\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..49691ace4b869aea438bf42470bd34c4765e8759 --- /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..2f22c10d720165da6c365a109cd1a4418094d16f --- /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\256\213\345\230\211\347\202\234/SQLQuery3.sql" "b/\347\254\254\345\215\201\344\272\214\346\254\241\350\257\276\344\275\234\344\270\232/\345\256\213\345\230\211\347\202\234/SQLQuery3.sql" new file mode 100644 index 0000000000000000000000000000000000000000..76e38226735d7d3b117b4ecc9d794658e092d3f0 --- /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\256\213\345\230\211\347\202\234/SQLQuery3.sql" @@ -0,0 +1,148 @@ +--首先创建网吧计费系统的数据库,表结构和数据如2.bmp所示,表的说明如下: +use master +go + +create database InternetBar +on +( + name = 'InternetBar', + filename = 'D:\InternetBar.mlf', + size = 5mb, + maxsize=50mb, + filegrowth = 10% +) +log on +( + name = 'InternetBar_log', + filename = 'D:\InternetBar_log.mlf', + size = 5mb, + maxsize=50mb, + filegrowth = 10% +) + +use InternetBar +go +--表一为上网卡信息表(tbl_card) +--id: 卡号,主键 +--passWord:密码 +--balance:卡上的余额 +--userName:该上网卡所属的用户名 +create table tbl_card +( + cardId varchar(8) primary key, + password varchar(20), + balance money, + userName nvarchar(10) +) +--表2为网吧机器说明表(tbl_computer) +--id:机器编号,主键 +--onUse:该机器是否正在使用,0为未使用,1为正在使用 +--note:该机器的说明 +create table tbl_computer +( + ComputerId varchar(8) primary key, + onUse int check(onuse=0 or onuse=1), + note text +) + +--表3为上网记录表(tbl_record): +--id:记录编号,主键 +--cardId:本次上网的卡号,外键 +--ComputerId:本次上网记录所使用的机器号,外键 +--beginTime:本次上网记录的开始时间 +--endTime:本次上网记录的结束时间 +--fee:本次上网的费用 +create table tbl_record +( + REId varchar(5) primary key, + cardId varchar(8) references tbl_card(cardId), + ComputerId varchar(8) references tbl_computer(computerId), + beginTime datetime, + endTime datetime, + fee money +) +go + +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 +('25','0078_RJV','B01','2007-07-15 19:00:00','2007-07-16 12:00:00',20), +('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 tbl_card.cardId,userName,ComputerId,beginTime,endtime,fee as 消费金额 from tbl_card +inner join tbl_record on tbl_card.cardId=tbl_record.cardId +where tbl_card.cardId='0023_ABC' order BY fee DESC + +--2. 查询出每台机器上的上网次数和消费的总金额 +SELECT ComputerId,COUNT(ComputerId)上网次数,sum(fee)消费的总金额 from tbl_record +group by ComputerId + +--3. 查询出所有已经使用过的上网卡的消费总金额 +select COM.ComputerId,SUM(fee) from tbl_computer COM left join tbl_record RE on COM.ComputerId=RE.ComputerId and onUse=1 +where onuse = 1 +GROUP BY COM.ComputerId + + +--4. 查询出从未消费过的上网卡的卡号和用户名 +SELECT CA.cardId,userName from tbl_card CA left join tbl_record RE on CA.cardId = RE.cardId +where fee is null + +--5. 将密码与用户名一样的上网卡信息查询出来 +select A.* from tbl_card A , tbl_card B where A.password = B.userName + +--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 tbl_card.cardId 卡号,userName 用户名,ComputerId 上网的机器号,fee 消费金额 from tbl_card left join tbl_record on tbl_card.cardId=tbl_record.cardId +where tbl_card.cardId like '%ABC%' + +--8. 查询出是周六、周天上网的记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tbl_card.cardId 卡号,userName 用户名,ComputerId 上网的机器号,beginTime ,endTime ,fee 消费金额 from tbl_card +left join tbl_record on tbl_card.cardId=tbl_record.cardId +where datepart(DW,beginTime)=7 or DATENAME(DW,beginTime)='星期日' + +--9. 查询成一次上网时间超过12小时的的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tbl_card.cardId 卡号,userName 用户名,ComputerId 上网的机器号,beginTime ,endTime ,fee 消费金额 from tbl_card +left join tbl_record on tbl_card.cardId=tbl_record.cardId +where DATEDIFF(HOUR,beginTime,endTime)>=12 + + +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select tbl_card.cardId 卡号,userName 用户名,ComputerId 上网的机器号,beginTime ,endTime ,fee 消费金额 from tbl_card +inner join tbl_record on tbl_card.cardId=tbl_record.cardId +where fee not in (select distinct top 3 fee from tbl_card +inner join tbl_record on tbl_card.cardId=tbl_record.cardId 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/\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..d43d2d8cf78bd80cb1879e7857b501171cbfc214 --- /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..cf3f27443e56b1be63ea5fac5b276fbcfc45a608 --- /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..ecce57d2e9438a70c3478d599f403f673ef98f00 --- /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..b0f6230d0300330097ee067fba8d910c769a11b6 --- /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..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/\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..2cc7e7a3c0a370f41df7747dfea0bc86731fe552 --- /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..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/\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..dbdcb938a31a1b0599e99ddaf14d266a83a76e11 --- /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..864623daa7ea27aa68be18dffa73a01577a1ade5 --- /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\231\272\351\276\231/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\231\272\351\276\231/SQLQuery1.sql" new file mode 100644 index 0000000000000000000000000000000000000000..7915b5679b121a07f74f40072b166e8a2f7aec79 --- /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\231\272\351\276\231/SQLQuery1.sql" @@ -0,0 +1,116 @@ +use master +go + +create database Card +on +( + name = 'Card', + filename = 'D:\SQL\Card.mdf', + size = 10MB, + maxsize = 50MB, + filegrowth = 10% +) +log on +( + name = 'Card_log', + filename = 'D:\SQL\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 \ 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\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..87b37ca27613ee43c531baa1d5a28200b6bd5f5a --- /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..f28e9c59632a58a605b622bdc29de1ef960d6df9 --- /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..5252b2a408455ad000f02969358ba6e50f3396cc --- /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..46443acc8c4daa53e478d72341d9a4a31277eb03 --- /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..f28e9c59632a58a605b622bdc29de1ef960d6df9 --- /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..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/\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..97915a20e32e6f5739840af1d19c81b8e851da0d --- /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..64fe115680bafed74b078dcbc10f25d1efcac4ba --- /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..864623daa7ea27aa68be18dffa73a01577a1ade5 --- /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..fc4e0328281f73e8e59f817cbde4e41745869fcb --- /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..3d239fa2664a03d52b611ae541f8c886ecc59f70 --- /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..90381a35b4a68ebeead40c30ef6bafcc7ec84664 --- /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..8fb8b8f00b506d2f4632fefc68d75a68ca15de92 --- /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