From bd6e26376e8e05027237c2ba4a3807897465b162 Mon Sep 17 00:00:00 2001 From: yang-shirui <2403666581@qq.com> Date: Mon, 12 Apr 2021 19:48:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\345\215\242\345\256\201/SQLQuery1.sql" | 196 +-- .../SQLQuery15.sql" | 176 +-- .../SQLQuery16.sql" | 196 +-- .../\344\275\234\344\270\2321.sql" | 154 +- .../SQLQuery1.sql" | 270 ++-- .../3.30.sql" | 172 +-- .../work1.sql" | 184 +-- .../\344\275\234\344\270\23211.txt" | 146 +- .../SQLQuery2.sql" | 206 +-- .../3.30\344\275\234\344\270\232.sql" | 150 +- .../3.31\344\275\234\344\270\232.sql" | 340 ++--- .../SQLQuery1.sql" | 224 +-- ...2\344\270\200\344\273\243\347\240\201.txt" | 242 +-- .../3.30.sql" | 172 +-- .../SQLQuery2.sql" | 156 +- .../SQLQuery2.sql" | 204 +-- .../SQLQuery1.sql" | 200 +-- .../SQLQuery2.sql" | 248 ++-- .../SQLQuery1.sql" | 172 +-- ...1\344\270\200\344\275\234\344\270\232.sql" | 170 +-- .../\350\242\201\345\235\232/SQLQueryq1.sql" | 150 +- .../1.txt" | 144 +- .../\344\275\234\344\270\232\344\270\200.sql" | 180 +-- .../SQLQuery2.sql" | 204 +-- .../SQLQuery1.sql" | 148 +- .../SQLQuery1.sql" | 356 ++--- .../SQLQuery2.sql" | 422 +++--- .../SQLQuery1.sql" | 148 +- ...6\234\254\346\226\207\346\241\243 (2).txt" | 182 +-- .../SQLQuery\347\273\203\344\271\2401.sql" | 134 +- .../SQLQuery17.sql" | 116 +- .../SQLQuery18.sql" | 112 +- .../SQLQuery19.sql" | 98 +- .../\344\275\234\344\270\2321.sql" | 116 +- .../\344\275\234\344\270\2322.sql" | 112 +- .../\344\275\234\344\270\2323.sql" | 98 +- .../.keep" | 0 .../SQLQuery1.sql" | 67 - .../SQLQuery50.sql" | 278 ---- .../SQLQuery1.sql" | 138 +- .../review1.sql" | 110 +- .../SQLQuery1.sql" | 196 +-- ...1\346\254\241\344\275\234\344\270\232.sql" | 34 +- ...\346\254\241\344\275\234\344\270\2322.sql" | 183 ++- ...\346\254\241\344\275\234\344\270\2323.sql" | 180 ++- .../SQLQuery1.sql" | 166 +-- .../SQLQuery3.sql" | 128 +- .../SQLQuery4.sql" | 170 +-- ...\345\244\215\344\271\240\351\242\2301.sql" | 122 +- .../4.1a1.sql" | 106 +- .../SQLQuery1sql.sql" | 132 +- ...0\344\270\200\344\273\243\347\240\201.txt" | 198 +-- .../4.1a1.sql" | 106 +- ...\345\244\215\344\271\240\351\242\2301.sql" | 160 +- ...\345\244\215\344\271\240\351\242\2302.sql" | 140 +- ...\345\244\215\344\271\240\351\242\2303.sql" | 140 +- ...\347\273\203\344\271\240\351\242\2301.sql" | 140 +- .../SQLQuery2.sql" | 136 +- .../SQLQuery1.sql" | 138 +- .../SQLQuery4.sql" | 140 +- .../SQLQuery2.sql" | 92 +- .../\350\242\201\345\235\232/SQLQueryC1.sql" | 92 +- .../SQLQuery1.sql" | 112 +- .../\350\260\255\350\277\233/Sql13.sql" | 136 +- .../Sql13.sql" | 136 +- .../Sql13.sql" | 136 +- .../\344\275\234\344\270\232\344\270\200.sql" | 134 +- .../SQLQuery5.sql" | 206 +-- .../Sql13.sql" | 136 +- .../50.sql" | 1310 ++++++++--------- .../SQLQuery1.sql" | 144 +- .../SQLQuery2.sql" | 170 +-- .../SQLQuery3.sql" | 166 +-- .../SQLQuery2.sql" | 138 +- .../SQLQuery1(5).sql" | 138 +- .../SQLQuery2.sql" | 138 +- .../\344\275\234\344\270\2321.sql" | 268 ++-- .../\344\275\234\344\270\2322.sql" | 266 ++-- .../\345\215\242\345\256\201/SQLQuery2.sql" | 232 +-- .../SQLQuery16.sql" | 192 +-- .../SQLQuery4.sql" | 236 +-- .../work2.sql" | 188 +-- .../\344\275\234\344\270\23212.sql" | 234 +-- ...4\346\254\241\344\275\234\344\270\232.sql" | 248 ++-- .../SQLQuery2.sql" | 286 ++-- .../SQLQuery21.sql" | 266 ++-- ...2\344\272\214\344\273\243\347\240\201.txt" | 286 ++-- .../3.31zuoye2.sql" | 236 +-- .../SQLQusry2.sql" | 168 +-- .../SQLQuery5.sql" | 206 +-- .../SQLQuery2.sql" | 208 +-- ...4\346\254\241\344\275\234\344\270\232.sql" | 210 +-- .../\350\242\201\345\235\232/SQLQueryq2.sql" | 190 +-- .../SQLQuery1.sql" | 180 +-- ...4\346\254\241\344\275\234\344\270\232.sql" | 210 +-- .../SQLQuery1.sql" | 216 +-- .../1.txt" | 268 ++-- .../\344\275\234\344\270\232\344\272\214.sql" | 208 +-- .../SQLQuery5.sql" | 206 +-- .../SQLQuery10.sql" | 286 ++-- .../SQLQuery2.sql" | 420 +++--- .../SQLQuery1.sql" | 240 +-- .../SQLQuery2.sql" | 336 ++--- 103 files changed, 9827 insertions(+), 10053 deletions(-) delete mode 100644 "\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\345\255\235\346\266\265 /.keep" delete mode 100644 "\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\345\255\235\346\266\265 /SQLQuery1.sql" delete mode 100644 "\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\345\255\235\346\266\265 /SQLQuery50.sql" rename "\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\345\255\235\346\266\265 /SQLQuery2.sql" => "\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" (51%) rename "\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\345\255\235\346\266\265 /SQLQuery3.sql" => "\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" (47%) 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" index 202304a..54e3242 100644 --- "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" @@ -1,99 +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以上 的学生的学号 和考试平均分 以及选修课程的数量 +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" index d4f7b1c..8effde7 100644 --- "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" @@ -1,89 +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 --课程信息表 +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" index 8b48be5..77bb6cf 100644 --- "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" @@ -1,99 +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 +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" index 9f7898e..31b8c6d 100644 --- "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" @@ -1,78 +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 +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" index c74b406..8eb6244 100644 --- "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" @@ -1,136 +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 +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/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" index 4c83076..a08aa6d 100644 --- "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" @@ -1,86 +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 +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\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" index 8fdcea0..bc933bc 100644 --- "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" @@ -1,92 +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 +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\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" index c4e035b..88de680 100644 --- "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" @@ -1,74 +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 + +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\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" index 61ee4b6..fd36268 100644 --- "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" @@ -1,104 +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 +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/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" index 5baed16..aac0742 100644 --- "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" @@ -1,75 +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 +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" index 3f66cde..a2d371c 100644 --- "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" @@ -1,170 +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 - +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\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" index 8a8ddc0..3fdcf78 100644 --- "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" @@ -1,112 +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 +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\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" index bad5956..14ce2cc 100644 --- "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" @@ -1,121 +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 +--浣滀笟涓 + +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" index 4c83076..a08aa6d 100644 --- "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" @@ -1,86 +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 +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\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" index 0a39b3b..06cfa1e 100644 --- "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" @@ -1,79 +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 +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" index 440f9d4..44605de 100644 --- "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" @@ -1,103 +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 +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/\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" index 8138dd1..360b72f 100644 --- "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" @@ -1,101 +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以上的学生的学号和考试平均分以及选修课程的数量 +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" index 5d30e49..e6a0f8b 100644 --- "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" @@ -1,125 +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 +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\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" index 0f213b5..1fd8c3f 100644 --- "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" @@ -1,86 +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 +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" index 7472e6b..4466051 100644 --- "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" @@ -1,85 +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 +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/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" index 52c5411..734c047 100644 --- "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" @@ -1,75 +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 - +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\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" index 2b307b2..eca5106 100644 --- "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" @@ -1,73 +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 +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" index e17b165..6b52a65 100644 --- "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" @@ -1,91 +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 +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\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" index 1878feb..648e0a8 100644 --- "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" @@ -1,103 +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 +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" index ea2f252..5aa21c3 100644 --- "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" @@ -1,74 +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 + +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/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" index 887f88f..2242e2d 100644 --- "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" @@ -1,178 +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 - +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" index e592302..61e3b49 100644 --- "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" @@ -1,211 +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 +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/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" index ea2f252..5aa21c3 100644 --- "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" @@ -1,74 +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 + +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/\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" index 0544818..9acc02c 100644 --- "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" @@ -1,91 +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 +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\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" index 37a7683..b0523bc 100644 --- "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" @@ -1,68 +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之间 +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/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" index a63ba25..e6faa4d 100644 --- "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" @@ -1,59 +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 - +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" index fd531b6..b1e6ae5 100644 --- "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" @@ -1,57 +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 +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" index 8b95560..52bafd0 100644 --- "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" @@ -1,50 +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、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +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" index 78924d1..e80b49c 100644 --- "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" @@ -1,59 +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 - +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" index fd531b6..b1e6ae5 100644 --- "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" @@ -1,57 +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 +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" index 8b95560..52bafd0 100644 --- "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" @@ -1,50 +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、查询明星类型为“体育明星”中年龄最大的姓名、特技、籍贯信息,要求显示列别名。 +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\220\264\345\255\235\346\266\265 /.keep" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\345\255\235\346\266\265 /.keep" deleted file mode 100644 index e69de29..0000000 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\220\264\345\255\235\346\266\265 /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\220\264\345\255\235\346\266\265 /SQLQuery1.sql" deleted file mode 100644 index d1a00c2..0000000 --- "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\345\255\235\346\266\265 /SQLQuery1.sql" +++ /dev/null @@ -1,67 +0,0 @@ -create database GoodsDB -on -( - name=GoodsDB, - filename='E:\SQL\42\GoodsDB.mdf', - size=5MB, - maxsize=10MB, - filegrowth=10% -) -log on -( - name=GoodsDB_log, - filename='E:\SQL\42\GoodsDB_log.ldf', - size=5MB, - maxsize=10MB, - filegrowth=10% -) -drop table GoodsType - -use GoodsDB -go - -truncate table GoodsType - -CREATE TABLE GoodsType -( - TypeID int not null primary key identity(1,1), - TypeName nvarchar(20), -) -create table GoodsInfo -( - GoodsID int, - GoodsName nvarchar(20), - GoodsColor nvarchar(20), - GoodsBrand nvarchar(20), - GoodsMoney money, - TypeID int foreign key references GoodsType(TypeID) -) - -insert into GoodsType -select 1,'服装内衣' union -select 2,'鞋包配饰' union -select 3,'手机数码' - -insert into GoodsInfo -select 1,'提花小西装','红色','菲曼琪',300,1 union -select 2,'百搭短裤','绿色','哥弟',100,1 union -select 3,'无袖背心','白色','阿依莲',700,1 union -select 4,'低帮休闲鞋','红色','菲曼琪',900,1 union -select 5,'中跟单鞋','绿色','哥弟',400,2 union -select 6,'平底鞋','白色','阿依莲',200,2 union -select 7,'迷你照相机','红色','尼康',500,1 union -select 8,'硬盘','黑色','希捷',600,3 union -select 9,'显卡','黑色','技嘉',800,3 - - ---3、查询价格最贵的商品名称,商品颜色和商品价格,要求使用别名显示列名 - -select top 1 GoodsID 商品编号,GoodsName 商品名称,GoodsColor 商品颜色, GoodsBrand 商品品牌,GoodsMoney 商品价格,TypeID 商品类型编号 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/\345\220\264\345\255\235\346\266\265 /SQLQuery50.sql" "b/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\345\255\235\346\266\265 /SQLQuery50.sql" deleted file mode 100644 index 8c75a28..0000000 --- "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\345\255\235\346\266\265 /SQLQuery50.sql" +++ /dev/null @@ -1,278 +0,0 @@ - --- 1.查询"数学 "课程比" 语文 "课程成绩高的学生的信息及课程分数 - -select StudentCode 学号,C.StudentName,Birthday,Sex , A.CourseId 课程ID,A.Score,B.CourseId 课程ID,B.Score from StudentCourseScore A -inner join StudentCourseScore B on A.StudentId=B.StudentId -inner join StudentInfo C on C.StudentCode = A.StudentId -where A.Score>B.Score and A.CourseId=2 and B.CourseId=1 - --- 1.1 查询同时存在" 数学 "课程和" 语文 "课程的情况 - -select * from StudentCourseScore A -inner join CourseInfo B on CourseId=B.Id -where CourseId=1 or CourseId=2 - --- 1.2 查询存在" 数学 "课程但可能不存在" 语文 "课程的情况(不存在时显示为 null ) - -select * from CourseInfo A -left join StudentCourseScore B on CourseId=A.Id - --- 1.3 查询不存在" 数学 "课程但存在" 语文 "课程的情况 - -select * from CourseInfo A -inner join StudentCourseScore B on CourseId=A.Id -where CourseId!=2 or CourseId=1 - --- 2.查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩 - -select StudentCode,StudentName,AVG(Score) from StudentCourseScore A -inner join StudentInfo B on A.StudentId=B.StudentCode -group by StudentCode,StudentName having AVG(Score)>60 - --- 3.查询在 成绩 表存在成绩的学生信息 - -select distinct A.* from StudentInfo A -left join StudentCourseScore B on A.StudentCode=B.StudentId -where StudentId is not null - --- 4.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null ) - -select B.StudentCode 学生编号,StudentName,COUNT(StudentId),sum(Score) from StudentCourseScore A -right join StudentInfo B on A.StudentId=B.Id -group by B.StudentCode,StudentName - --- 4.1 查有成绩的学生信息 - -select distinct A.* from StudentInfo A -left join StudentCourseScore B on A.StudentCode=B.StudentId -where StudentId is not null - - --- 5.查询「李」姓老师的数量 - -select * from Teachers A where TeacherName like '李%' - --- 6.查询学过「张三」老师授课的同学的信息 - -select D.* from StudentCourseScore A -inner join CourseInfo B on A.CourseId=B.Id -inner join Teachers C on B.TeacherId=C.Id -inner join StudentInfo D on StudentCode=A.StudentId -where TeacherName='张三' - --- 7.查询没有学全所有课程的同学的信息 - -select A.* from StudentInfo A -left join StudentCourseScore B on A.StudentCode=B.StudentId -group by StudentCode,A.Id,StudentName,Birthday,Sex,ClassId having COUNT(CourseId)<3 - --- 8.查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息 - -select distinct(StudentCode),StudentName,Birthday,Sex,ClassId from StudentCourseScore A -inner join StudentInfo B on StudentCode=StudentId -where CourseId in (select CourseId from StudentCourseScore where StudentId =1) AND StudentId!=1 - --- 9.查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息 - -select A.* from StudentInfo A -left join StudentCourseScore B on StudentCode=StudentId -group by StudentCode,A.Id,StudentName,Birthday,Sex,ClassId having count(CourseId)=3 and StudentCode!=01 - --- 10.查询没学过"张三"老师讲授的任一门课程的学生姓名 - -select distinct(StudentName) from StudentCourseScore A -right join CourseInfo B on A.CourseId=B.Id -right join Teachers C on B.TeacherId=C.Id -right join StudentInfo D on StudentCode=A.StudentId -where TeacherName!='张三' or TeacherName is null - --- 11.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 - -select StudentCode 学号,StudentName 姓名,AVG(Score) 平均成绩 from StudentCourseScore A -inner join StudentInfo B on StudentCode=StudentId -group by StudentName,StudentCode having COUNT(StudentId)>=2 and AVG(Score)<60 - - --- 12.检索" 数学 "课程分数小于 60,按分数降序排列的学生信息 - -select D.* from StudentCourseScore A -right join CourseInfo B on A.CourseId=B.Id -inner join StudentInfo D on StudentCode=A.StudentId -where CourseName='数学' and score<=60 -order by Score Desc - --- 13.按平均成绩从高到低显示所有学生 的 所有课程的成绩 以及 平均成绩 - -select StudentId,SUM(Score)所有课程的成绩,AVG(Score) 平均成绩 from StudentCourseScore A -group by A.StudentId order by AVG(A.Score) DESC - - --- 14.查询各科成绩最高分、最低分和平均分: - -select CourseId,max(Score)最高分,MIN(Score)最低分,AVG(Score)平均分 from StudentCourseScore A -group by CourseId - - --- 15.以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 --- 15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次 --- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 --- 16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺 --- 17.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比 --- 18.查询各科成绩前三名的记录 - - --- 19.查询每门课程被选修的学生数 - -select CourseId,count(CourseId) from StudentCourseScore A group by CourseId - --- 20.查询出只选修两门课程的学生学号和姓名 - - -select StudentCode,StudentName from StudentCourseScore A -inner join StudentInfo B on A.StudentId=B.StudentCode -group by StudentCode,StudentName having count(StudentId)=2 - --- 21.查询男生、女生人数 - -select Sex,COUNT(Sex) from StudentInfo A group by Sex - --- 22.查询名字中含有「风」字的学生信息 - -select * from StudentInfo A where StudentName like '%风%' - --- 23.查询同名同性学生名单,并统计同名人数 - -select StudentName,COUNT(StudentName) from StudentInfo A -group by StudentName,Sex having COUNT(StudentName)>1 - --- 24.查询 1990 年出生的学生名单 - -select * from StudentInfo A where Birthday like '1990%' - --- 25.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 - -select CourseId,AVG(Score) from StudentCourseScore A group by CourseId order by AVG(Score) DESC - --- 26.查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩 - - -select StudentCode,StudentName,AVG(Score) from StudentCourseScore A -inner join StudentInfo B on A.StudentId=B.StudentCode -group by StudentCode,StudentName having AVG(Score)>85 - - --- 27.查询课程名称为「数学」,且分数低于 60 的学生姓名和分数 - - -select C.StudentName,Score from StudentCourseScore A -inner join CourseInfo B on A.CourseId=B.Id -inner join StudentInfo C on A.StudentId=C.StudentCode -where CourseName='数学' and Score<60 - - --- 28.查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况) - -select StudentCode,StudentName,CourseName,Score from StudentInfo A -left join StudentCourseScore B on B.StudentId=A.StudentCode -left join CourseInfo C on B.CourseId=C.Id - --- 29.查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数 - -select C.StudentName,CourseName, Score from StudentCourseScore A -inner join CourseInfo B on A.CourseId=B.Id -inner join StudentInfo C on A.StudentId=C.StudentCode -where Score>70 - --- 30.查询不及格的课程 - -select CourseName from StudentCourseScore A -inner join CourseInfo B on A.CourseId=B.Id -group by CourseName having AVG(Score)<60 - - --- 31.查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名 - -select StudentCode,StudentName from StudentCourseScore A -inner join StudentInfo C on A.StudentId=C.StudentCode -where CourseId=1 and Score>80 - --- 32.求每门课程的学生人数 - -select CourseName,COUNT(CourseName) from StudentCourseScore A -inner join CourseInfo B on A.CourseId=B.Id -group by CourseName - --- 33.成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 - -select top 1 StudentCode 学号,StudentName,Birthday,Sex,ClassId ,Score from StudentCourseScore A -right join CourseInfo B on A.CourseId=B.Id -right join Teachers C on B.TeacherId=C.Id -inner join StudentInfo D on StudentCode=A.StudentId -where TeacherName='张三' -order by Score DESC - ---34.成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩 - -select StudentCode 学号,StudentName,Birthday,Sex,ClassId ,Score, rank()over(order by score DESC)as pm from StudentCourseScore A -right join CourseInfo B on A.CourseId=B.Id -right join Teachers C on B.TeacherId=C.Id -inner join StudentInfo D on StudentCode=A.StudentId -where TeacherName='张三' - --- 35.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 - - -select distinct A.StudentId,A.CourseId,A.Score from StudentCourseScore A -inner join StudentCourseScore B on A.StudentId=B.StudentId -where B.Score=A.Score and B.CourseId !=A.CourseId - - --- 36.查询每门功成绩最好的前两名 - -select top 2 * from StudentCourseScore A where CourseId=1 order by CourseId ,Score DESC - --- 37.统计每门课程的学生选修人数(超过 5 人的课程才统计)。 - -select CourseName,COUNT(CourseName) from StudentCourseScore A -inner join CourseInfo B on A.CourseId=B.Id -group by CourseName having COUNT(CourseName)>5 - --- 38.检索至少选修两门课程的学生学号 - -select StudentId from StudentCourseScore A -inner join CourseInfo B on A.CourseId=B.Id -group by StudentId having COUNT(StudentId)>2 - --- 39.查询选修了全部课程的学生信息 - -select StudentCode 学号,StudentName,Birthday,Sex,ClassId from StudentCourseScore A -inner join CourseInfo B on A.CourseId=B.Id -inner join StudentInfo C on A.StudentId=C.StudentCode -group by StudentCode,StudentName,Birthday,Sex,ClassId having COUNT(StudentId)=3 - - --- 40.查询各学生的年龄,只按年份来算 - -select * from StudentCourseScore A -select * from CourseInfo A -select * from StudentInfo A -select * from Teachers A - -select * from StudentInfo A where DATEADD(YYYY,DATETIME(),) - - --- 41.按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一 - - --- 42.查询本周过生日的学生 - - --- 43.查询下周过生日的学生 - - - --- 44.查询本月过生日的学生 - - - --- 45.查询下月过生日的学生 \ 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/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" index 9097b1d..42a3c5e 100644 --- "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" @@ -1,69 +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 - +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\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" index c6ea7b4..d3255fc 100644 --- "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" @@ -1,55 +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 +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\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" index 672354e..e310c57 100644 --- "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" @@ -1,99 +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之间 +--复习一 +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" index 9178b14..797c731 100644 --- "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" @@ -63,8 +63,38 @@ select top 1 GoodsName 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 +group by A.TypeID,TypeName --5、查询商品信息所有列,要求商品颜色为红色,价格在300~600之间 -select * from GoodsInfo where GoodsColor='红色' and GoodsMoney>=300 and GoodsMoney<=600 \ No newline at end of file +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/\345\220\264\345\255\235\346\266\265 /SQLQuery2.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" similarity index 51% rename from "\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\345\255\235\346\266\265 /SQLQuery2.sql" rename to "\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" index de7f898..a2e5614 100644 --- "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\345\255\235\346\266\265 /SQLQuery2.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" @@ -1,72 +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 '小房子',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_price 租金,house_name 房屋名称 from HOUSE order by house_price Desc \ No newline at end of file +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/\345\220\264\345\255\235\346\266\265 /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\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" similarity index 47% rename from "\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\345\255\235\346\266\265 /SQLQuery3.sql" rename to "\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" index fc3c063..8fd3463 100644 --- "a/\347\254\254\345\215\201\344\270\211\346\254\241\350\257\276\344\275\234\344\270\232/\345\220\264\345\255\235\346\266\265 /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\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" @@ -1,65 +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 籍贯信息 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 StarInfo where S_T_NO=1 order by S_AGE DESC \ No newline at end of file +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/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" index d78c0e8..bf65451 100644 --- "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" @@ -1,84 +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 = '体育明星' +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" index 3d0c5c1..de618ae 100644 --- "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" @@ -1,65 +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之间 - +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" index 51d3034..21d6945 100644 --- "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" @@ -1,85 +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) +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/\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" index a1276cb..f9a046f 100644 --- "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" @@ -1,61 +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 - +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" index b0057fa..a2f9cc0 100644 --- "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" @@ -1,54 +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之间 +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/\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" index 982e7eb..7d6a83c 100644 --- "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" @@ -1,66 +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 +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\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" index b12613a..a34494c 100644 --- "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" @@ -1,100 +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之间 +--复习一 +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/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" index b0057fa..a2f9cc0 100644 --- "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" @@ -1,54 +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之间 +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\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" index 2ca681c..b58c668 100644 --- "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" @@ -1,80 +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 = '红色' - +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" index d5dea63..bbc5721 100644 --- "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" @@ -1,71 +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 +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" index 401955a..3c3ef98 100644 --- "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" @@ -1,70 +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 - +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" index e841897..0b76105 100644 --- "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" @@ -1,71 +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 +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/\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" index c893c44..831c090 100644 --- "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" @@ -1,69 +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之间 +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\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" index 9097b1d..42a3c5e 100644 --- "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" @@ -1,69 +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 - +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\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" index 22bfcd0..5516440 100644 --- "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" @@ -1,71 +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之间 +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" index f38e3ec..ed6318f 100644 --- "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" @@ -1,47 +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之间 +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" index 3880de1..b9174c8 100644 --- "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" @@ -1,46 +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 +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/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" index 1656b8e..78944ba 100644 --- "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" @@ -1,56 +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 +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" index 14b793a..24737c3 100644 --- "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" @@ -1,69 +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之间 +--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" index 14b793a..24737c3 100644 --- "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" @@ -1,69 +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之间 +--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" index 14b793a..24737c3 100644 --- "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" @@ -1,69 +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之间 +--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" index 9fec250..724dfa2 100644 --- "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" @@ -1,67 +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 - +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\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" index 679639f..864623d 100644 --- "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" @@ -1,103 +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 - +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" index 14b793a..24737c3 100644 --- "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" @@ -1,69 +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之间 +--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/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" index ddffbdf..983d38c 100644 --- "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" @@ -1,207 +1,207 @@ 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 - + +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 +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 @@ -213,99 +213,99 @@ where 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 + 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 - + 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 + 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 - - +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 - - +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 - - +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.* @@ -314,41 +314,41 @@ from join StudentCourseScore sc on - si.Id = sc.StudentId + si.Id = sc.StudentId + - -- 5.查询「李」姓老师的数量 -select - count(*) '数量' -from - Teachers -where - TeacherName like '李%' - +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 + 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 +select * from StudentCourseScore -- 7.查询没有学全所有课程的同学的信息 select * @@ -356,63 +356,63 @@ 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 +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 + ) 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 + 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) '平均成绩' @@ -436,61 +436,61 @@ where ) 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 - +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 - +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 @@ -583,7 +583,7 @@ 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 '排名' @@ -597,201 +597,201 @@ group by sc1.CourseId, sc1.StudentId, sc1.Score order by sc1.CourseId, Score desc - --- 16.查询学生的总成绩,并进行排名,总分重复时保留名次空缺 + +-- 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 - + (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 - + (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 +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 - +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 - +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 - +select + case when sex='f' then '女' else '男' end '性别', count(*) '人数' +from + StudentInfo +group by + Sex + -- 22.查询名字中含有「风」字的学生信息 -select - * -from - StudentInfo +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 - - +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' - +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 +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 - +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 - - +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 - +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 @@ -807,132 +807,132 @@ 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 - +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 - +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 + id in (select + StudentId + from + StudentCourseScore + where CourseId = 1 and Score > 80 ) - + -- 32.求每门课程的学生人数 -select - CourseId, count(distinct StudentId) '人数' -from - StudentCourseScore -group by - CourseId - +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 - +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 = '张三' - ) - +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 - +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 + 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 @@ -943,91 +943,91 @@ 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 - - +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 - +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 + 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 +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 - +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) - +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) +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 +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" index b31e14f..f1e1c54 100644 --- "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" @@ -1,73 +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 +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" index 8059c73..7b5bf5d 100644 --- "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" @@ -1,86 +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 +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" index d78c0e8..bf65451 100644 --- "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" @@ -1,84 +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 = '体育明星' +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/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" index 0d4d404..c35d456 100644 --- "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" @@ -1,69 +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' +--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/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" index 32cf467..b2da2ac 100644 --- "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" @@ -1,70 +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之间 +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/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" index 0d4d404..c35d456 100644 --- "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" @@ -1,69 +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' +--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\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" index 1a7fbdf..e610192 100644 --- "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" @@ -1,135 +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 - +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" index 5553bbc..cfff09b 100644 --- "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" @@ -1,134 +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 - +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/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" index 1a52dd6..bbb23b8 100644 --- "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" @@ -1,117 +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) +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/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" index 1d3d03e..49691ac 100644 --- "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" @@ -1,97 +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 +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/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" index b779ec5..2f22c10 100644 --- "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" @@ -1,118 +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 - - +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\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" index e328f27..d43d2d8 100644 --- "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" @@ -1,94 +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 - - - +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\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" index 9cd0591..cf3f274 100644 --- "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" @@ -1,118 +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) +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/\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" index fc7145d..ecce57d 100644 --- "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" @@ -1,124 +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 +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/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" index f9ca5ca..b0f6230 100644 --- "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" @@ -1,143 +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 - +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\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" index e5cc1e5..d0376e1 100644 --- "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" @@ -1,134 +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 - +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\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" index b585266..2cc7e7a 100644 --- "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" @@ -1,144 +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) +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" index 0827f8c..1259560 100644 --- "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" @@ -1,118 +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 - - - +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\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" index 8b5ce95..dbdcb93 100644 --- "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" @@ -1,85 +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 +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" index 679639f..864623d 100644 --- "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" @@ -1,103 +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 - +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/\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" index 95d3421..87b37ca 100644 --- "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" @@ -1,105 +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 消费金额 +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" index 0430e66..f28e9c5 100644 --- "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" @@ -1,106 +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 +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" index 7e58053..5252b2a 100644 --- "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" @@ -1,96 +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 +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/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" index 70730ee..46443ac 100644 --- "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" @@ -1,90 +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) - - - +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" index 0430e66..f28e9c5 100644 --- "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" @@ -1,106 +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 +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" index a06ab01..6e51500 100644 --- "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" @@ -1,109 +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) +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" index 0b1355a..97915a2 100644 --- "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" @@ -1,135 +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 +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/\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" index eecbb0c..64fe115 100644 --- "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" @@ -1,104 +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 - - +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\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" index 679639f..864623d 100644 --- "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" @@ -1,103 +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 - +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" index 030df65..fc4e032 100644 --- "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" @@ -1,143 +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 +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/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" index 9b1a9f2..3d239fa 100644 --- "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" @@ -1,211 +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 +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" index 61940ce..90381a3 100644 --- "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" @@ -1,120 +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 - +--棣栧厛鍒涘缓缃戝惂璁¤垂绯荤粺鐨勬暟鎹簱锛岃〃缁撴瀯鍜屾暟鎹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/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" index 6c89833..8fb8b8f 100644 --- "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" @@ -1,171 +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 + +--首先创建网吧计费系统的数据库,表结构和数据如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 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +where datediff(HH,BeginTime,Endtime)>12 +--10. 查询除消费金额排列前三名(最高)的上网记录,要求显示上网的卡号,用户名,机器号,开始时间、结束时间和消费金额 +select top 3 * from tbl_record order by fee desc + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- Gitee