From 53e990b577bd9d72c59d64e925a6d5f199f435f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=AD=A3=E8=B1=AA?= Date: Tue, 27 Sep 2022 15:56:16 +0000 Subject: [PATCH 1/2] =?UTF-8?q?=E6=96=B0=E5=BB=BA=2037=E5=BC=A0=E6=AD=A3?= =?UTF-8?q?=E8=B1=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "37\345\274\240\346\255\243\350\261\252/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "37\345\274\240\346\255\243\350\261\252/.keep" diff --git "a/37\345\274\240\346\255\243\350\261\252/.keep" "b/37\345\274\240\346\255\243\350\261\252/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From a2c6fb752a35833248d00b587840a20c8126731f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=AD=A3=E8=B1=AA?= Date: Tue, 27 Sep 2022 15:56:39 +0000 Subject: [PATCH 2/2] =?UTF-8?q?37=E5=BC=A0=E6=AD=A3=E8=B1=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张正豪 --- .../\344\275\234\344\270\232/SQLQuery1.sql" | 30 +++ .../\347\254\224\350\256\260.txt" | 27 +++ .../DBTEST.sql" | 225 ++++++++++++++++++ .../SQLQuery1.sql" | 57 +++++ .../\344\275\234\344\270\232.txt" | 12 + ...\347\273\237\345\207\275\346\225\2601.sql" | 54 +++++ .../\344\275\234\344\270\232/SQLQuery1.sql" | 50 ++++ .../\344\275\234\344\270\232/banktest.sql" | 107 +++++++++ ...5\350\257\242\347\254\224\350\256\260.txt" | 7 + .../\344\275\234\344\270\232/SQLQuery1.sql" | 14 ++ .../\347\254\224\350\256\260.txt" | 9 + .../\344\275\234\344\270\232/SQLQuery1.sql" | 41 ++++ .../\347\254\224\350\256\260.txt" | 6 + .../\344\275\234\344\270\232/SQLQuery1.sql" | 85 +++++++ .../\347\254\224\350\256\260.txt" | 8 + 15 files changed, 732 insertions(+) create mode 100644 "37\345\274\240\346\255\243\350\261\252/2022-09.26\350\247\206\345\233\276\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" create mode 100644 "37\345\274\240\346\255\243\350\261\252/2022-09.26\350\247\206\345\233\276\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\347\254\224\350\256\260.txt" create mode 100644 "37\345\274\240\346\255\243\350\261\252/9-19\345\274\240\346\255\243\350\261\252/DBTEST.sql" create mode 100644 "37\345\274\240\346\255\243\350\261\252/9-19\345\274\240\346\255\243\350\261\252/SQLQuery1.sql" create mode 100644 "37\345\274\240\346\255\243\350\261\252/9-19\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232.txt" create mode 100644 "37\345\274\240\346\255\243\350\261\252/9-19\345\274\240\346\255\243\350\261\252/\347\263\273\347\273\237\345\207\275\346\225\2601.sql" create mode 100644 "37\345\274\240\346\255\243\350\261\252/9-20\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" create mode 100644 "37\345\274\240\346\255\243\350\261\252/9-20\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/banktest.sql" create mode 100644 "37\345\274\240\346\255\243\350\261\252/9-20\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\345\255\220\346\237\245\350\257\242\347\254\224\350\256\260.txt" create mode 100644 "37\345\274\240\346\255\243\350\261\252/9-23\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" create mode 100644 "37\345\274\240\346\255\243\350\261\252/9-23\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\347\254\224\350\256\260.txt" create mode 100644 "37\345\274\240\346\255\243\350\261\252/9.21\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" create mode 100644 "37\345\274\240\346\255\243\350\261\252/9.21\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\347\254\224\350\256\260.txt" create mode 100644 "37\345\274\240\346\255\243\350\261\252/9.22\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" create mode 100644 "37\345\274\240\346\255\243\350\261\252/9.22\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\347\254\224\350\256\260.txt" diff --git "a/37\345\274\240\346\255\243\350\261\252/2022-09.26\350\247\206\345\233\276\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" "b/37\345\274\240\346\255\243\350\261\252/2022-09.26\350\247\206\345\233\276\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000..fac0f2e --- /dev/null +++ "b/37\345\274\240\346\255\243\350\261\252/2022-09.26\350\247\206\345\233\276\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" @@ -0,0 +1,30 @@ +--дͼʵֲѯп˻Ϣʾţ֤ +create view v_information +as +select AccountCode,CardNo,RealName,CardMoney from AccountInfo inner join BankCard on BankCard.AccountId=AccountInfo.AccountId +go +select * from v_information +drop view v_information + + + +--ת +create table huatai( + year int, + month int, + amount float +) + +insert into huatai(year,month,amount) values (1991,1,1.1) +insert into huatai(year,month,amount) values (1991,2,1.2) +insert into huatai(year,month,amount) values (1991,3,1.3) +insert into huatai(year,month,amount) values (1991,4,1.4) + +insert into huatai(year,month,amount) values (1992,1,2.1) +insert into huatai(year,month,amount) values (1992,2,2.2) +insert into huatai(year,month,amount) values (1992,3,2.3) +insert into huatai(year,month,amount) values (1992,4,2.4) + +select * from huatai + +select * from huatai pivot(max(amount) for month in ([1],[2],[3],[4])) a \ No newline at end of file diff --git "a/37\345\274\240\346\255\243\350\261\252/2022-09.26\350\247\206\345\233\276\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\347\254\224\350\256\260.txt" "b/37\345\274\240\346\255\243\350\261\252/2022-09.26\350\247\206\345\233\276\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\347\254\224\350\256\260.txt" new file mode 100644 index 0000000..c7f3301 --- /dev/null +++ "b/37\345\274\240\346\255\243\350\261\252/2022-09.26\350\247\206\345\233\276\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\347\254\224\350\256\260.txt" @@ -0,0 +1,27 @@ +视图 + +创建视图 + + create view V_name as select row_name from table_name + +删除视图 + + drop view V_name + +### union + + SELECT _column_name(s)_ FROM _table1_ + UNION + SELECT _column_name(s)_ FROM _table2_ + +### union all + + SELECT _column_name(s)_ FROM _table1_ + UNION ALL + SELECT _column_name(s)_ FROM _table2_ + +### unpivot + +select from Sales + + unpivot (Amount for people in (A1,A2,A3,A4) as a \ No newline at end of file diff --git "a/37\345\274\240\346\255\243\350\261\252/9-19\345\274\240\346\255\243\350\261\252/DBTEST.sql" "b/37\345\274\240\346\255\243\350\261\252/9-19\345\274\240\346\255\243\350\261\252/DBTEST.sql" new file mode 100644 index 0000000..afff882 --- /dev/null +++ "b/37\345\274\240\346\255\243\350\261\252/9-19\345\274\240\346\255\243\350\261\252/DBTEST.sql" @@ -0,0 +1,225 @@ +if exists(select * from sys.databases where name='DBTEST') +drop database DBTEST1; +go + +create database DBTEST1; +go + +use DBTEST1; +go + +--ű +create table Department +( + --ű;int;primary key;identity(1,1)1ʼΪ1; + DepartmentId int primary key identity(1,1), + --;nvarchar(50)50ַ;not nullΪ; + DepartmentName nvarchar(50) not null, + --textı; + DepartmentRemark text +); +go + +--ְrankΪϵͳؼ֣˴ʹ[]Զ֣ϵͳؼ +create table [Rank] +( + RankId int primary key identity(1,1), + RankName nvarchar(50) not null, + RankRemark text +); +go + +--ԱϢ +create table People +( + PeopleId int primary key identity(1,1), + --references,ֶαԼ + DepartmentId int references Department(DepartmentId) not null, + RankId int references [Rank](RankId) not null, + PeopleName nvarchar(50) not null, + --defaultֶĬֵ; checkԹ涨ֵֶԼ; + PeopleSex nvarchar(1) default('') check(PeopleSex='' or PeopleSex='Ů') not null, + PeopleBirth datetime not null, + PeopleSalary decimal(12,2) check(PeopleSalary>= 1000 and PeopleSalary <= 100000) not null, + --uniqueΨһԼΪṩΨһԱ֤; + PeoplePhone nvarchar(20) unique not null, + PeopleAddress nvarchar(100), + --datetimesmalldatetimeԱʾʱͣgetdate()ڻȡϵͳǰʱ + PeopleAddTime smalldatetime default(getdate()) +); +go + + + +------------------------------ݲ------------------------------ +--ű +insert into Department values('','˾IJ'); +go + +--һԲ +insert into Department(DepartmentName,DepartmentRemark) +select 'г','ţIJ' union +select 'Ʒ','ǿյIJ' union +select 'ܾ','쵼IJ' ; +go + +-------ְ +insert into [Rank](RankName,RankRemark) +values('','') +insert into [Rank](RankName,RankRemark) +values('м','߱') +insert into [Rank](RankName,RankRemark) +values('߼','߱Դȫ'); +go + +---------Ա +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,3,'','','1984-7-9',20000,'13554785452','ɶ',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,2,'','Ů','1987-7-9',15000,'13256854578','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,1,'','','1988-8-8',12000,'13985745871','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,1,'ŷ','','1990-8-8',8000,'13535987412','˲',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,3,'','','1989-4-8',9000,'13845789568','˲',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,3,'','','1995-4-8',9500,'13878562568','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,2,'Ƹ','','1989-4-20',8500,'13335457412','人',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,1,'','Ů','1989-4-20',6500,'13437100050','人',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,2,'ܲ','','1987-12-20',25000,'13889562354','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,3,'','','1981-11-11',9000,'13385299632','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,1,'Τ','','1978-1-13',8000,'13478545263','Ϻ',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,1,'','','1998-12-12',7500,'13878523695','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,3,'','','1968-11-22',9000,'13698545841','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,3,'','','1988-1-22',11000,'13558745874','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,2,'Ȩ','','1990-2-21',12000,'13698745214','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,2,'','Ů','1995-2-21',13000,'13985478512','Ϻ',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,1,'С','Ů','1996-2-21',13500,'13778787874','',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,2,'','','1992-10-11',8000,'13987455214','人',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(2,3,'³','','1984-9-10',5500,'13254785965','ɶ',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(3,3,'','','1987-5-19',8500,'13352197364','ɶ',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,1,'½ѷ','','1996-5-19',7500,'13025457392','Ͼ',getdate()) + +insert into People(DepartmentId,RankId,PeopleName,PeopleSex,PeopleBirth, +PeopleSalary,PeoplePhone,PeopleAddress,PeopleAddTime) +values(1,2,'̫ʷ','','1983-6-1',7500,'13077778888','Ϻ',getdate()) + + +---ѯ +select * from Department +select * from [Rank] +select * from People + + +--1. ѯ + +--2. ָвѯԱн绰 +select PeopleName as ,Peoplesex as Ա from People; +--3. ָвѯ,ԶԱн绰 +--4. ѯ˾ԱڳУҪظݣ + +--ظУdistinct +select distinct peopleAddress from People; +--5. 蹤յ10%ѯԭʼʺ͵ĹʣʾԱнннвѯ +select top 3 PeopleName , PeopleSalary н, PeopleSalary*1.1 нн from People; + +--top ordey by + + +select * from People +--ָУԱн绰ѯԱΪŮԱϢ,Զ +--2. ѯнڵ10000 ԱϢ( ) +--3. ѯнڵ10000 ŮԱϢ() +select * from People where PeopleSalary>=10000 and PeopleSex='Ů' +--4. ʾ1980-1-1֮󣬶нڵ10000ŮԱϢ +select * from People where PeopleSalary>=10000 and PeopleSex='Ů' and PeopleBirth>'1995-1-1'; + +--5. ʾнڵ15000 Ա,нڵ8000ŮԱϢ +select * from People where (PeopleSalary>=8000 and peoplesex='Ů') or PeopleSalary>=15000 +--6. ѯн10000-20000 ֮ԱϢ( ) +select * from People where PeopleSalary between 10000 and 20000 +--7. ѯַڱϺԱϢ +--8. ѯԱϢ(ݹ򣬽): ascĬϣ: desc: +select * from People order by PeopleSalary desc + +--9. ʾеԱϢֵijȽе +--10. ѯߵ5˵Ϣ +select top 5 * from People order by PeopleSalary desc +--11. ѯߵ10%ԱϢ +select top 10 percent * from People order by PeopleSalary desc +--12. ѯַûдԱϢ +--Ϊղ=is null, is not null +select * from People where PeopleAddress is null; + +--13. ѯַѾдԱϢ +--14. ѯе80ԱϢ + +--15. ѯ30-40 ֮䣬ҹ15000-30000 ֮ԱϢ +--16. ѯз 6.22--7.22 ԱϢ +select * from people where +(MONTH(PeopleBirth)=6 and DAY(PeopleBirth)>=22) or +(MONTH(PeopleBirth)=7 and DAY(PeopleBirth)<=22) +; +--17. ѯʱƸߵ +select * from people where PeopleSalary > 9000 +--18. ѯͬһе +--19. ѯФΪԱϢ +--20. ѯԱϢһʾ(,ţ,,,,,,,,,,) +--case when end diff --git "a/37\345\274\240\346\255\243\350\261\252/9-19\345\274\240\346\255\243\350\261\252/SQLQuery1.sql" "b/37\345\274\240\346\255\243\350\261\252/9-19\345\274\240\346\255\243\350\261\252/SQLQuery1.sql" new file mode 100644 index 0000000..4cace76 --- /dev/null +++ "b/37\345\274\240\346\255\243\350\261\252/9-19\345\274\240\346\255\243\350\261\252/SQLQuery1.sql" @@ -0,0 +1,57 @@ +USE DBTEST1; +select * from Department;--ű +select * from [Rank];--ְ +select * from People;--ԱϢ +--1. ѯ人еԱϢҪʾԼԱϸ +--select * from People where PeopleAddress +--in +--(select PeopleAddress from People where PeopleAddress = '人') + +select De.DepartmentRemark ڲ ,Pe.* from People Pe +join Department De on De.DepartmentId = Pe.DepartmentId +where PeopleAddress = '人'; + + +--2. ѯ人еԱϢҪʾƣְԼԱϸ +select De.DepartmentRemark ڲ,Ra.RankRemark ְ,Pe.* from People Pe +join Department De on De.DepartmentId = Pe.DepartmentId +join [Rank] Ra on Ra.RankId = Pe.RankId +where PeopleAddress = '人'; + +--3. ݲŷͳԱԱܺͣƽʣ߹ʺ͹ʡ +select count(DepartmentId)Ա,sum(PeopleSalary)Աܺ,AVG(PeopleSalary)ƽ,max(PeopleSalary)߹,min(PeopleSalary)͹ from People + group by DepartmentId; +-- select * from People;--ԱϢ + +--4. ݲŷͳԱԱܺͣƽʣ߹ʺ͹ʣƽ10000 µIJͳƣҸƽʽС +select count(DepartmentId)Ա,sum(PeopleSalary)Աܺ,AVG(PeopleSalary)ƽ,max(PeopleSalary)߹,min(PeopleSalary)͹ from People + group by DepartmentId + having Avg (PeopleSalary) >=10000 + order by ƽ desc; + -- select * from People;--ԱϢ + +--5. ݲƣȻְλƣͳԱԱܺͣƽʣ߹ʺ͹ +select d.DepartmentName ,COUNT(*) Ա,SUM(PeopleSalary) Աܺ ,AVG(PeopleSalary) ƽ,MAX(PeopleSalary) ߹,MIN(PeopleSalary) ͹ from People p +join Department d on d.DepartmentId=p.DepartmentId +group by d.DepartmentName,RankId + + +--6.ѯз 6.22--7.22 ԱϢ +select * from People where PeopleAddTime between '2022-06-22' and '2022-07-22' +update People set PeopleAddTime='2017-07-22 19:08:00' where PeopleId = 10 +--7.ѯԱϢһʾ(,ţ,,,,,,,,,,) +select *,case year(PeopleAddTime) % 12 +when 0 then '' +when 1 then '' +when 2 then '' +when 3 then '' +when 4 then '' +when 5 then 'ţ' +when 6 then '' +when 7 then '' +when 8 then '' +when 9 then '' +when 10 then '' +when 11 then '' +end 'Ф' + from People diff --git "a/37\345\274\240\346\255\243\350\261\252/9-19\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232.txt" "b/37\345\274\240\346\255\243\350\261\252/9-19\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232.txt" new file mode 100644 index 0000000..6e8855c --- /dev/null +++ "b/37\345\274\240\346\255\243\350\261\252/9-19\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232.txt" @@ -0,0 +1,12 @@ + 1. 随机生成短信验证码(6位数字) + + +-- 2. 统计每个用户的所有卡余额之和,并四舍五入,保留一位小数 + + +-- 3.查询用户表中名字部分为中文的所有用户记录(即必须含有中文,但不能全部是中文,比如‘刘备a1’,但不能是'刘备') + +-- 4.找出所有含有非法字符('笨蛋','傻瓜','死')的记录,并这些非法字符,比如 '死张三是傻瓜' 变成 '*张三是**'(几个字符,就替换为几个*) + + +-- 5.查找账号表中相同月份开户的用户 diff --git "a/37\345\274\240\346\255\243\350\261\252/9-19\345\274\240\346\255\243\350\261\252/\347\263\273\347\273\237\345\207\275\346\225\2601.sql" "b/37\345\274\240\346\255\243\350\261\252/9-19\345\274\240\346\255\243\350\261\252/\347\263\273\347\273\237\345\207\275\346\225\2601.sql" new file mode 100644 index 0000000..890919b --- /dev/null +++ "b/37\345\274\240\346\255\243\350\261\252/9-19\345\274\240\346\255\243\350\261\252/\347\263\273\347\273\237\345\207\275\346\225\2601.sql" @@ -0,0 +1,54 @@ +##################数值函数 + + +SELECT CardMoney 金额, CEIL(CardMoney) 向上取整1,CEILING(CardMoney)向上取整2, +FLOOR(CardMoney)向下取整, ROUND(CardMoney) +四舍五入1,ROUND(CardMoney,1) 四舍五入2 FROM `bankcard` ; + + + +## 取得1-10之间的随机数 +SELECT CEIL(RAND()*10); + + + +##################字符串函数 +####### 长度 +#LENGTH: 字节长度(数字,字母,符号占用一个字节,一个汉字占3个字节--utf8) +#CHAR_LENGTH 字符长度 +SELECT RealName,LENGTH(RealName) 字节长度,CHAR_LENGTH(RealName) 字符长度1,CHARACTER_LENGTH(RealName) as 字符长度2 from accountinfo; + + +##找出含有中文的 +SELECT * from accountinfo +where LENGTH(RealName) != CHAR_LENGTH(RealName) ; + +##找出名字全部是汉字的记录 +SELECT * from accountinfo +where LENGTH(RealName) != CHAR_LENGTH(RealName) and LENGTH(RealName)%CHAR_LENGTH(RealName) =0; + +SELECT * from accountinfo +where LENGTH(RealName) =3* CHAR_LENGTH(RealName); + + + + +####### 替换 +##INSERT(str,pos,len,newstr) 指定位置、指定长度替换 +##REPLACE(str,from_str,to_str) 模糊查找替换 + +SELECT RealName,INSERT(RealName,2,1,'三丰'),REPLACE(RealName,'飞','三丰') +from accountinfo +where AccountId=4 +; +##获取日期中的年月 +select CardTime,INSERT(CardTime,5,-1,'') as 年,INSERT(INSERT(CardTime,1,5,''),3,-1,'') as 月 from bankcard; + + +####截取 LEFT(str,len) RIGHT(str,len) substr + +select CardTime,LEFT(CardTime,4)年,right(CardTime,8),SUBSTR(CardTime,6,2),SUBSTR(CardTime,6),SUBSTRing(CardTime,6,2),SUBSTRing(CardTime,6) from bankcard +; + + + diff --git "a/37\345\274\240\346\255\243\350\261\252/9-20\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" "b/37\345\274\240\346\255\243\350\261\252/9-20\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000..24152e6 --- /dev/null +++ "b/37\345\274\240\346\255\243\350\261\252/9-20\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" @@ -0,0 +1,50 @@ +use banktest; +select * from AccountInfo;--˻Ϣ洢Ϣ +select * from BankCard;--п洢пϢ +select * from CardExchange;--Ϣ洢ǮȡǮļ¼ +select * from CardStateChange;--״̬Ϣ仯洢п״̬仯ļ¼״̬1:,2:ʧ,3:,4:ע +select * from CardTransfer;--תϢ洢תϢ¼ +--1. пΪ"6225547858741263"ѯȹпϢʾţ֤ +select * from BankCard Bk +join AccountInfo Ac on Bk.AccountId = Ac.AccountId +where CardMoney > (select CardMoney from BankCard where CardNo = 6225547858741263); +--2. ˻ϢвѯߵĽϸ(ǮȡǮϢ) +select Bk.CardMoney ,Ca.* from BankCard Bk +join CardExchange Ca on Bk.CardNo = Ca.CardNo +where CardMoney =(select Max(CardMoney) from BankCard); +--3. ѯȡ¼п˻Ϣʾţ֤ +select * from BankCard Ba +join AccountInfo Ac on Ba.AccountId = Ac.AccountId + where CardNo not in +(select case MoneyOutBank +when 0 then CardNo +end '' + from CardExchange ); +--4. ѯûд¼п˻Ϣʾţ֤ +select * from BankCard Ba +join AccountInfo Ac on Ba.AccountId = Ac.AccountId +where CardNo in ( +select case MoneyInBank +when 0 then CardNo +end '' + from CardExchange ) +--5. пΪ"6225547858741263",ѯǷյתˡ +select case CardNoIn +when 6225547858741263 then '' +end 'Ƿյת' + from CardTransfer ; +--6. ѯ״ȡп˻Ϣʾţ֤״ +select * from BankCard Ba +join AccountInfo Ac on Ba.AccountId = Ac.AccountId +where CardNo in ( +select top 1 CardNo from CardExchange +group by CardNo +order by count(*) desc); +--7.ѯûת˽׼¼п˻Ϣʾţ֤ +--select * from AccountInfo;--˻Ϣ洢Ϣ +--select * from BankCard;--п洢пϢ +--select * from CardTransfer;--תϢ洢תϢ¼ +select Bk.CardNo ,Bk.CardMoney ,Ac.* from BankCard Bk +join AccountInfo Ac on Bk.AccountId = Ac.AccountId +where CardNo not in (select CardNoOut from CardTransfer) + and CardNo not in (select CardNoIn from CardTransfer); \ No newline at end of file diff --git "a/37\345\274\240\346\255\243\350\261\252/9-20\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/banktest.sql" "b/37\345\274\240\346\255\243\350\261\252/9-20\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/banktest.sql" new file mode 100644 index 0000000..92f9316 --- /dev/null +++ "b/37\345\274\240\346\255\243\350\261\252/9-20\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/banktest.sql" @@ -0,0 +1,107 @@ +create database BankTest; +go +use banktest; +go +--˻Ϣ洢Ϣ +create table AccountInfo +( + AccountId int primary key identity(1,1), --˻ + AccountCode varchar(20) not null, --֤ + AccountPhone varchar(20) not null, --绰 + RealName varchar(20) not null, --ʵ + OpenTime smalldatetime not null, --ʱ +) +--п洢пϢ +create table BankCard +( + CardNo varchar(30) primary key, --п + AccountId int not null, --˻ţ˻Ϣγϵ + CardPwd varchar(30) not null, --п + CardMoney money not null, --п + CardState int not null,--1:,2:ʧ,3:,4:ע + CardTime smalldatetime default(getdate()) --ʱ +) +--Ϣ洢ǮȡǮļ¼ +create table CardExchange +( + ExchangeId int primary key identity(1,1), --Զ + CardNo varchar(30) not null, --п(пγϵ) + MoneyInBank money not null, --Ǯ + MoneyOutBank money not null, --ȡǮ + ExchangeTime smalldatetime not null, --ʱ +) +--תϢ洢תϢ¼ +create table CardTransfer +( + TransferId int primary key identity(1,1),--תԶ + CardNoOut varchar(30) not null, --תп(пγϵ) + CardNoIn varchar(30) not null, --תп(пγϵ) + TransferMoney money not null,--׽ + TransferTime smalldatetime not null, --ʱ +) +--״̬Ϣ仯洢п״̬仯ļ¼״̬1:,2:ʧ,3:,4:ע +create table CardStateChange +( + StateId int primary key identity(1,1),--״̬ϢԶ + CardNo varchar(30) not null, --п(пγϵ) + OldState int not null, --пԭʼ״̬ + NewState int not null, --п״̬ + StateWhy varchar(200) not null, --״̬仯ԭ + StateTime smalldatetime not null, --¼ʱ +) + + + +--Ϊŷ˽пIJ +--֤420107198905064135 +--֤420107199507104133 +--ŷ֤420107199602034138 +insert into AccountInfo(AccountCode,AccountPhone,RealName,OpenTime) +values('420107198905064135','13554785425','',GETDATE()) +insert into BankCard(CardNo,AccountId,CardPwd,CardMoney,CardState) +values('6225125478544587',1,'123456',0,1) + +insert into AccountInfo(AccountCode,AccountPhone,RealName,OpenTime) +values('420107199507104133','13454788854','',GETDATE()) +insert into BankCard(CardNo,AccountId,CardPwd,CardMoney,CardState) +values('6225547858741263',2,'123456',0,1) + +insert into AccountInfo(AccountCode,AccountPhone,RealName,OpenTime) +values('420107199602034138','13456896321','ŷ',GETDATE()) +insert into BankCard(CardNo,AccountId,CardPwd,CardMoney,CardState) +values('6225547854125656',3,'123456',0,1) + +select * from AccountInfo +select * from BankCard + + + +--дǮǮ2000ԪǮ8000ԪŷɴǮ500000Ԫ +select * from AccountInfo +update BankCard set CardMoney = CardMoney + 2000 where CardNo = '6225125478544587' +insert into CardExchange(CardNo,MoneyInBank,MoneyOutBank,ExchangeTime) +values('6225125478544587',2000,0,GETDATE()) + +update BankCard set CardMoney = CardMoney + 8000 where CardNo = '6225547858741263' + +insert into CardExchange(CardNo,MoneyInBank,MoneyOutBank,ExchangeTime) +values('6225547858741263',8000,0,GETDATE()) + +update BankCard set CardMoney = CardMoney + 500000 where CardNo = '6225547854125656' +insert into CardExchange(CardNo,MoneyInBank,MoneyOutBank,ExchangeTime) +values('6225547854125656',500000,0,GETDATE()) + +--תˣŷת1000Ԫ +update BankCard set CardMoney = CardMoney -1000 where CardNo = '6225125478544587' +update BankCard set CardMoney = CardMoney + 1000 where CardNo = '6225547854125656' + +insert into CardTransfer(CardNoOut,CardNoIn,TransferMoney,TransferTime) +values('6225125478544587','6225547854125656',1000,GETDATE()) + + + +select * from AccountInfo --Ϣ +select * from BankCard --пϢ +select * from CardStateChange --п״̬ı +select * from CardTransfer --ת˱ +select * from CardExchange --׼¼ \ No newline at end of file diff --git "a/37\345\274\240\346\255\243\350\261\252/9-20\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\345\255\220\346\237\245\350\257\242\347\254\224\350\256\260.txt" "b/37\345\274\240\346\255\243\350\261\252/9-20\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\345\255\220\346\237\245\350\257\242\347\254\224\350\256\260.txt" new file mode 100644 index 0000000..a4a488b --- /dev/null +++ "b/37\345\274\240\346\255\243\350\261\252/9-20\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\345\255\220\346\237\245\350\257\242\347\254\224\350\256\260.txt" @@ -0,0 +1,7 @@ +any -- 满足其中一个条件 +all -- 满足全部条件 +case 字段名 +when 条件 then 结果 +end '结果字段名称' --等于 java 的switch case 用法,加在select 后面 +top 数字 -- 等于 sql 的limit用法,加在select 后面 + diff --git "a/37\345\274\240\346\255\243\350\261\252/9-23\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" "b/37\345\274\240\346\255\243\350\261\252/9-23\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000..aa26ef3 --- /dev/null +++ "b/37\345\274\240\346\255\243\350\261\252/9-23\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" @@ -0,0 +1,14 @@ +--ʹSTUDENTSݿ +use STUDENTS; +--1.tb_student(name) Ϊ50 +create nonclustered index IX_name on tb_student(name) +with( +fillfactor = 50 +); +--2.tb_record(borrow_time,return_time) ʹѯûͬѧûͬѧͼ黹 +create nonclustered index IX_return on tb_record(return_time); +select * from tb_record with (index = IX_return) where return_time is null; +--3.id() ۼtb_book(id) +alter table tb_book add id int; +create index IX_id on tb_book(id); +drop index IX_RealName on AccountInfo \ No newline at end of file diff --git "a/37\345\274\240\346\255\243\350\261\252/9-23\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\347\254\224\350\256\260.txt" "b/37\345\274\240\346\255\243\350\261\252/9-23\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\347\254\224\350\256\260.txt" new file mode 100644 index 0000000..902741e --- /dev/null +++ "b/37\345\274\240\346\255\243\350\261\252/9-23\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\347\254\224\350\256\260.txt" @@ -0,0 +1,9 @@ +IX_ -- 索引命名规范 +nonclustered -- 非聚集索引 +clustered -- 聚集索引 +with( +fillfactor = 数值 +) -- 填充因子 +with (index = 索引名) -- 索引查询 + + diff --git "a/37\345\274\240\346\255\243\350\261\252/9.21\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" "b/37\345\274\240\346\255\243\350\261\252/9.21\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000..6139d2e --- /dev/null +++ "b/37\345\274\240\346\255\243\350\261\252/9.21\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" @@ -0,0 +1,41 @@ +use TEST04; +select * from Student;--ѧ +select * from Course;--γ̱ +select * from Teacher;--ʦ +select * from SC;--ɼ +--13. ƽɼӸߵʾѧпγ̵ijɼԼƽɼ +select * from SC;--ɼ + +select * from SC S1 +join (select [SId],AVG(score) ƽ from SC +group by [SId]) S2 on S2.[SID] = S1.[SID] +order by ƽ desc; +--14. ѯƳɼ߷֡ͷֺƽ֣ + +--ʽʾγ IDγ name߷֣ͷ֣ƽ֣ʣеʣʣ + +--Ϊ>=60еΪ70-80Ϊ80-90Ϊ>=90 + +--Ҫγ̺źѡѯУͬγ̺ +select * from (select'01' γID,''γname, max(score) ߷,min(score)ͷ,avg(score)ƽ from SC where CID = 01) S1, +(select'02' γID,'ѧ'γname, max(score) ߷,min(score)ͷ,avg(score)ƽ from SC where CID = 02)S2, +(select'03' γID,'ѧ'γname, max(score) ߷,min(score)ͷ,avg(score)ƽ from SC where CID = 03)S3; + +--15. Ƴɼ򣬲ʾ Score ظʱοȱ +select S.*,Rank() over (order by score) as '' from + SC S; +--15.1 Ƴɼ򣬲ʾ Score ظʱϲ +select S.*,dense_rank() over (order by score) as '' from + SC S; +--16. ѯѧܳɼܷظʱοȱ +select S.*,Rank() over (order by ܷ) as '' from + (select [SID],sum(score)ܷ from SC group by [SID]) S; +--16.1 ѯѧܳɼܷظʱοȱ +select * from SC;--ɼ + +select S.*,ROW_NUMBER() over (order by ܷ) as '' from + (select [SID],sum(score)ܷ from SC group by [SID]) S; + +--select [SID],sum(score) ܷ from SC +--group by [SID] +--order by sum(score) desc; \ No newline at end of file diff --git "a/37\345\274\240\346\255\243\350\261\252/9.21\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\347\254\224\350\256\260.txt" "b/37\345\274\240\346\255\243\350\261\252/9.21\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\347\254\224\350\256\260.txt" new file mode 100644 index 0000000..ff6fe98 --- /dev/null +++ "b/37\345\274\240\346\255\243\350\261\252/9.21\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\347\254\224\350\256\260.txt" @@ -0,0 +1,6 @@ +row_number() over (partition by 字段名 order by 字段名 ) +-- partition by 等于 group by 的用法,row_number() 窗口函数【数据重复时继续按顺序排】 + +rank() --【数据重复时,排序重复,不保留名次空缺】 + +dense_rank() --【数据重复时,排序重复,保留名次空缺】 \ No newline at end of file diff --git "a/37\345\274\240\346\255\243\350\261\252/9.22\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" "b/37\345\274\240\346\255\243\350\261\252/9.22\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" new file mode 100644 index 0000000..11f636c --- /dev/null +++ "b/37\345\274\240\346\255\243\350\261\252/9.22\345\274\240\346\255\243\350\261\252/\344\275\234\344\270\232/SQLQuery1.sql" @@ -0,0 +1,85 @@ +use banktest1; +select * from AccountInfo; --个人信息 +select * from BankCard; --银行卡信息 +select * from CardStateChange; --银行卡状态更改表 +select * from CardTransfer; --转账表 +select * from CardExchange; --交易记录 +--1. 为赵云此人进行开户开卡操作,赵云身份证:420107199904054233 +declare @name varchar(20),@id varchar(20) +set @name = '赵云' +set @id = '420107199904054233' +insert into AccountInfo values(@id,'13554785426',@name,'2022-09-19 19:20:00') +go +--2. 需要求出张飞的银行卡卡号和余额,张飞身份证:420107199602034138 (1.使用连接查询,2.使用变量) + +--#### 逻辑控制 + +--##### 条件分支if-else +declare @id varchar(20) +set @id = '420107199602034138' +select b.CardNo 卡号,b.CardMoney 余额 from BankCard b +inner join AccountInfo a +on a.AccountId = b.AccountId +where a.AccountCode = @id +go +--​ 3.某用户银行卡号为“6225547854125656”,该用户执行取钱操作,取钱5000元,余额充足则进行取钱操作,并提示"取钱成功",否则提示“余额不足”。 + +--##### 条件分支:case-when +declare @Moey int +select @Moey = CardMoney from BankCard where CardNo='6225547854125656' +print @Moey +select *,case +when @Moey>5000 then '取钱成功' +else '余额不足' +end + from BankCard where CardNo = '6225547854125656' + +--​ 4.查询银行卡信息,将银行卡状态1,2,3,4分别转换为汉字“正常,挂失,冻结,注销”,并且根据银行卡余额显示银行卡等级 30万以下为“普通用户”,30万及以上为"VIP用户", + +----显示列分别为卡号,身份证,姓名,余额,用户等级,银行卡状态。 + +--##### **while** +select t.CardNo 卡号,a.AccountCode 身份证,a.RealName 姓名,t.CardMoney 余额, +case +when t.CardMoney < 300000 then '普通用户' +when t.CardMoney >= 300000 then 'VIP用户' +end 登记 +from AccountInfo a +inner join +( +select * , +case +when b.CardState = 1 then '正常' +when b.CardState = 2 then '挂失' +when b.CardState = 3 then '冻结' +when b.CardState = 4 then '注销' +end 状态 +from BankCard b +) t +on a.AccountId = t.AccountId +go + +--​ 5.循环打印1-10。 +declare @i int +set @i = 1 + +while (@i<=10) +begin + print @i + set @i = @i +1 +end +go +--​ 6.打印99乘法表 +declare @i int = 1 +while @i <= 9 + begin + declare @j int = 1 + declare @result varchar(200) = '' + while @j <= @i + begin + set @result = @result + cast(@i as varchar(1))+ ' * ' + cast(@j as varchar(1)) + ' = '+ cast(@i*@j as varchar(2)) + char(9) + set @j = @j + 1 + end + print @result + set @i = @i + 1 + end \ No newline at end of file diff --git "a/37\345\274\240\346\255\243\350\261\252/9.22\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\347\254\224\350\256\260.txt" "b/37\345\274\240\346\255\243\350\261\252/9.22\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\347\254\224\350\256\260.txt" new file mode 100644 index 0000000..a084603 --- /dev/null +++ "b/37\345\274\240\346\255\243\350\261\252/9.22\345\274\240\346\255\243\350\261\252/\347\254\224\350\256\260/\347\254\224\350\256\260.txt" @@ -0,0 +1,8 @@ +- set @变量名 = 值 + +- select @变量名 = 值 +--例: + select @id = 1001 + set @name = '周飘' +case +when then -- Gitee