From fd3bc781b94338d571a722f62ed79e62eb61c465 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=98=8A=E7=AB=A5?= <10033735+Harshreich@user.noreply.gitee.com> Date: Tue, 20 Sep 2022 09:56:37 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E9=99=88=E6=98=8A=E7=AB=A510?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 陈昊童 <10033735+Harshreich@user.noreply.gitee.com> --- .../\344\275\234\344\270\232/22.9.20.sql" | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 "\351\231\210\346\230\212\347\253\245/\344\275\234\344\270\232/22.9.20.sql" diff --git "a/\351\231\210\346\230\212\347\253\245/\344\275\234\344\270\232/22.9.20.sql" "b/\351\231\210\346\230\212\347\253\245/\344\275\234\344\270\232/22.9.20.sql" new file mode 100644 index 0000000..6afe772 --- /dev/null +++ "b/\351\231\210\346\230\212\347\253\245/\344\275\234\344\270\232/22.9.20.sql" @@ -0,0 +1,35 @@ +select * from AccountInfo; --Ϣ +select * from BankCard; --пϢ +select * from CardStateChange; --п״̬ı +select * from CardTransfer; --ת˱ +select * from CardExchange; --׼¼ +--1. пΪ"6225547858741263"ѯȹпϢʾţ֤ +select CardNo,CardMoney,AccountCode,RealName from BankCard b +join AccountInfo a on a.AccountId=b.AccountId +where CardMoney> (select CardMoney from BankCard where CardNo =6225547858741263); +--2. ˻ϢвѯߵĽϸ(ǮȡǮϢ) +select * from CardExchange where CardNo= +(select CardNo from bankcard where CardMoney=( select max(CardMoney) from BankCard)) +; + +--3. ѯȡ¼п˻Ϣʾţ֤ +select * from AccountInfo +where AccountId in (select AccountId from BankCard where CardNo in(select CardNo from CardExchange where MoneyOutBank !=0)) +; +--4. ѯûд¼п˻Ϣʾţ֤ +select * from AccountInfo +where AccountId in (select AccountId from BankCard where CardNo in(select CardNo from CardExchange where MoneyInBank =0)) +--5. пΪ"6225547858741263",ѯǷյתˡ +if exists(select * from CardTransfer where CardNoIn ='6225547858741263') +print '' +else +print 'û'; +--6. ѯ״ȡп˻Ϣʾţ֤״ +select top 1 b.CardNo,AccountCode,RealName,CardMoney,״ from BankCard b +inner join AccountInfo a on a.AccountId=b.AccountId +inner join (select CardNo,COUNT(MoneyInBank)+ COUNT(MoneyOutBank)״ from CardExchange group by CardNo )ce on ce.CardNo=b.CardNo +order by ״ +--7. ѯûת˽׼¼п˻Ϣʾţ֤ +select b.*,CardNo,AccountCode,RealName,CardMoney from BankCard b +inner join AccountInfo a on a.AccountId =b.AccountId +where CardNo not in (select CardNoIn from CardTransfer)and CardNo not in (select CardNoOut from CardTransfer) \ No newline at end of file -- Gitee From 4c26adb3de8a44b972c46391b83d2e3c243ec7ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=98=8A=E7=AB=A5?= <10033735+Harshreich@user.noreply.gitee.com> Date: Tue, 20 Sep 2022 09:56:52 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=E9=99=88=E6=98=8A=E7=AB=A510?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 陈昊童 <10033735+Harshreich@user.noreply.gitee.com> --- ...20\345\255\220\346\237\245\350\257\242.md" | 215 ++++++++++++++++++ 1 file changed, 215 insertions(+) create mode 100644 "\351\231\210\346\230\212\347\253\245/\347\254\224\350\256\260/22.9.20\345\255\220\346\237\245\350\257\242.md" diff --git "a/\351\231\210\346\230\212\347\253\245/\347\254\224\350\256\260/22.9.20\345\255\220\346\237\245\350\257\242.md" "b/\351\231\210\346\230\212\347\253\245/\347\254\224\350\256\260/22.9.20\345\255\220\346\237\245\350\257\242.md" new file mode 100644 index 0000000..795bafc --- /dev/null +++ "b/\351\231\210\346\230\212\347\253\245/\347\254\224\350\256\260/22.9.20\345\255\220\346\237\245\350\257\242.md" @@ -0,0 +1,215 @@ +## 回顾 + +1. 变量的使用。要先用DECLARE 关键字声明,然后用SET或SELECT赋值。局部变量前必须有 “@” 作前缀,全局变量必须有 “@@” 作前缀。 +2. 变量的输出可以用:PRINT或SELECT语句。 +3. 逻辑控制语句提供了条件操作所需的顺序和逻辑。 +4. 批处理可以提高语句执行的效率,使用“GO”作为结束标志。 + + + +## 嵌套/子查询 + +#### 什么是子查询? + +**问题:编写T-SQL语句,查询“黄炜杰”同学的分数大于80 分的考试成绩记录?** + +```sql +--实现方式1:连接查询 +--实现方式2: 子查询 +``` + + + +#### 子查询的特点和优势 + +1)使用灵活。 + +1. 可以成为SQL 语句的多个部分。 +2. **子查询作为查询条件使用**。 + +```sql +--练习:查询学号在曾鹏前面的同学信息 +``` + +1. **子查询作为临时表使用**。 from () + +```sql +--练习:查询所有学生的HTML成绩,如果学生没有成绩显示null +``` + +1. **子查询作为列使用**。 + +```sql +--练习:查询所有学生的HTML成绩,如果学生没有成绩显示null +``` + +2)降低SQL 的语句的复杂度,提高SQL 语句的可读性。 + +#### 子查询 + +![](https://gitee.com/snailclass/tuchuang/raw/master/img/image-20220906173549480-2022-9-1913:24:45.png) + +##### 按结果分类 + +###### 标量子查询 : + +**通常可以使用连接查询替代** + +标量子查询是指子查询返回的是**单一值**,如一个数字或一个字符串。 + +```sql +--1.查询书目表(tb_bibliography)中,条码号(barcode)为"TP311.13"图书(需要在图书表中先查找"TP31L13"的ISBN号)的图书名称(name)和作者(author) + + +--2.查询借阅表(tb-record)中姓名为"邓承明"(需要在学生表中先查找"邓承明'的学号)的学生的借阅信息,显示学号(stu-num)和条码号(barcode) +``` + + + +###### 列子查询 + +**列子查询是指子查询返回的结果集是N行1列**,该结果通常来自对表某个字段的查询结果,带in关键字的子查询是最常用的一类子查询,在使用in关键字进行查询时,子查询语句返回的结果应该是一个数据列中的多个值,如果仅返回1个数值,则可用标量子查询代替。 + +```sql +--in习题 +--查询学生表(tb-student)中和黄弘相同专业学生读者的学号(stu-num)姓名(name)和专业(major) +--先查询 黄宏的 专业 +select major from tb_student where name='黄弘' +--in代表:只要符合后面的条件就筛选出来 +select * from tb_student where major in (select major from tb_student where name='黄弘') + + +--查询学生表(tb-student)中还未还书的读者的学号(stu-num)和姓名(name) +--查询未还书学生的学号 +select stu_num from tb_record where return_time is null +--通过学号查询学生姓名 +select * from tb_student where stu_num in (select stu_num from tb_record where return_time is null) + + +--all any some 习题 +--1.查询学生表(tb-student)中,比信息学院出生日期最大的还要大的所有学生记录,即比信息学院年龄最小的还要小的所有记录,使用ALL关键字,显示学生的姓名(name)、出生日期(birth)和所属学院(school),同样的结果尝试用max()函数再实现一次。 +--信息学院学生最小年龄 +select birth from tb_student where school='信息学院' +select max(birth) from tb_student where school='信息学院' + +select * from tb_student where birth>(select max(birth) from tb_student where school='信息学院' ) + +--all关键字:>,<,= +select * from tb_student where birth>all(select birth from tb_student where school='信息学院') +--2.查询学生表(tb-student)中,比信息学院出生日期最小的还要大的所有学生记录,即比信息学院年龄最大的还要小的所有记录,使用ANY关键字,显示学生的姓名(name)、出生日期(birth)和所属学院(school)。尝试用SOME关键字代替ANY,重新执行后观察查询结果。 +select * from tb_student where birth>any(select birth from tb_student where school='信息学院') +select * from tb_student where birth>some(select birth from tb_student where school='信息学院') +--3.查询书目表(tb-bibliography)中,使用ANY关键字,每种类型中最贵的图 书名称(name)和价格(price) +-- =any 与 in 等价 +select max(price) 最贵的价格 from tb_bibliography group by category +select name 图书名称,price 价格 from tb_bibliography +where price in (select max(price) 最贵的价格 from tb_bibliography group by category) + +``` + + + +###### 行子查询 + +行子查询是指子查询返回的结果集是1行N列,该子查询的结果通常是对表的某行数据进行查询而返回的结果集。 + +```sql +--(1)查询书目表(tb_bibliography)中与《管理信息系统实用教程(第3版)》同作者(author),同类型(category)的书。 +--(2)查询学生表(tb_student)中与"邹睿睿'在同一个学院(school)且同一年级(学号头两位相同则表明在同一年级)学生的学号、姓名和学院。 +``` + + + +###### 表子查询(from子查询) + +表子查询是指子查询返回是M行N列的结果集,查询语句可嵌套在FROM关 +键字后,且需要定义别名。 + +```sql +--查询每种图书在馆的本数,显示ISBN号(ISBN)、图书名称(name)和该图书的在馆数量。 +``` + + + +##### 按位置分类 + +###### exists子查询 + +关键字EXISTS构造子查询时,当子查询的结果集不为空时,则EXISTS返回的结果为TRUE,外层查询语句进行查询;当子查询的结果集为空时,则EXISTS返回的结果为FALSE,外层查询语句不进行查询。 + +```sql +--(1)查询学生表(tb-student)中,是否有学生读者的姓名是"黄弘" +if exists(select stu_num from tb_record where stu_num in (select stu_num from tb_student where name='黄弘') and return_time is null) + print '有' +else + print '没有' + +--(2)查询书目表(tb-bibliography)中,库存数为0的书目名称(name) +``` + +## 排序函数 + +**排序函数 OVER( [分组子句] 排序子句[DESC][ASC] )** + +**排序子句 :ORDER BY 排序列,排序列…** + +练习:使用各个排名函数对学员的Java 成绩进行排名,并仔细体会其中排序函数的具体用法与其中的区别。 + +```sql + +``` + +- ROW_NUMBER()函数生成的排序根据排序子句给出**递增连续的序号** +- RANK()函数生成的排序根据排序子句给出**递增的序号,但是存在并列并且跳空** +- DENSE_RANK() 函数生成的排序根据排序子句给出**递增的序号,但是存在并列不跳空** + +根据上面三个函数,我们可以增加一个序列,但是有时候我们需要对数据进行分组,然后对分组后的数据进行增加序列,PARTITION BY可以与以上三个函数联合使用 + +```sql +--学生的Java成绩,并根据成绩排名 +--row_number() +select StuName 姓名,ROW_NUMBER() over(order by score desc) 排名,score 分数 from StuScore +join stuinfo on StuInfo.StuID = StuScore.StuID +where subject = 'Java' + +--rank() +select StuName 姓名,rank() over(order by score desc) 排名,score 分数 from StuScore +join stuinfo on StuInfo.StuID = StuScore.StuID +where subject = 'Java' + +--DENSE_RANK() + select StuName 姓名,dense_rank() over(order by score desc) 排名,score 分数 from StuScore +join stuinfo on StuInfo.StuID = StuScore.StuID +where subject = 'Java' + +``` + + + + + +## 分页查询 + +#### 分页方案一 + +**使用top分页** + +```sql +declare @PageSize int = 5 -- 每页5条 +declare @PageIndex int = 2 --第2页 +select top(@PageSize) * from Student +where Stuid not in (select top(@PageSize)*(@PageIndex-1)) StuId from Student +``` + + + +#### 分页方案二 + +**使用row_number分页** + +```sql +declare @PageSize int = 5 +declare @PageIndex int = 3 +select * from +(select ROW_NUMBER() over(order by StuId) RowId,*from Student) as Temp +where RowId between (@PageIndex-1)*@PageSize+1 and @PageIndex*PageSize \ No newline at end of file -- Gitee