From 2e6a36540c0d5bf455732f89053791df3350026e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E8=B4=B5=E6=A3=AE?= <3032059138@qq.com> Date: Sun, 18 Sep 2022 15:07:52 +0000 Subject: [PATCH 1/3] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20?= =?UTF-8?q?=E8=A2=81=E8=B4=B5=E6=A3=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\350\242\201\350\264\265\346\243\256/.keep" | 0 .../SQLQuery1.sql" | 299 ------------------ .../\347\254\224\350\256\260.txt" | 111 ------- 3 files changed, 410 deletions(-) delete mode 100644 "\350\242\201\350\264\265\346\243\256/.keep" delete mode 100644 "\350\242\201\350\264\265\346\243\256/\350\242\201\350\264\265\346\243\256/SQLQuery1.sql" delete mode 100644 "\350\242\201\350\264\265\346\243\256/\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" diff --git "a/\350\242\201\350\264\265\346\243\256/.keep" "b/\350\242\201\350\264\265\346\243\256/.keep" deleted file mode 100644 index e69de29..0000000 diff --git "a/\350\242\201\350\264\265\346\243\256/\350\242\201\350\264\265\346\243\256/SQLQuery1.sql" "b/\350\242\201\350\264\265\346\243\256/\350\242\201\350\264\265\346\243\256/SQLQuery1.sql" deleted file mode 100644 index 0231695..0000000 --- "a/\350\242\201\350\264\265\346\243\256/\350\242\201\350\264\265\346\243\256/SQLQuery1.sql" +++ /dev/null @@ -1,299 +0,0 @@ -if exists(select * from sys.databases where name='DBTEST') -drop database DBTEST; -go - -create database DBTEST; -go - -use DBTEST; -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), - --datetime和smalldatetime都可以表示时间类型,getdate()用于获取系统当前时间 - PeopleAddTime smalldatetime default(getdate()) -); -go - - - -------------------------------插入数据部分------------------------------ ---部门表插入数据 -insert into Department values('行政部','公司主管行政工作的部门'); -go - ---一次性插入多条数据 -insert into Department(DepartmentName,DepartmentRemark) -select '市场部','吹牛的部门' union -select '产品部','天马星空的部门' union -select '总经办','都是领导的部门' ; -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. 显示所有的员工信息,按照名字的长度进行倒序排列 ---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 - ---查询出巨蟹 6.22--7.22 的员工信息 -select * from People where -(MONTH(PeopleBirth) = 6 and DAY(PeopleBirth) >=22) or -(MONTH(PeopleBirth) = 7 and DAY(PeopleBirth) <=22) - ---1. 查询出武汉地区所有的员工信息,要求显示部门名称以及员工的详细资料 -select p.*,d.DepartmentName 部门名称 from People p -inner join Department d on p.DepartmentId=d.DepartmentId -where p.PeopleAddress='武汉' - - ---2. 查询出武汉地区所有的员工信息,要求显示部门名称,职级名称以及员工的详细资料 -select p.*,d.DepartmentName 部门名称,r.RankName 职级名称 from People p -inner join Department d on p.DepartmentId=d.DepartmentId -inner join [Rank] r on r.RankId=p.RankId -where p.PeopleAddress='武汉' - ---3. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资。 -select count(*) 员工人数,sum(PeopleSalary) 员工工资总和,convert(decimal(15,2),avg(PeopleSalary)) 平均工资,max(PeopleSalary) 最高工资,min(PeopleSalary) 最低工资 from people p -inner join Department d on d.DepartmentId=p.DepartmentId -group by d.DepartmentId - ---4. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资,平均工资在10000 以下的不参与统计, ---并且根据平均工资降序排列。 -select count(*) 员工人数,sum(PeopleSalary) 员工工资总和,convert(decimal(15,2),avg(PeopleSalary)) 平均工资,max(PeopleSalary) 最高工资,min(PeopleSalary) 最低工资 from people p -inner join Department d on d.DepartmentId=p.DepartmentId -group by d.DepartmentId -having convert(decimal(15,2),avg(PeopleSalary))>=10000 -order by 平均工资 desc - ---5. 根据部门名称,然后根据职位名称,分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资 -select [Rank].RankName 职位名称,count(*) 员工人数,sum(PeopleSalary) 员工工资总和,convert(decimal(15,2),avg(PeopleSalary)) 平均工资,max(PeopleSalary) 最高工资, -min(PeopleSalary) 最低工资 from Department -inner join People on People.DepartmentId=Department.DepartmentId -inner join [Rank] on People.RankId=[Rank].RankId -group by [Rank].RankName - ---查询所有员工信息,添加一列显示属相(鼠,牛,虎,兔,龙,蛇,马,羊,猴,鸡,狗,猪) -select PeopleName 姓名,PeopleSex 性别,PeopleSalary 工资,PeoplePhone 电话,PEOPLEBIRTH 生日, -case -when year(PeopleBirth) % 12 = 1 then '鼠' -when year(PeopleBirth) % 12 = 2 then '牛' -when year(PeopleBirth) % 12 = 3 then '虎' -when year(PeopleBirth) % 12 = 4 then '兔' -when year(PeopleBirth) % 12 = 5 then '龙' -when year(PeopleBirth) % 12 = 6 then '蛇' -when year(PeopleBirth) % 12 = 7 then '马' -when year(PeopleBirth) % 12 = 8 then '羊' -when year(PeopleBirth) % 12 = 9 then '猴' -when year(PeopleBirth) % 12 = 10 then '鸡' -when year(PeopleBirth) % 12 = 11 then '狗' -when year(PeopleBirth) % 12 = 0 then '猪' -else '' -end "生肖" -from People - -select PeopleName 姓名,PeopleSex 性别,PeopleSalary 工资,PeoplePhone 电话,PEOPLEBIRTH 生日, -case - when year(PeopleBirth) % 12 = 4 then '鼠' - when year(PeopleBirth) % 12 = 5 then '牛' - when year(PeopleBirth) % 12 = 6 then '虎' - when year(PeopleBirth) % 12 = 7 then '兔' - when year(PeopleBirth) % 12 = 8 then '龙' - when year(PeopleBirth) % 12 = 9 then '蛇' - when year(PeopleBirth) % 12 = 10 then '马' - when year(PeopleBirth) % 12 = 11 then '羊' - when year(PeopleBirth) % 12 = 0 then '猴' - when year(PeopleBirth) % 12 = 1 then '鸡' - when year(PeopleBirth) % 12 = 2 then '狗' - when year(PeopleBirth) % 12 = 3 then '猪' - ELSE '' -end 生肖 -from People \ No newline at end of file diff --git "a/\350\242\201\350\264\265\346\243\256/\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" "b/\350\242\201\350\264\265\346\243\256/\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" deleted file mode 100644 index d318cf7..0000000 --- "a/\350\242\201\350\264\265\346\243\256/\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" +++ /dev/null @@ -1,111 +0,0 @@ -**SQL涓父鐢ㄨ繍绠楃** - -```sql -=锛氱瓑浜庯紝姣旇緝鏄惁鐩哥瓑鍙婅祴鍊 -!=锛氭瘮杈冧笉绛変簬 ->锛氭瘮杈冨ぇ浜 -<锛氭瘮杈冨皬浜 ->=锛氭瘮杈冨ぇ浜庣瓑浜 -<=锛氭瘮杈冨皬浜庣瓑浜 -IS NULL锛氭瘮杈冧负绌 -IS NOT NULL锛氭瘮杈冧笉涓虹┖ -in锛氭瘮杈冩槸鍚﹀湪鍏朵腑 -like锛氭ā绯婃煡璇 -BETWEEN...AND...锛氭瘮杈冩槸鍚﹀湪涓よ呬箣闂 50-100 BETWEEN 50 AND 100 -and锛氶昏緫涓庯紙涓や釜鏉′欢鍚屾椂鎴愮珛琛ㄨ揪寮忔垚绔嬶級 -or锛氶昏緫鎴栵紙涓や釜鏉′欢鏈変竴涓垚绔嬭〃杈惧紡鎴愮珛锛 -not锛氶昏緫闈烇紙鏉′欢鎴愮珛锛岃〃杈惧紡鍒欎笉鎴愮珛锛涙潯浠朵笉鎴愮珛锛岃〃杈惧紡鍒欐垚绔嬶級 -``` -#### 妯$硦鏌ヨ - -妯$硦鏌ヨ浣跨敤like鍏抽敭瀛楀拰閫氶厤绗︾粨鍚堟潵瀹炵幇锛岄氶厤绗﹀叿浣撳惈涔夊涓嬶細 - -```sql -%锛氫唬琛ㄥ尮閰0涓瓧绗︺1涓瓧绗︽垨澶氫釜瀛楃銆 -_锛氫唬琛ㄥ尮閰嶆湁涓斿彧鏈1涓瓧绗︺ -[]锛氫唬琛ㄥ尮閰嶈寖鍥村唴 -[^]锛氫唬琛ㄥ尮閰嶄笉鍦ㄨ寖鍥村唴 -``` -#### 鑱氬悎鍑芥暟 - -SQL SERVER涓仛鍚堝嚱鏁颁富瑕佹湁锛 - -```sql -count:姹傛暟閲 -max:姹傛渶澶у -min:姹傛渶灏忓 -sum:姹傚拰 -avg:姹傚钩鍧囧 -``` -ROUND鍑芥暟鐢ㄦ硶锛 - -```sql -round(num,len,[type]) -鍏朵腑: -num琛ㄧず闇瑕佸鐞嗙殑鏁板瓧锛宭en琛ㄧず闇瑕佷繚鐣欑殑闀垮害锛宼ype澶勭悊绫诲瀷(0鏄粯璁ゅ间唬琛ㄥ洓鑸嶄簲鍏ワ紝闈0浠h〃鐩存帴鎴彇) -select ROUND(123.45454,3) --123.45500 -select ROUND(123.45454,3,1) --123.45400 - -CONVERT()涓嶤AST()鍑芥暟: - -```sql ---1.淇濈暀灏忔暟 -convert(decimal(13,2),12.45454) -cast(12.45454 as decimal(13,2)) ---2.寮哄埗杞崲绫诲瀷 -``` - - - -#### SQL涓父鐢ㄧ殑鏃堕棿鍑芥暟 - -```sql -select DATEDIFF(day, '2019-08-20', getDate()); --鑾峰彇鎸囧畾鏃堕棿鍗曚綅鐨勫樊鍊 -SELECT DATEADD(MINUTE,-5,GETDATE()) --鍔犲噺鏃堕棿,姝ゅ涓鸿幏鍙栦簲鍒嗛挓鍓嶇殑鏃堕棿,MINUTE 琛ㄧず鍒嗛挓锛屽彲涓 YEAR,MONTH,DAY,HOUR -select DATENAME(month, getDate()); --褰撳墠鏈堜唤 -select DATENAME(WEEKDAY, getDate()); --褰撳墠鏄熸湡鍑 -select DATEPART(month, getDate()); --褰撳墠鏈堜唤 -select DAY(getDate()); --杩斿洖褰撳墠鏃ユ湡澶╂暟 -select MONTH(getDate()); --杩斿洖褰撳墠鏃ユ湡鏈堟暟 -select YEAR(getDate()); --杩斿洖褰撳墠鏃ユ湡骞存暟 - -SELECT CONVERT(VARCHAR(22),GETDATE(),20) --2020-01-09 14:46:46 -SELECT CONVERT(VARCHAR(24),GETDATE(),21) --2020-01-09 14:46:55.91 -SELECT CONVERT(VARCHAR(22),GETDATE(),23) --2020-01-09 -SELECT CONVERT(VARCHAR(22),GETDATE(),24) --15:04:07 -Select CONVERT(varchar(20),GETDATE(),14) --15:05:49:330 -``` - -**鏃堕棿鏍煎紡鎺у埗瀛楃涓诧細** - -| 鍚嶇О | 鏃ユ湡鍗曚綅 | 缂╁啓 | -| ------------ | ----------- | --------- | -| 骞 | year | yyyy 鎴杫y | -| 瀛e害 | quarter | qq,q | -| 鏈 | month | mm,m | -| 涓骞翠腑绗嚑澶 | dayofyear | dy,y | -| 鏃 | day | dd,d | -| 涓骞翠腑绗嚑鍛 | week | wk,ww | -| 鏄熸湡 | weekday | dw | -| 灏忔椂 | Hour | hh | -| 鍒嗛挓 | minute | mi,n | -| 绉 | second | ss,s | -| 姣 | millisecond | ms | - -SQL璇彞鎵ц椤哄簭: - -```sql -(7) SELECT -(8) DISTINCT -(1) FROM -(3) JOIN -(2) ON -(4) WHERE -(5) GROUP BY -(6) HAVING -(9) ORDER BY -(10) LIMIT -``` - - - -- Gitee From 8111de8c4d75f34f90e38936aed3d41d06f1dfc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E8=B4=B5=E6=A3=AE?= <3032059138@qq.com> Date: Sun, 18 Sep 2022 15:08:06 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E8=A2=81=E8=B4=B5?= =?UTF-8?q?=E6=A3=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\350\242\201\350\264\265\346\243\256/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "\350\242\201\350\264\265\346\243\256/.keep" diff --git "a/\350\242\201\350\264\265\346\243\256/.keep" "b/\350\242\201\350\264\265\346\243\256/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 533fb56195ee6d1b6519fe0677e827c365c95976 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E8=B4=B5=E6=A3=AE?= <3032059138@qq.com> Date: Sun, 18 Sep 2022 15:08:32 +0000 Subject: [PATCH 3/3] 28 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 琚佽吹妫 <3032059138@qq.com> --- .../SQLQuery1.sql" | 299 ++++++++++++++++++ .../\347\254\224\350\256\260.txt" | 111 +++++++ 2 files changed, 410 insertions(+) create mode 100644 "\350\242\201\350\264\265\346\243\256/\350\242\201\350\264\265\346\243\256/SQLQuery1.sql" create mode 100644 "\350\242\201\350\264\265\346\243\256/\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" diff --git "a/\350\242\201\350\264\265\346\243\256/\350\242\201\350\264\265\346\243\256/SQLQuery1.sql" "b/\350\242\201\350\264\265\346\243\256/\350\242\201\350\264\265\346\243\256/SQLQuery1.sql" new file mode 100644 index 0000000..0231695 --- /dev/null +++ "b/\350\242\201\350\264\265\346\243\256/\350\242\201\350\264\265\346\243\256/SQLQuery1.sql" @@ -0,0 +1,299 @@ +if exists(select * from sys.databases where name='DBTEST') +drop database DBTEST; +go + +create database DBTEST; +go + +use DBTEST; +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), + --datetime和smalldatetime都可以表示时间类型,getdate()用于获取系统当前时间 + PeopleAddTime smalldatetime default(getdate()) +); +go + + + +------------------------------插入数据部分------------------------------ +--部门表插入数据 +insert into Department values('行政部','公司主管行政工作的部门'); +go + +--一次性插入多条数据 +insert into Department(DepartmentName,DepartmentRemark) +select '市场部','吹牛的部门' union +select '产品部','天马星空的部门' union +select '总经办','都是领导的部门' ; +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. 显示所有的员工信息,按照名字的长度进行倒序排列 +--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 + +--查询出巨蟹 6.22--7.22 的员工信息 +select * from People where +(MONTH(PeopleBirth) = 6 and DAY(PeopleBirth) >=22) or +(MONTH(PeopleBirth) = 7 and DAY(PeopleBirth) <=22) + +--1. 查询出武汉地区所有的员工信息,要求显示部门名称以及员工的详细资料 +select p.*,d.DepartmentName 部门名称 from People p +inner join Department d on p.DepartmentId=d.DepartmentId +where p.PeopleAddress='武汉' + + +--2. 查询出武汉地区所有的员工信息,要求显示部门名称,职级名称以及员工的详细资料 +select p.*,d.DepartmentName 部门名称,r.RankName 职级名称 from People p +inner join Department d on p.DepartmentId=d.DepartmentId +inner join [Rank] r on r.RankId=p.RankId +where p.PeopleAddress='武汉' + +--3. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资。 +select count(*) 员工人数,sum(PeopleSalary) 员工工资总和,convert(decimal(15,2),avg(PeopleSalary)) 平均工资,max(PeopleSalary) 最高工资,min(PeopleSalary) 最低工资 from people p +inner join Department d on d.DepartmentId=p.DepartmentId +group by d.DepartmentId + +--4. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资,平均工资在10000 以下的不参与统计, +--并且根据平均工资降序排列。 +select count(*) 员工人数,sum(PeopleSalary) 员工工资总和,convert(decimal(15,2),avg(PeopleSalary)) 平均工资,max(PeopleSalary) 最高工资,min(PeopleSalary) 最低工资 from people p +inner join Department d on d.DepartmentId=p.DepartmentId +group by d.DepartmentId +having convert(decimal(15,2),avg(PeopleSalary))>=10000 +order by 平均工资 desc + +--5. 根据部门名称,然后根据职位名称,分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资 +select [Rank].RankName 职位名称,count(*) 员工人数,sum(PeopleSalary) 员工工资总和,convert(decimal(15,2),avg(PeopleSalary)) 平均工资,max(PeopleSalary) 最高工资, +min(PeopleSalary) 最低工资 from Department +inner join People on People.DepartmentId=Department.DepartmentId +inner join [Rank] on People.RankId=[Rank].RankId +group by [Rank].RankName + +--查询所有员工信息,添加一列显示属相(鼠,牛,虎,兔,龙,蛇,马,羊,猴,鸡,狗,猪) +select PeopleName 姓名,PeopleSex 性别,PeopleSalary 工资,PeoplePhone 电话,PEOPLEBIRTH 生日, +case +when year(PeopleBirth) % 12 = 1 then '鼠' +when year(PeopleBirth) % 12 = 2 then '牛' +when year(PeopleBirth) % 12 = 3 then '虎' +when year(PeopleBirth) % 12 = 4 then '兔' +when year(PeopleBirth) % 12 = 5 then '龙' +when year(PeopleBirth) % 12 = 6 then '蛇' +when year(PeopleBirth) % 12 = 7 then '马' +when year(PeopleBirth) % 12 = 8 then '羊' +when year(PeopleBirth) % 12 = 9 then '猴' +when year(PeopleBirth) % 12 = 10 then '鸡' +when year(PeopleBirth) % 12 = 11 then '狗' +when year(PeopleBirth) % 12 = 0 then '猪' +else '' +end "生肖" +from People + +select PeopleName 姓名,PeopleSex 性别,PeopleSalary 工资,PeoplePhone 电话,PEOPLEBIRTH 生日, +case + when year(PeopleBirth) % 12 = 4 then '鼠' + when year(PeopleBirth) % 12 = 5 then '牛' + when year(PeopleBirth) % 12 = 6 then '虎' + when year(PeopleBirth) % 12 = 7 then '兔' + when year(PeopleBirth) % 12 = 8 then '龙' + when year(PeopleBirth) % 12 = 9 then '蛇' + when year(PeopleBirth) % 12 = 10 then '马' + when year(PeopleBirth) % 12 = 11 then '羊' + when year(PeopleBirth) % 12 = 0 then '猴' + when year(PeopleBirth) % 12 = 1 then '鸡' + when year(PeopleBirth) % 12 = 2 then '狗' + when year(PeopleBirth) % 12 = 3 then '猪' + ELSE '' +end 生肖 +from People \ No newline at end of file diff --git "a/\350\242\201\350\264\265\346\243\256/\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" "b/\350\242\201\350\264\265\346\243\256/\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" new file mode 100644 index 0000000..d318cf7 --- /dev/null +++ "b/\350\242\201\350\264\265\346\243\256/\350\242\201\350\264\265\346\243\256/\347\254\224\350\256\260.txt" @@ -0,0 +1,111 @@ +**SQL涓父鐢ㄨ繍绠楃** + +```sql +=锛氱瓑浜庯紝姣旇緝鏄惁鐩哥瓑鍙婅祴鍊 +!=锛氭瘮杈冧笉绛変簬 +>锛氭瘮杈冨ぇ浜 +<锛氭瘮杈冨皬浜 +>=锛氭瘮杈冨ぇ浜庣瓑浜 +<=锛氭瘮杈冨皬浜庣瓑浜 +IS NULL锛氭瘮杈冧负绌 +IS NOT NULL锛氭瘮杈冧笉涓虹┖ +in锛氭瘮杈冩槸鍚﹀湪鍏朵腑 +like锛氭ā绯婃煡璇 +BETWEEN...AND...锛氭瘮杈冩槸鍚﹀湪涓よ呬箣闂 50-100 BETWEEN 50 AND 100 +and锛氶昏緫涓庯紙涓や釜鏉′欢鍚屾椂鎴愮珛琛ㄨ揪寮忔垚绔嬶級 +or锛氶昏緫鎴栵紙涓や釜鏉′欢鏈変竴涓垚绔嬭〃杈惧紡鎴愮珛锛 +not锛氶昏緫闈烇紙鏉′欢鎴愮珛锛岃〃杈惧紡鍒欎笉鎴愮珛锛涙潯浠朵笉鎴愮珛锛岃〃杈惧紡鍒欐垚绔嬶級 +``` +#### 妯$硦鏌ヨ + +妯$硦鏌ヨ浣跨敤like鍏抽敭瀛楀拰閫氶厤绗︾粨鍚堟潵瀹炵幇锛岄氶厤绗﹀叿浣撳惈涔夊涓嬶細 + +```sql +%锛氫唬琛ㄥ尮閰0涓瓧绗︺1涓瓧绗︽垨澶氫釜瀛楃銆 +_锛氫唬琛ㄥ尮閰嶆湁涓斿彧鏈1涓瓧绗︺ +[]锛氫唬琛ㄥ尮閰嶈寖鍥村唴 +[^]锛氫唬琛ㄥ尮閰嶄笉鍦ㄨ寖鍥村唴 +``` +#### 鑱氬悎鍑芥暟 + +SQL SERVER涓仛鍚堝嚱鏁颁富瑕佹湁锛 + +```sql +count:姹傛暟閲 +max:姹傛渶澶у +min:姹傛渶灏忓 +sum:姹傚拰 +avg:姹傚钩鍧囧 +``` +ROUND鍑芥暟鐢ㄦ硶锛 + +```sql +round(num,len,[type]) +鍏朵腑: +num琛ㄧず闇瑕佸鐞嗙殑鏁板瓧锛宭en琛ㄧず闇瑕佷繚鐣欑殑闀垮害锛宼ype澶勭悊绫诲瀷(0鏄粯璁ゅ间唬琛ㄥ洓鑸嶄簲鍏ワ紝闈0浠h〃鐩存帴鎴彇) +select ROUND(123.45454,3) --123.45500 +select ROUND(123.45454,3,1) --123.45400 + +CONVERT()涓嶤AST()鍑芥暟: + +```sql +--1.淇濈暀灏忔暟 +convert(decimal(13,2),12.45454) +cast(12.45454 as decimal(13,2)) +--2.寮哄埗杞崲绫诲瀷 +``` + + + +#### SQL涓父鐢ㄧ殑鏃堕棿鍑芥暟 + +```sql +select DATEDIFF(day, '2019-08-20', getDate()); --鑾峰彇鎸囧畾鏃堕棿鍗曚綅鐨勫樊鍊 +SELECT DATEADD(MINUTE,-5,GETDATE()) --鍔犲噺鏃堕棿,姝ゅ涓鸿幏鍙栦簲鍒嗛挓鍓嶇殑鏃堕棿,MINUTE 琛ㄧず鍒嗛挓锛屽彲涓 YEAR,MONTH,DAY,HOUR +select DATENAME(month, getDate()); --褰撳墠鏈堜唤 +select DATENAME(WEEKDAY, getDate()); --褰撳墠鏄熸湡鍑 +select DATEPART(month, getDate()); --褰撳墠鏈堜唤 +select DAY(getDate()); --杩斿洖褰撳墠鏃ユ湡澶╂暟 +select MONTH(getDate()); --杩斿洖褰撳墠鏃ユ湡鏈堟暟 +select YEAR(getDate()); --杩斿洖褰撳墠鏃ユ湡骞存暟 + +SELECT CONVERT(VARCHAR(22),GETDATE(),20) --2020-01-09 14:46:46 +SELECT CONVERT(VARCHAR(24),GETDATE(),21) --2020-01-09 14:46:55.91 +SELECT CONVERT(VARCHAR(22),GETDATE(),23) --2020-01-09 +SELECT CONVERT(VARCHAR(22),GETDATE(),24) --15:04:07 +Select CONVERT(varchar(20),GETDATE(),14) --15:05:49:330 +``` + +**鏃堕棿鏍煎紡鎺у埗瀛楃涓诧細** + +| 鍚嶇О | 鏃ユ湡鍗曚綅 | 缂╁啓 | +| ------------ | ----------- | --------- | +| 骞 | year | yyyy 鎴杫y | +| 瀛e害 | quarter | qq,q | +| 鏈 | month | mm,m | +| 涓骞翠腑绗嚑澶 | dayofyear | dy,y | +| 鏃 | day | dd,d | +| 涓骞翠腑绗嚑鍛 | week | wk,ww | +| 鏄熸湡 | weekday | dw | +| 灏忔椂 | Hour | hh | +| 鍒嗛挓 | minute | mi,n | +| 绉 | second | ss,s | +| 姣 | millisecond | ms | + +SQL璇彞鎵ц椤哄簭: + +```sql +(7) SELECT +(8) DISTINCT +(1) FROM +(3) JOIN +(2) ON +(4) WHERE +(5) GROUP BY +(6) HAVING +(9) ORDER BY +(10) LIMIT +``` + + + -- Gitee