From d7921619d83c1e2f133c79993b00c3540606cac1 Mon Sep 17 00:00:00 2001 From: unknown <“921978039@qq.com”> Date: Wed, 7 Sep 2022 21:00:23 +0800 Subject: [PATCH] 0 --- .../9.7\344\275\234\344\270\232.md" | 84 ++++++++++++++ .../\347\254\224\350\256\260/9.7.md" | 107 ++++++++++++++++++ 2 files changed, 191 insertions(+) create mode 100644 "05 \351\273\204\347\202\234\346\235\260/\344\275\234\344\270\232/9.7\344\275\234\344\270\232.md" create mode 100644 "05 \351\273\204\347\202\234\346\235\260/\347\254\224\350\256\260/9.7.md" diff --git "a/05 \351\273\204\347\202\234\346\235\260/\344\275\234\344\270\232/9.7\344\275\234\344\270\232.md" "b/05 \351\273\204\347\202\234\346\235\260/\344\275\234\344\270\232/9.7\344\275\234\344\270\232.md" new file mode 100644 index 0000000..f6dd8ee --- /dev/null +++ "b/05 \351\273\204\347\202\234\346\235\260/\344\275\234\344\270\232/9.7\344\275\234\344\270\232.md" @@ -0,0 +1,84 @@ +``` +use banktest; +go +select * from AccountInfo --个人信息 +select * from BankCard --银行卡信息 +select * from CardStateChange --银行卡状态更改表 +select * from CardTransfer --转账表 +select * from CardExchange --交易记录 +``` + +--1. 关羽的银行卡号为"6225547858741263",查询出余额比关羽多的银行卡信息,显示卡号,身份证,姓名,余额。 + +``` +declare @qqq int +select @qqq=CardMoney from BankCard where CardNo = '6225547858741263' +select b.CardNo 卡号,b.CardMoney 余额,a.RealName 姓名,a.AccountCode 身份证 from BankCard as b +join AccountInfo a on a.AccountId=b.AccountId +where b.CardMoney>@qqq +``` + +--2. 从所有账户信息中查询出余额最高的交易明细(存钱取钱信息)。 + +``` +--select max(MoneyInBank)存钱,Max(MoneyOutBank)取款 from CardExchange +declare @inmoney money +declare @outmoney money +select @inmoney=max(MoneyInBank),@outmoney=max(MoneyOutBank) from CardExchange +if @inmoney>0 + begin + print '存入'+cast(@inmoney as char)+'元' + end + if @outmoney>0 + begin + print '取出'+@outmoney+'元' + end +else + print '没有交易记录' +``` + +--3. 查询有取款记录的银行卡及账户信息,显示卡号,身份证,姓名,余额。 + +``` +select C1.CardNo,B1.CardMoney,A1.AccountCode,A1.RealName from CardExchange as C1 join BankCard as B1 on C1.CardNo=B1.CardNo +join AccountInfo as A1 on B1.AccountId=A1.AccountId +where MoneyOutBank !=0 +``` + +--4. 查询出没有存款记录的银行卡及账户信息,显示卡号,身份证,姓名,余额。 + +``` +select b.CardNo'卡号',a.AccountCode'身份证',a.RealName'姓名',b.CardMoney'余额' from AccountInfo as a join BankCard as b on a.AccountId=b.AccountId +where a.AccountId in (select AccountId from BankCard where CardMoney =0.00) +``` + +--5. 关羽的银行卡号为"6225547858741263",查询当天是否有收到转账。 + +``` +declare @Transfermoney money +select @Transfermoney=TransferMoney from CardTransfer where CardNoIn=6225547858741263 +if @Transfermoney>0 + begin + print'收到转账' + end +else + print'没收到转账' +``` + +--6. 查询出交易次数(存款取款操作)最多的银行卡账户信息,显示:卡号,身份证,姓名,余额,交易数。 + +``` +select s2.CardMoney,s2.CardNo,s1.CardNo,count(s1.CardNo)'交易数' from CardExchange as s1 +left join BankCard as s2 on s1.CardNo = s2.CardNo +group by s1.CardNo +``` + +--7. 查询出没有转账交易记录的银行卡账户信息,显示卡号,身份证,姓名,余额。 + +``` +select s2.CardNo'卡号',s3.AccountCode'身份证',s3.RealName'姓名',s2.CardMoney'余额' from CardTransfer as s1 +right join BankCard as s2 on s1.CardNoOut=s2.CardNo +right join AccountInfo as s3 on s2.AccountId = s3.AccountId +where s1.CardNoOut is null +``` + diff --git "a/05 \351\273\204\347\202\234\346\235\260/\347\254\224\350\256\260/9.7.md" "b/05 \351\273\204\347\202\234\346\235\260/\347\254\224\350\256\260/9.7.md" new file mode 100644 index 0000000..86d9e9c --- /dev/null +++ "b/05 \351\273\204\347\202\234\346\235\260/\347\254\224\350\256\260/9.7.md" @@ -0,0 +1,107 @@ +# 连接 + +### 例子: + +``` +select sName,sAge, + case + when english <60 then '不及格' + when english IS null then '缺考' + else CONVERT(nvarchar, english) + end as'英语成绩' +from student as s +left join Score as c on s.sid =c.sid + +内连接 inner join...on... + 查询满足on后面条件的数据 + + +--练习:将StuScore成绩表中的学生成绩用五分制显示。 +-- 5分:80分以上 +-- 4分:60~79分 +-- 3分:40~59分 +-- 2分:20~39分 +-- 1分:0~19分 +select stuName,stuID,english, +case + when english>=80 then '5分' + when english>=60 then '4分' + when english>=40 then '3分' + when english>=20 then '2分' + when english>=0 then '1分' + else '0分' +end '5分制' +from StuScore +inner join StuInfo on StuScore.StuID = StuInfo.StuNo +``` + +## 批处理语句 + +go语句特点: + +1. 等待go语句前的代码执行完成后,再执行go后面的代码。 +2. 批处理语句的结束标志。 + +``` +--下面的@num变量作用域为全局 +--declare @num int +--set @num = 0 + +--下面的@num变量的作用域是局部,只在两个go之间可以使用,最后一行代码会报错 +--.........sql代码 +--go +--declare @num int +--set @num = 0 +--go +--set @num = 1 +``` + +### 比较运算符 + +SQLServer中有三个关键字可以修改比较[运算符](https://gitee.com/link?target=https%3A%2F%2Fso.csdn.net%2Fso%2Fsearch%3Fq%3D%E8%BF%90%E7%AE%97%E7%AC%A6%26spm%3D1001.2101.3001.7020):All、Any和Some,其中Some和Any等价 + +``` +set nocount on + +use tempdb +go + +if (object_id ('t1') is not null)drop table t1 +create table t1 (n int) +insert into t1 select 2 unionselect 3 + +if (object_id ('t2') is not null)drop table t2 +create table t2 (n int) +insert into t2 select 1 unionselect 2 union select 3 union select 4 + +-- t1表数据 2,3 +-- t2表数据 1,2,3,4 +-- '>all' 表示:t2表中列n的数据大于t1表中列n的数据的数,结果只有4. +select * from t2 where n > all(select n from t1 ) --4 +select * from t2 where n > any(select n from t1 ) --3,4 +select * from t2 where n > some(selectn from t1) --3,4 + +select * from t2 where n = all(select n from t1 ) --无数据 +select * from t2 where n = any(select n from t1 ) --2,3 +select * from t2 where n = some(selectn from t1) --2,3 + +select * from t2 where n < all(select n from t1 ) --1 +select * from t2 where n < any(select n from t1 ) --1,2 +select * from t2 where n < some(selectn from t1) --1,2 + +select * from t2 where n <>all (select n from t1 ) --1,4 +select * from t2 where n <>any (select n from t1 ) --1,2,3,4 +select * from t2 where n <>some(select n from t1) --1,2,3,4 +set nocount off +``` + +注意: + +1. =any 与in 等效. +2. 如果t1中包含null数据,那么所有All相关的比较运算将不会返回任何结果。因为t1和t2表的null的存在他们和notexists之类的比较符会有一些区别。 比如下面两句 + +``` +select * from t2 a where not exists(select1 from t1 where n>=a.n) + +select * from t2 where n > all(select n from t1) +``` \ No newline at end of file -- Gitee