From 42856d6df36a6e6e4aba752de05e90a44ae3c0c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E5=90=AF=E6=81=92?= <3013545725@qq.com> Date: Tue, 20 Sep 2022 14:17:46 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E7=AC=AC=E4=B8=89?= =?UTF-8?q?=E6=AC=A1=E7=BD=97=E5=90=AF=E6=81=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "\347\275\227\345\220\257\346\201\222/\347\254\254\344\270\211\346\254\241\347\275\227\345\220\257\346\201\222/.keep" diff --git "a/\347\275\227\345\220\257\346\201\222/\347\254\254\344\270\211\346\254\241\347\275\227\345\220\257\346\201\222/.keep" "b/\347\275\227\345\220\257\346\201\222/\347\254\254\344\270\211\346\254\241\347\275\227\345\220\257\346\201\222/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From b6e93d99831e39574aada020faf750a157842068 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E5=90=AF=E6=81=92?= <3013545725@qq.com> Date: Tue, 20 Sep 2022 14:18:26 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=E7=BD=97=E5=90=AF=E6=81=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 罗启恒 <3013545725@qq.com> --- .../2022-9-20 \344\275\234\344\270\232.sql" | 45 ++++ .../2022-9-19 \347\254\224\350\256\260.sql" | 218 ++++++++++++++++++ 2 files changed, 263 insertions(+) create mode 100644 "\347\275\227\345\220\257\346\201\222/\347\254\254\344\270\211\346\254\241\347\275\227\345\220\257\346\201\222/\347\275\227\345\220\257\346\201\222/\344\275\234\344\270\232/2022-9-20 \344\275\234\344\270\232.sql" create mode 100644 "\347\275\227\345\220\257\346\201\222/\347\254\254\344\270\211\346\254\241\347\275\227\345\220\257\346\201\222/\347\275\227\345\220\257\346\201\222/\347\254\224\350\256\260/2022-9-19 \347\254\224\350\256\260.sql" diff --git "a/\347\275\227\345\220\257\346\201\222/\347\254\254\344\270\211\346\254\241\347\275\227\345\220\257\346\201\222/\347\275\227\345\220\257\346\201\222/\344\275\234\344\270\232/2022-9-20 \344\275\234\344\270\232.sql" "b/\347\275\227\345\220\257\346\201\222/\347\254\254\344\270\211\346\254\241\347\275\227\345\220\257\346\201\222/\347\275\227\345\220\257\346\201\222/\344\275\234\344\270\232/2022-9-20 \344\275\234\344\270\232.sql" new file mode 100644 index 0000000..b91e390 --- /dev/null +++ "b/\347\275\227\345\220\257\346\201\222/\347\254\254\344\270\211\346\254\241\347\275\227\345\220\257\346\201\222/\347\275\227\345\220\257\346\201\222/\344\275\234\344\270\232/2022-9-20 \344\275\234\344\270\232.sql" @@ -0,0 +1,45 @@ +select * from AccountInfo +select * from BankCard +select * from CardExchange +select * from CardStateChange +select * from CardTransfer +--пΪ"6225547858741263"ѯȹпϢʾţ֤ +select AccountCode ֤,CardNo ,RealName ,CardMoney from AccountInfo +join BankCard on BankCard.AccountId = AccountInfo.AccountId +where RealName = '' + +--˻ϢвѯߵĽϸ(ǮȡǮϢ) +select * from BankCard +join CardExchange on BankCard.CardNo=CardExchange.CardNo +where CardMoney in (select MAX(CardMoney) from BankCard); +--ѯȡ¼п˻Ϣʾţ֤ +select BankCard.CardNo,AccountInfo.AccountCode,AccountInfo.RealName,BankCard.CardMoney from AccountInfo +join BankCard on AccountInfo.AccountId = BankCard.AccountId +where AccountInfo.AccountId= +(select BankCard.AccountId from BankCard +join CardExchange on CardExchange.CardNo=BankCard.CardNo +where BankCard.CardMoney-CardExchange.MoneyInBank>0 +); +--ѯûд¼п˻Ϣʾţ֤ +select BankCard.CardNo,AccountInfo.AccountCode,AccountInfo.RealName,BankCard.CardMoney from CardExchange +join BankCard on CardExchange.CardNo = BankCard.CardNo +join AccountInfo on AccountInfo.AccountId=BankCard.AccountId +where MoneyInBank is null +--пΪ"6225547858741263",ѯǷյתˡ +if exists (select * from CardTransfer where CardNoIn='6225547858741263') +print'ת' +else +print'ת' +--ѯ״ȡп˻Ϣʾţ֤״ +select BankCard.CardNo,AccountInfo.AccountCode,AccountInfo.RealName,BankCard.CardMoney,a.״ from AccountInfo +join BankCard on AccountInfo.AccountId= BankCard.AccountId +join (select CardNo,COUNT(CardNo)״ from CardExchange group by CardNo ) a on a.CardNo=BankCard.CardNo +order by a.״ +desc; +--ѯûת˽׼¼п˻Ϣʾţ֤ +select a.*,AccountInfo.AccountCode,AccountInfo.RealName from AccountInfo , +( +select * from BankCard where CardNo not in(select CardNoOut from CardTransfer ) +and CardNo not in(select CardNoIn from CardTransfer ) +) a +where AccountInfo.AccountId=a.AccountId \ No newline at end of file diff --git "a/\347\275\227\345\220\257\346\201\222/\347\254\254\344\270\211\346\254\241\347\275\227\345\220\257\346\201\222/\347\275\227\345\220\257\346\201\222/\347\254\224\350\256\260/2022-9-19 \347\254\224\350\256\260.sql" "b/\347\275\227\345\220\257\346\201\222/\347\254\254\344\270\211\346\254\241\347\275\227\345\220\257\346\201\222/\347\275\227\345\220\257\346\201\222/\347\254\224\350\256\260/2022-9-19 \347\254\224\350\256\260.sql" new file mode 100644 index 0000000..985e5a2 --- /dev/null +++ "b/\347\275\227\345\220\257\346\201\222/\347\254\254\344\270\211\346\254\241\347\275\227\345\220\257\346\201\222/\347\275\227\345\220\257\346\201\222/\347\254\224\350\256\260/2022-9-19 \347\254\224\350\256\260.sql" @@ -0,0 +1,218 @@ +## Ƕ/Ӳѯ + +#### ʲôӲѯ + +**:дT-SQL䣬ѯ쿽ܡͬѧķ80 ֵĿԳɼ¼** + +```sql +--ʵַʽ1Ӳѯ +--ʵַʽ2: Ӳѯ +``` + + +#### Ӳѯص + +1ʹ + +1. ԳΪSQL Ķ֡ +2. **ӲѯΪѯʹ** + +```sql +--ϰ:ѯѧǰͬѧϢ +``` + +1. **ӲѯΪʱʹ** from () + +```sql +--ϰ:ѯѧHTMLɼѧûгɼʾnull +``` + +1. **ӲѯΪʹ** + +```sql +--ϰ:ѯѧHTMLɼѧûгɼʾnull +``` + +2SQL ĸӶȣSQL Ŀɶԡ + +#### Ӳѯ + +![](https://gitee.com/snailclass/tuchuang/raw/master/img/image-20220906173549480-2022-9-1913:24:45.png) + +##### + +###### Ӳѯ + +**ͨʹӲѯ** + +ӲѯָӲѯص**һֵ**һֻһַ + +```sql +--1.ѯĿ(tb_bibliography)У(barcode)Ϊ"TP31113"ͼ飨ҪͼȲ"TP31L13"ISBNţͼ(name)(author) + + +--2.ѯı(tb-recordΪ"˳"ҪѧȲ"˳'ѧţѧĽϢʾѧ(stu-num)(barcode) +``` + + + +###### Ӳѯ + +**ӲѯָӲѯصĽN1**ýͨԶԱijֶεIJѯ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) + +``` + + + +###### Ӳѯ + +ӲѯָӲѯصĽ1NУӲѯĽͨǶԱijݽвѯصĽ + +```sql +--1ѯĿ(tb_bibliography)롶Ϣϵͳʵý̳̣3棩ͬ(author)ͬ(category)顣 +--2ѯѧ(tb_student)"'ͬһѧԺ(school)ͬһ꼶ѧͷλͬͬһ꼶ѧѧšѧԺ +``` + + + +###### Ӳѯ(fromӲѯ) + +ӲѯָӲѯMNеĽѯǶ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*PageSiz` + + + +#### ҳѯϰ + +```sql +--ݽṹ£ +create table Student +( + StuId int primary key identity(1,2), --Զ + StuName varchar(20), + StuSex varchar(4) +) \ No newline at end of file -- Gitee