From 5eac93e19ea5f3b89c1368c31c6beb4b85b36d05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B6=E5=B0=8F=E6=9D=B0?= <2365584215@qq.com> Date: Sun, 4 Sep 2022 13:18:14 +0000 Subject: [PATCH 1/8] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2040?= =?UTF-8?q?=E5=91=A8=E9=A3=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...45\350\257\242\344\275\234\344\270\232.md" | 357 ------------------ .../\344\275\234\344\270\232/README.md" | 1 - ...344\271\240SQL\345\237\272\347\241\200.md" | 194 ---------- 3 files changed, 552 deletions(-) delete mode 100644 "40\345\221\250\351\243\230/\344\275\234\344\270\232/2022.08.31 \347\254\254\344\270\200\350\212\202\350\257\276\345\244\215\344\271\240-\346\237\245\350\257\242\344\275\234\344\270\232.md" delete mode 100644 "40\345\221\250\351\243\230/\344\275\234\344\270\232/README.md" delete mode 100644 "40\345\221\250\351\243\230/\347\254\224\350\256\260/2022.08.31 \347\254\254\344\270\200\350\212\202\350\257\276-\345\244\215\344\271\240SQL\345\237\272\347\241\200.md" diff --git "a/40\345\221\250\351\243\230/\344\275\234\344\270\232/2022.08.31 \347\254\254\344\270\200\350\212\202\350\257\276\345\244\215\344\271\240-\346\237\245\350\257\242\344\275\234\344\270\232.md" "b/40\345\221\250\351\243\230/\344\275\234\344\270\232/2022.08.31 \347\254\254\344\270\200\350\212\202\350\257\276\345\244\215\344\271\240-\346\237\245\350\257\242\344\275\234\344\270\232.md" deleted file mode 100644 index 984302a..0000000 --- "a/40\345\221\250\351\243\230/\344\275\234\344\270\232/2022.08.31 \347\254\254\344\270\200\350\212\202\350\257\276\345\244\215\344\271\240-\346\237\245\350\257\242\344\275\234\344\270\232.md" +++ /dev/null @@ -1,357 +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. 根据指定列(姓名,性别,月薪,电话)查询性别为女的员工信息,并自定义中文列名 -select PeopleName 姓名,PeopleSex 性别,PeopleSalary 月薪,PeoplePhone 电话 from People where PeopleSex='女' - ---2. 查询月薪大于等于10000 的员工信息( 单条件 ) -select * from People where PeopleSalary>=10000 - ---3. 查询月薪大于等于10000 的女员工信息(多条件) -select * from People where PeopleSalary>=10000 and PeopleSex='女' - ---4. 显示出出身年月在1980-1-1之后,而且月薪大于等于10000的女员工信息。 -select * from People where PeopleBirth>='1980-1-1' and PeopleSalary>=10000 and PeopleSex='女' - ---5. 显示出月薪大于等于15000 的员工,或者月薪大于等于8000的女员工信息。 -select * from People where PeopleSalary>=80000 and PeopleSex='女' or PeopleSalary>=15000 - ---6. 查询月薪在10000-20000 之间员工信息( 多条件 ) -select * from People where PeopleSalary between 10000 and 20000 - ---7. 查询出地址在北京或者上海的员工信息 -select * from People where PeopleAddress='北京' or PeopleAddress='上海' - ---8. 查询所有员工信息(根据工资排序,降序排列) -select * from People order by PeopleSalary desc - ---9. 显示所有的员工信息,按照名字的长度进行倒序排列 -select * from People order by len(PeopleName) desc - ---10. 查询工资最高的5个人的信息 -select top 5 * from People order by PeopleSalary desc - ---11. 查询工资最高的10%的员工信息 -select top 10 percent * from People order by PeopleSalary desc - ---12. 查询出地址没有填写的员工信息 -select * from People where PeopleAddress is null - ---13. 查询出地址已经填写的员工信息 -select * from People where PeopleAddress is not null - ---14. 查询所有的80后员工信息 -select * from People where PeopleBirth>='1980-1-1' - ---15. 查询年龄在30-40 之间,并且工资在15000-30000 之间的员工信息 -select * from People where year(getdate())-year(PeopleBirth) between 30 and 40 and PeopleSalary between 15000 and 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>(select PeopleSalary from People where PeopleName='赵云') - ---18. 查询出和赵云在同一个城市的人 -select * from People where PeopleAddress=(select PeopleAddress from People where PeopleName='赵云') - ---19. 查询出生肖为鼠的人员信息 - ---20. 查询所有员工信息,添加一列显示属相(鼠,牛,虎,兔,龙,蛇,马,羊,猴,鸡,狗,猪) - - - - - -### ——模糊查询—— - ---1. 查询姓刘的员工信息 -select * from People where PeopleName like '刘%' - ---2. 查询名字中含有 " 尚 " 的员工信息 -select * from People where PeopleName like '%尚%' - ---3. 显示名字中含有“尚”或者“史”的员工信息 -select * from People where PeopleName like '%尚%' or PeopleName like '%史%' - ---4. 查询姓刘的员工,名字是2个字 -select * from People where PeopleName like '刘_' - ---5. 查询出名字最后一个字是香,名字一共三个字的员工信息 -select * from People where PeopleName like '__香' - ---6. 查询出电话号码开头138的员工信息 -select * from People where PeoplePhone like '138%' - ---7. 查询出电话号码开头138的员工信息,第4位可能是7,可能8 ,最后一个号码是5 -select * from People where PeoplePhone like '138[7,8]%5' - ---8. 查询出电话号码开头133的员工信息,第4位是2-5之间的数字 ,最后一个号码不是2和3 -select * from People where PeoplePhone like '133[2-5]%[^2-3]' - - - - - -### ——聚合函数—— - ---1. 求员工总人数 -select count(*) 总人数 from People - ---2. 求最大值,求最高工资 -select max(PeopleSalary) 最高工资 from People - ---3. 求最小值,求最小工资 -select min(PeopleSalary) 最低工资 from People - ---4. 求和,求所有员工的工资总和 -select sum(PeopleSalary) 总工资 from People - ---5. 求平均值,求所有员工的平均工资 -select avg(PeopleSalary) 平均工资 from People -select convert(decimal(12,2),avg(PeopleSalary)) 平均工资 from People - ---6. 求数量,最大值,最小值,总和,平均值,在一行显示 -select count(*) 总人数,max(PeopleSalary) 最高工资,min(PeopleSalary) 最低工资,sum(PeopleSalary) 总工资,convert(decimal(12,2),avg(PeopleSalary)) 平均工资 from People - ---7. 查询出武汉地区的员工人数,总工资,最高工资,最低工资和平均工资 -select count(*) 员工人数,sum(PeopleSalary) 总工资,max(PeopleSalary) 最高工资,min(PeopleSalary) 最低工资 from People where PeopleAddress='武汉' - ---8. 求出工资比平均工资高的人员信息 -select * from People where PeopleSalary>(select avg(PeopleSalary) from People) - ---9. 求数量,年龄最大值,年龄最小值,年龄总和,年龄平均值,在一行显示 -select count(*) 总人数,max(year(getdate())-year(PeopleBirth)) 年龄最大值,min(year(getdate())-year(PeopleBirth)) 年龄最小值, sum(year(getdate())-year(PeopleBirth)) 年龄总和,avg(year(getdate())-year(PeopleBirth)) 年龄平均值 from People - ---10. 计算出月薪在10000 以上的男性员工的最大年龄,最小年龄和平均年龄 -select max(year(getdate())-year(PeopleBirth)) 年龄最大值,min(year(getdate())-year(PeopleBirth)) 年龄最小值, avg(year(getdate())-year(PeopleBirth)) 年龄平均值 from People where PeopleSalary>=10000 and PeopleSex='男' - ---11. 统计出所在地在“武汉或上海”的所有女员工数量以及最大年龄,最小年龄和平均年龄 -select max(year(getdate())-year(PeopleBirth)) 年龄最大值,min(year(getdate())-year(PeopleBirth)) 年龄最小值, avg(year(getdate())-year(PeopleBirth)) 年龄平均值 from People where PeopleAddress in ('武汉','上海') and PeopleSex='女' - ---12. 求出年龄比平均年龄高的人员信息 -select * from People where (year(getdate())-year(PeopleBirth))>(select avg((year(getdate())-year(PeopleBirth))) from People) - - - - - -### ——分组查询—— - ---1. 根据员工所在地区分组统计员工人数 ,员工工资总和 ,平均工资,最高工资和最低工资 -select PeopleAddress,count(*) 员工总人数,sum(PeopleSalary) 工资总和,avg(PeopleSalary) 平均工资,max(PeopleSalary) 最高工资,min(PeopleSalary) 最低工资 from People group by PeopleAddress - ---2. 根据员工所在地区分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资,1985 年及以后出身的员工不参与统计。 -select PeopleAddress,count(*) 员工总人数,sum(PeopleSalary) 工资总和,avg(PeopleSalary) 平均工资,max(PeopleSalary) 最高工资,min(PeopleSalary) 最低工资 from People where PeopleBirth<='1985-1-1' group by PeopleAddress - ---3. 根据员工所在地区分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资,要求筛选出员工人数至少在2人及以上的记录,并且1985年及以后出身的员工不参与统计。 -select PeopleAddress,count(*) 员工总人数,sum(PeopleSalary) 工资总和,avg(PeopleSalary) 平均工资,max(PeopleSalary) 最高工资,min(PeopleSalary) 最低工资 from People where PeopleBirth<='1985-1-1' group by PeopleAddress having count(*)>=2 - - - -### ——连表查询—— - ---1. 查询出武汉地区所有的员工信息,要求显示部门名称以及员工的详细资料 -select * from People p -inner join Department d on p.DepartmentId=d.DepartmentId -where PeopleAddress='武汉' - ---2. 查询出武汉地区所有的员工信息,要求显示部门名称,职级名称以及员工的详细资料 -select * from People p -inner join Department d on p.DepartmentId=d.DepartmentId -inner join [Rank] r on p.RankId=r.RankId -where PeopleAddress='武汉' - ---3. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资。 -select d.DepartmentName,count(*)员工人数,sum(PeopleSalary) 工资总和,avg(PeopleSalary) 平均工资,max(PeopleSalary) 最高工资,min(PeopleSalary) 最低工资 from People p -inner join Department d on p.DepartmentId=d.DepartmentId -group by DepartmentName - ---4. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资,平均工资在10000 以下的不参与统计,并且根据平均工资降序排列。 -select d.DepartmentName,count(*)员工人数,sum(PeopleSalary) 工资总和,avg(PeopleSalary) 平均工资,max(PeopleSalary) 最高工资,min(PeopleSalary) 最低工资 from People p -inner join Department d on p.DepartmentId=d.DepartmentId -group by DepartmentName -having avg(PeopleSalary)>=10000 -order by avg(PeopleSalary) desc - ---5. 根据部门名称,然后根据职位名称,分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资 -select d.DepartmentName,r.RankName,count(*)员工人数,sum(PeopleSalary) 工资总和,avg(PeopleSalary) 平均工资,max(PeopleSalary) 最高工资,min(PeopleSalary) 最低工资 from People p -inner join Department d on p.DepartmentId=d.DepartmentId -inner join [Rank] r on p.RankId=r.RankId -group by DepartmentName,RankName \ No newline at end of file diff --git "a/40\345\221\250\351\243\230/\344\275\234\344\270\232/README.md" "b/40\345\221\250\351\243\230/\344\275\234\344\270\232/README.md" deleted file mode 100644 index 4fef031..0000000 --- "a/40\345\221\250\351\243\230/\344\275\234\344\270\232/README.md" +++ /dev/null @@ -1 +0,0 @@ -6. diff --git "a/40\345\221\250\351\243\230/\347\254\224\350\256\260/2022.08.31 \347\254\254\344\270\200\350\212\202\350\257\276-\345\244\215\344\271\240SQL\345\237\272\347\241\200.md" "b/40\345\221\250\351\243\230/\347\254\224\350\256\260/2022.08.31 \347\254\254\344\270\200\350\212\202\350\257\276-\345\244\215\344\271\240SQL\345\237\272\347\241\200.md" deleted file mode 100644 index 07db239..0000000 --- "a/40\345\221\250\351\243\230/\347\254\224\350\256\260/2022.08.31 \347\254\254\344\270\200\350\212\202\350\257\276-\345\244\215\344\271\240SQL\345\237\272\347\241\200.md" +++ /dev/null @@ -1,194 +0,0 @@ -### 1,增加 insert (into)...values... - -1,创建数据库 -create database+数据库名称 -go - -2,使用数据库 -use +数据库名称 -go - -3,创建表 -create table +表名 - 字段1+数据类型+属性+约束 - ...... - ==(不为空not null;不重复unique;默认default;检查check)== - -4,插入数据库 -insert (into)+表名(字段1,....) -values('字段1',.....) - -5,增加外键 - 外键字段+数据类型+foreign key ==references==+外键表名(外键字段) - -6,排序 -select *from +表名+order by +字段+==desc(降序)(默认升序asc)== - -7,消除重复行 -select distinct +字段 from +表名 - -### 2,删除 - -​ ==drop table +表名== - -### 3,修改update - -update + 表名称 + set + 列名称 = 新值 where 列名称 = 旧值 - -### 4,查询 - -#### 基础查询 - -#### 条件查询 - -**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表示需要处理的数字,len表示需要保留的长度,type处理类型(0是默认值代表四舍五入,非0代表直接截取) -select ROUND(123.45454,3) --123.45500 -select ROUND(123.45454,3,1) --123.45400 -``` - - - -#### 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 或yy | -| 季度 | 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 | - -#### 分组查询 group by - -#### 连接查询 inner join on - -* 内连接(自连接) - select +表1.列1 from 表1 - inner join 表2 on 表1.列=表2.列 - ..... - (where+条件;group by+字段;having+条件;order by+字段) - - - -* 左连接 - select +表1.列1 from 表1 - left join 表2 on 表1.列=表2.列 - left join 表3 on 表2.列=表3.列 - ..... - -* 右连接 - select +表1.列1 from 表1 - right join 表2 on 表1.列=表2.列 - right join 表3 on 表2.列=表3.列 - ..... - -#### 并集查询(用于两个select查询之间) - -select ...... -1,union (不允许有重复列) -2,union all (允许有重复列) -select...... - -* 交集查询(用于两个select查询之间) - select..... - intersect(将两个结果集中相同的记录取出来成为一个新的结果集) - select..... - -* 减集查询(用于两个select查询之间) - select.... - except(比较两个结果集,将except关键字前的结果集除去交集部分而组成的新的集合) - select.... - - - -#### 子查询 - -子查询;一个select-from-where语句称为一个查询块,将一个查询块嵌套在另一个查询块的 -where字句或者having子句短语的条件中的查询称为子查询(嵌套查询)。 -注意; - -* 可以使用子查询的位置:where,select,having,from -* 不可以使用子查询的位置:group by -* 主查询和子查询可以不是同一张表 -* 一般不在子查询中使用排序(但是在top-n问题分析中必须对子查询使用排序) -* 一般先执行子查询,再执行主查询(相关子查询除外) -* 单行子查询只能使用单行操作符;多行子查询只能使用多行操作符 -* 注意:当子查询为null值的问题 -- Gitee From 90ecf1ca7d64a3d0e94726d5a69929dbe56652e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B6=E5=B0=8F=E6=9D=B0?= <2365584215@qq.com> Date: Sun, 4 Sep 2022 13:18:52 +0000 Subject: [PATCH 2/8] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2010?= =?UTF-8?q?=E4=BD=99=E9=87=91=E6=98=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...72\350\241\250\347\273\203\344\271\240.md" | 57 -------------- ...45\350\257\242\344\275\234\344\270\232.md" | 74 ------------------- 2 files changed, 131 deletions(-) delete mode 100644 "10\344\275\231\351\207\221\346\230\237/8.29-\345\273\272\345\272\223\345\273\272\350\241\250\347\273\203\344\271\240.md" delete mode 100644 "10\344\275\231\351\207\221\346\230\237/8.30-\346\235\241\344\273\266\346\237\245\350\257\242\344\275\234\344\270\232.md" diff --git "a/10\344\275\231\351\207\221\346\230\237/8.29-\345\273\272\345\272\223\345\273\272\350\241\250\347\273\203\344\271\240.md" "b/10\344\275\231\351\207\221\346\230\237/8.29-\345\273\272\345\272\223\345\273\272\350\241\250\347\273\203\344\271\240.md" deleted file mode 100644 index e248dee..0000000 --- "a/10\344\275\231\351\207\221\346\230\237/8.29-\345\273\272\345\272\223\345\273\272\350\241\250\347\273\203\344\271\240.md" +++ /dev/null @@ -1,57 +0,0 @@ -```sql -create database DBTEST -go -use DBTEST -go ---部门信息表 -create table sectionInfo( -sectionID int primary key identity(1,1),--部门编号 -sectionName varchar(10) not null --部门名称 -) -go -insert sectionInfo -values('人事部'), - ('行政部'), - ('财会部'), - ('安全部'), - ('销售部') -go ---员工信息表 -create table userInfo( -userNo int primary key identity(1,1) not null,--员工编号 -userName varchar(10) unique not null check(len(userName)>4),--员工姓名 -userSex varchar(2) not null check(userSex='男'or userSex='女'),--员工性别 -userAge int not null check(userAge between 1 and 100),--员工年龄 -userAddress varchar(50) default('湖北'),--员工地址 -userSection int references sectionInfo(sectionID)--员工部门 -) -go -insert userInfo -values('小小小灿芳','男',20,'福建',2), - ('两年半的羿','男',20,'福建',4), - ('羿的铁杆粉','男',20,'福建',5) -insert userInfo(userName,userSex,userAge,userSection) -values('asdzxc','男',20,3), - ('aaaaaaaaaa','男',20,1) -go ---员工考勤表 -create table workInfo( -workId int primary key identity(1,1) not null,--考勤编号 -userId int references userInfo(userNo) not null,--考勤员工 -workTime datetime not null,--考勤时间 -workDescription varchar(40) not null check(workDescription='迟到'or workDescription='早退'or workDescription='旷工' -or workDescription='病假' or workDescription='事假')--考勤说明 -) -go -insert workInfo -values(2,'2022-8-29 10:44:00','事假'), - (3,'2022-8-29 10:32:50','迟到'), - (1,'2022-8-29 10:42:10','早退'), - (5,'2022-8-29 10:01:30','旷工'), - (4,'2022-8-29 10:55:16','病假') -go -select * from sectionInfo -select * from userInfo -select * from workInfo -``` - diff --git "a/10\344\275\231\351\207\221\346\230\237/8.30-\346\235\241\344\273\266\346\237\245\350\257\242\344\275\234\344\270\232.md" "b/10\344\275\231\351\207\221\346\230\237/8.30-\346\235\241\344\273\266\346\237\245\350\257\242\344\275\234\344\270\232.md" deleted file mode 100644 index da55365..0000000 --- "a/10\344\275\231\351\207\221\346\230\237/8.30-\346\235\241\344\273\266\346\237\245\350\257\242\344\275\234\344\270\232.md" +++ /dev/null @@ -1,74 +0,0 @@ -```sql -use DBTEST - ---1. 根据指定列(姓名,性别,月薪,电话)查询性别为女的员工信息,并自定义中文列名 -select PeopleName 姓名,PeopleSex 性别,PeopleSalary 月薪,PeoplePhone 电话 from People where PeopleSex='女' ---2. 查询月薪大于等于10000 的员工信息( 单条件 ) -select * from People where PeopleSalary>=10000 ---3. 查询月薪大于等于10000 的女员工信息(多条件) -select * from People where PeopleSalary>=10000 and PeopleSex='女' ---4. 显示出出身年月在1980-1-1之后,而且月薪大于等于10000的女员工信息。 -select * from People where YEAR(PeopleBirth)>=1980 and PeopleSalary>=10000 ---5. 显示出月薪大于等于15000 的员工,或者月薪大于等于8000的女员工信息。 -select * from People where PeopleSalary>=15000 or PeopleSalary>=8000 ---6. 查询月薪在10000-20000 之间员工信息( 多条件 ) -select * from People where PeopleSalary between 10000 and 20000 ---7. 查询出地址在北京或者上海的员工信息 -select * from People where PeopleAddress='北京' or PeopleAddress='上海' ---8. 查询所有员工信息(根据工资排序,降序排列) -select * from People -order by PeopleSalary desc ---9. 显示所有的员工信息,按照名字的长度进行倒序排列 -select * from People -order by len(PeopleName) desc ---10. 查询工资最高的5个人的信息 -select top 5 * from People ---11. 查询工资最高的10%的员工信息 -select top 10 percent * from People ---12. 查询出地址没有填写的员工信息 -select * from People -where PeopleAddress is null ---13. 查询出地址已经填写的员工信息 -select * from People -where PeopleAddress is not null ---14. 查询所有的80后员工信息 -select * from People -where YEAR(PeopleBirth)>=1980 ---15. 查询年龄在30-40 之间,并且工资在15000-30000 之间的员工信息 -select * from People -where (year(GETDATE())-year(PeopleBirth)) between 30 and 40 and -PeopleSalary between 15000 and 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>(select PeopleSalary from People where PeopleName='赵云') ---18. 查询出和赵云在同一个城市的人 -select * from People -where PeopleAddress=(select PeopleAddress from People where PeopleName='赵云') ---19. 查询出生肖为鼠的人员信息 -select * from People -where year(PeopleBirth)%12 = 4 ---20. 查询所有员工信息,添加一列显示属相(鼠,牛,虎,兔,龙,蛇,马,羊,猴,鸡,狗,猪) -select *, -CASE year(peopleBirth)%12 - when 4 then '鼠' - when 5 then '牛' - when 6 then '虎' - when 7 then '兔' - when 8 then '龙' - when 9 then '蛇' - when 10 then '马' - when 11 then '羊' - when 12 then '猴' - when 1 then '鸡' - when 2 then '狗' - when 3 then '猪' - else '' -end 生肖 -from people -``` - -- Gitee From 849bc74356832a0966efc9e5be089fb724f2f03b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B6=E5=B0=8F=E6=9D=B0?= <2365584215@qq.com> Date: Sun, 4 Sep 2022 13:19:02 +0000 Subject: [PATCH 3/8] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2033?= =?UTF-8?q?=E8=B0=B7=E5=85=86=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...33\350\260\267\345\205\206\346\230\216.md" | 64 ------------------- 1 file changed, 64 deletions(-) delete mode 100644 "33\350\260\267\345\205\206\346\230\216/33\350\260\267\345\205\206\346\230\216.md" diff --git "a/33\350\260\267\345\205\206\346\230\216/33\350\260\267\345\205\206\346\230\216.md" "b/33\350\260\267\345\205\206\346\230\216/33\350\260\267\345\205\206\346\230\216.md" deleted file mode 100644 index 178e9eb..0000000 --- "a/33\350\260\267\345\205\206\346\230\216/33\350\260\267\345\205\206\346\230\216.md" +++ /dev/null @@ -1,64 +0,0 @@ -```sql -create database DBTEST -go - -use DBTEST -go - ---*部门信息表* -create table sectionInfo( -sectionID int primary key identity(1,1),--部门编号 -sectionName varchar(10) not null --部门名称 -) - -insert sectionInfo(sectionName) -values('技术部'), - ('卫生部'), - ('财务部'), - ('人事部'), - ('会计部') - - ---*员工信息表* -create table userInfo( -userNo int primary key identity(100,1),--员工编号 -userName varchar(10) unique not null check(len(userName)>4),--员工姓名 -userSex varchar(2) check(userSex='男' or userSex='女') not null,--员工性别 -userAge int check(userAge between 0 and 100) not null , --员工年龄 -userAddress varchar(50) default('湖北') not null, --员工地址 -userSection int foreign key references sectionInfo(sectionID) --员工部门 -) - -insert userInfo(userName,userSex,userAge,userAddress,userSection) -values('张三das','男',20,'河南省',2), - ('李四sda','女',18,'北京',3), - ('王五asd','男',20,'福建省',2), - ('赵六sad','女',30,'河南省',1), - ('田七sad','男',19,'湖南省',2) - ---*员工考勤表* -create table workInfo( -workId int primary key identity(1,1),--考勤编号 -userId int foreign key references userInfo(userNo) not null, --考勤员工 -workTime datetime not null,--考勤时间 -workDescription varchar(40) check(workDescription='迟到' or - workDescription='早退' or - workDescription='矿工' or - workDescription='病假' or - workDescription='事假') not null --考勤说明 - ) - -insert workInfo(userId,workTime,workDescription) -values (106,'2022-08-17 20:00:00','迟到'), - (107,'2022-08-17 20:00:00','迟到'), - (109,'2022-07-17 20:00:00','迟到'), - (108,'2022-04-16 20:00:00','迟到'), - (108,'2022-03-17 20:00:00','迟到') - - -select * from sectionInfo -select * from userInfo -select * from workInfo - -``` - -- Gitee From 8dbdd8319c23f14405fa4a2e2923484b847c8d17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B6=E5=B0=8F=E6=9D=B0?= <2365584215@qq.com> Date: Sun, 4 Sep 2022 13:19:31 +0000 Subject: [PATCH 4/8] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2011?= =?UTF-8?q?=E5=88=98=E5=98=89=E8=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.keep" | 0 .../\347\254\224\350\256\260.md" | 203 ------------------ 2 files changed, 203 deletions(-) delete mode 100644 "11\345\210\230\345\230\211\350\277\234/.keep" delete mode 100644 "11\345\210\230\345\230\211\350\277\234/\347\254\224\350\256\260.md" diff --git "a/11\345\210\230\345\230\211\350\277\234/.keep" "b/11\345\210\230\345\230\211\350\277\234/.keep" deleted file mode 100644 index e69de29..0000000 diff --git "a/11\345\210\230\345\230\211\350\277\234/\347\254\224\350\256\260.md" "b/11\345\210\230\345\230\211\350\277\234/\347\254\224\350\256\260.md" deleted file mode 100644 index 34f3d05..0000000 --- "a/11\345\210\230\345\230\211\350\277\234/\347\254\224\350\256\260.md" +++ /dev/null @@ -1,203 +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), - --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. ָУԱн绰ѯԱΪŮԱϢ,Զ -select PeopleName ,PeopleSex Ա,PeopleSalary н, PeoplePhone 绰 from People where PeopleSex = 'Ů' ---2. ѯнڵ10000 ԱϢ( ) -select * from People where PeopleSalary>10000 ---3. ѯнڵ10000 ŮԱϢ() -select * from People where PeopleSalary>10000 and PeopleSex = 'Ů' ---4. ʾ1980-1-1֮󣬶нڵ10000ŮԱϢ -select * from People where PeopleBirth>'1980-1-1'and PeopleSalary>10000 and PeopleSex ='Ů' ---5. ʾнڵ15000 Ա,нڵ8000ŮԱϢ -select * from People where PeopleSalary>15000 or PeopleSalary>8000 and PeopleSex='Ů' ---6. ѯн10000-20000 ֮ԱϢ( ) -select * from People where PeopleSalary between 10000 and 20000 ---7. ѯַڱϺԱϢ -select * from People where PeopleAddress='' or PeopleAddress= 'Ϻ' ---8. ѯԱϢ(ݹ򣬽) -select * from People order by PeopleSalary desc ---9. ʾеԱϢֵijȽе -select * from People order by len(PeopleName) ---10. ѯߵ5˵Ϣ -select top 5 * from People order by PeopleSalary desc ---11. ѯߵ10%ԱϢ -select top 10 percent * from People order by PeopleSalary desc ---12. ѯַûдԱϢ -select * from People where PeopleAddress is null ---13. ѯַѾдԱϢ -select * from People where PeopleAddress is not null ---14. ѯе80ԱϢ -select * from People where yearPeopleBirth>80 ---15. ѯ30-40 ֮䣬ҹ15000-30000 ֮ԱϢ ---16. ѯз 6.22--7.22 ԱϢ -select * from People where datediff(day,'06-22','07-22')=30 ---17. ѯʱƸߵ ---18. ѯͬһе ---19. ѯФΪԱϢ ---20. ѯԱϢһʾ(,ţ,,,,,,,,,,) \ No newline at end of file -- Gitee From 1d19b828d2be463fbaa8313171733cdb1b15883d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B6=E5=B0=8F=E6=9D=B0?= <2365584215@qq.com> Date: Sun, 4 Sep 2022 13:19:39 +0000 Subject: [PATCH 5/8] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2030?= =?UTF-8?q?=E5=90=B4=E8=B6=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "30\345\220\264\350\266\205/.keep" | 0 .../8.30\344\275\234\344\270\232.md" | 70 ------------------- 2 files changed, 70 deletions(-) delete mode 100644 "30\345\220\264\350\266\205/.keep" delete mode 100644 "30\345\220\264\350\266\205/8.30\344\275\234\344\270\232.md" diff --git "a/30\345\220\264\350\266\205/.keep" "b/30\345\220\264\350\266\205/.keep" deleted file mode 100644 index e69de29..0000000 diff --git "a/30\345\220\264\350\266\205/8.30\344\275\234\344\270\232.md" "b/30\345\220\264\350\266\205/8.30\344\275\234\344\270\232.md" deleted file mode 100644 index cb2dadd..0000000 --- "a/30\345\220\264\350\266\205/8.30\344\275\234\344\270\232.md" +++ /dev/null @@ -1,70 +0,0 @@ -``` ---1. 查询所有行所有列 -select * from People ---2. 指定列查询(姓名,性别,月薪,电话) -select PeopleName,PeopleSex,PeopleSalary,PeoplePhone from People ---3. 指定列查询,并自定义中文列名(姓名,性别,月薪,电话) -select PeopleName 姓名,PeopleSex 性别,PeopleSalary 月薪,PeoplePhone 电话 from People ---4. 查询公司员工所在城市(不需要重复数据) -select distinct PeopleAddress from People ---5. 假设工资普调10%,查询原始工资和调整后的工资,显示(姓名,性别,月薪,加薪后的月薪)(添加列查询)。 -select PeopleName 姓名,PeopleSex 性别,PeopleSalary 月薪,PeopleSalary*1.1 加薪后的月薪 from People - ---1. 根据指定列(姓名,性别,月薪,电话)查询性别为女的员工信息,并自定义中文列名 -select PeopleName 姓名,PeopleSex 性别 ,PeopleSalary 月薪 ,PeoplePhone 电话 from People -where PeopleSex='女' ---2. 查询月薪大于等于10000 的员工信息( 单条件 ) -select * from People -where PeopleSalary >10000 ---3. 查询月薪大于等于10000 的女员工信息(多条件) -select * from People -where PeopleSex='女' and PeopleSalary >10000 ---4. 显示出出身年月在1980-1-1之后,而且月薪大于等于10000的女员工信息。 -select * from People -where PeopleSex='女' and PeopleSalary >10000 and PeopleBirth>1980-1-1 ---5. 显示出月薪大于等于15000 的员工,或者月薪大于等于8000的女员工信息。 -select * from People -where PeopleSalary >= 15000 or( PeopleSalary >8000 and PeopleSex='女') ---6. 查询月薪在10000-20000 之间员工信息( 多条件 ) -select * from people -where PeopleSalary between 10000 and 20000 ---7. 查询出地址在北京或者上海的员工信息 -select * from People -where PeopleAddress ='北京' or PeopleAddress='上海' ---8. 查询所有员工信息(根据工资排序,降序排列) -select * from People -order by PeopleSalary desc ---9. 显示所有的员工信息,按照名字的长度进行倒序排列 -select * from People -order by len(PeopleName) desc ---10. 查询工资最高的5个人的信息 -select top 5 * from People -order by PeopleSalary desc ---11. 查询工资最高的10%的员工信息 - ---12. 查询出地址没有填写的员工信息 -select * from People -where PeopleAddress is null ---13. 查询出地址已经填写的员工信息 -select * from People -where PeopleAddress is not null ---14. 查询所有的80后员工信息 -select * from People -where PeopleBirth>1980-1-1 ---15. 查询年龄在30-40 之间,并且工资在15000-30000 之间的员工信息 -select * from people -where (YEAR(GETDATE())-YEAR(PeopleBirth)) between 30 and 40 ---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 > (select PeopleSalary from People where PeopleName = '赵云') ---18. 查询出和赵云在同一个城市的人 -select * from People -where PeopleAddress = (select PeopleAddress from People where PeopleName = '赵云') ---19. 查询出生肖为鼠的人员信息 -select * from people -where (year(PeopleBirth)%12)-3 =1 ---20. 查询所有员工信息,添加一列显示属相(鼠,牛,虎,兔,龙,蛇,马,羊,猴,鸡,狗,猪) -``` \ No newline at end of file -- Gitee From 9912c150a1672e2e113205d5c8f0b9436c2c6a75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B6=E5=B0=8F=E6=9D=B0?= <2365584215@qq.com> Date: Sun, 4 Sep 2022 13:19:45 +0000 Subject: [PATCH 6/8] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2015?= =?UTF-8?q?=E5=90=B4=E6=98=8E=E6=9D=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...72\350\241\250\347\273\203\344\271\240.md" | 65 ---- ...45\350\257\242\347\273\203\344\271\240.md" | 43 --- ...74\345\220\210\347\273\203\344\271\240.md" | 32 -- .../sql\345\244\215\344\271\240.md" | 283 ------------------ 4 files changed, 423 deletions(-) delete mode 100644 "15\345\220\264\346\230\216\346\235\260/220829\345\273\272\350\241\250\347\273\203\344\271\240.md" delete mode 100644 "15\345\220\264\346\230\216\346\235\260/220830-\346\237\245\350\257\242\347\273\203\344\271\240.md" delete mode 100644 "15\345\220\264\346\230\216\346\235\260/220831-\347\273\274\345\220\210\347\273\203\344\271\240.md" delete mode 100644 "15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/sql\345\244\215\344\271\240.md" diff --git "a/15\345\220\264\346\230\216\346\235\260/220829\345\273\272\350\241\250\347\273\203\344\271\240.md" "b/15\345\220\264\346\230\216\346\235\260/220829\345\273\272\350\241\250\347\273\203\344\271\240.md" deleted file mode 100644 index fb5dd34..0000000 --- "a/15\345\220\264\346\230\216\346\235\260/220829\345\273\272\350\241\250\347\273\203\344\271\240.md" +++ /dev/null @@ -1,65 +0,0 @@ -create database DBTEST -go -use DBTEST -go - -create table sectionlnfo( -sectionID int identity(1,1) primary key, -sectionName varchar(10) not null -) -go - - - -create table userlnfo( -userNo int identity(1,1) primary key not null, -userName varchar(10) Unique not null check(len(userName)>4), -userSex varchar(2) not null check(userSex='男'or userSex='女'), -userAge int not null check(userAge>= 1 and userAge<=100), -userAffress varchar(50) default('湖北'), -userSection int references sectionlnfo(sectionID), -) -go - -drop table userlnfo - -create table worklnfo( -workld int identity(1,1) primary key not null, -userid int references userlnfo(userNO) not null, -workTime datetime not null, -workDescription varchar(40) not null check(workDescription='迟到'or workDescription='早退'), -) -go - -drop table worklnfo - - - -Insert sectionlnfo values -('技术部'), -('开发部'), -('财务部'), -('采购部'), -('生产部') - - - -insert userlnfo values -('aaaaa','男',23,'湖南',1), -('bbbbb','男',18,'',2), -('ccccc','女',20,'龙岩',3), -('ddddd','女',18,'三明',4), -('eeeee','男',30,'福州',5) - -go - -insert worklnfo values -(1,2022-8-29,'迟到'), -(2,2022-8-29,'早退'), -(3,2022-8-29,'迟到'), -(4,2022-8-29,'早退'), -(5,2022-8-29,'迟到') - -select * from sectionlnfo -select * from userlnfo -select * from worklnfo \ No newline at end of file diff --git "a/15\345\220\264\346\230\216\346\235\260/220830-\346\237\245\350\257\242\347\273\203\344\271\240.md" "b/15\345\220\264\346\230\216\346\235\260/220830-\346\237\245\350\257\242\347\273\203\344\271\240.md" deleted file mode 100644 index f5641f2..0000000 --- "a/15\345\220\264\346\230\216\346\235\260/220830-\346\237\245\350\257\242\347\273\203\344\271\240.md" +++ /dev/null @@ -1,43 +0,0 @@ ---4. 查询公司员工所在城市(不需要重复数据) -select distinct PeopleAddress from People ---5. 假设工资普调10%,查询原始工资和调整后的工资,显示(姓名,性别,月薪,加薪后的月薪)(添加列查询)。 -select top 10 percent * from People ------- - ---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 >( -select PeopleSalary from People where PeopleName='赵云' -) - ---18. 查询出和赵云在同一个城市的人 -select * from People where PeopleAddress in( -select PeopleAddress from People where PeopleName='赵云' -) ---19. 查询出生肖为鼠的人员信息 -select * from People where year(PeopleBirth) % 12 = 4 ---20. 查询所有员工信息,添加一列显示属相(鼠,牛,虎,兔,龙,蛇,马,羊,猴,鸡,狗,猪) - -``` -select *, -case year(PeopleBirth) % 12 - when 4 then '鼠' - when 5 then '牛' - when 6 then '虎' - when 7 then '兔' - when 8 then '龙' - when 9 then '蛇' - when 10 then '马' - when 11 then '羊' - when 0 then '猴' - when 1 then '鸡' - when 2 then '狗' - when 3 then '猪' - ELSE '' -end 生肖 -from People -``` - diff --git "a/15\345\220\264\346\230\216\346\235\260/220831-\347\273\274\345\220\210\347\273\203\344\271\240.md" "b/15\345\220\264\346\230\216\346\235\260/220831-\347\273\274\345\220\210\347\273\203\344\271\240.md" deleted file mode 100644 index e0bcae5..0000000 --- "a/15\345\220\264\346\230\216\346\235\260/220831-\347\273\274\345\220\210\347\273\203\344\271\240.md" +++ /dev/null @@ -1,32 +0,0 @@ -select * from Department -select * from [Rank] -select * from People - ---1. 查询出武汉地区所有的员工信息,要求显示部门名称以及员工的详细资料 -select p.*,DepartmentName from People p -join Department d on p.DepartmentId=d.DepartmentId -where PeopleAddress='武汉' - ---2. 查询出武汉地区所有的员工信息,要求显示部门名称,职级名称以及员工的详细资料 -select p.*,DepartmentName,RankName from People p -join Department d on p.DepartmentId=d.DepartmentId -join [Rank]r on p.RankId=r.RankId -where PeopleAddress='武汉' - ---3. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资。 -select DepartmentName,count(*) 员工人数,sum(PeopleSalary) 工资总和,avg(PeopleSalary) 平均工资,max(PeopleSalary)最高工资,min(PeopleSalary)最低工资 from People p -join Department d on p.DepartmentId=d.DepartmentId -group by DepartmentName - ---4. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资,平均工资在10000 以下的不参与统计,并且根据平均工资降序排列。 -select DepartmentName,count(*) 员工人数,sum(PeopleSalary) 工资总和,avg(PeopleSalary) 平均工资,max(PeopleSalary)最高工资,min(PeopleSalary)最低工资 from People p -join Department d on p.DepartmentId=d.DepartmentId -group by DepartmentName -having avg(PeopleSalary)>10000 -order by avg(PeopleSalary) desc - ---5. 根据部门名称,然后根据职位名称,分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资 -select DepartmentName, RankName,count(*) 员工人数,sum(PeopleSalary) 工资总和,avg(PeopleSalary) 平均工资,max(PeopleSalary)最高工资,min(PeopleSalary)最低工资 from People p -join Department d on p.DepartmentId=d.DepartmentId -join [Rank]r on p.RankId=r.RankId -group by DepartmentName,RankName \ No newline at end of file diff --git "a/15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/sql\345\244\215\344\271\240.md" "b/15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/sql\345\244\215\344\271\240.md" deleted file mode 100644 index 6a157ee..0000000 --- "a/15\345\220\264\346\230\216\346\235\260/\347\254\224\350\256\260/sql\345\244\215\344\271\240.md" +++ /dev/null @@ -1,283 +0,0 @@ - - -## 创表约束 - -### 主键 - -```sql -字段 数据类型 primary key -``` - -******* - -### 外键 - -```sql -字段 数据类型 references 主键表(关系列) -``` - -******** - -### 标识列 - -```sql -identity(1,1) -``` - -***** - -### 唯一约束 - -```sql -unique -``` - -******* - -### 非空 - -```sql -not null -``` - -***** - -### 默认 - -```sql -default('') -``` - -***** - -### 检查约束 - -```sql -check(表达式) -``` - -****** - -## 插入数据 - -```sql -insert 表名 (字段1,...默认约束不加字段)values () -``` - -****** - -## 删除数据 - -```sql -delete 表名 where 字段 = 内容 -``` - -****** - -## 更新数据 - -```sql -update 表名 set 字段名=值 where 条件 -``` - -****** - -## 简单查询 - -### 查询所以内容 - -```sql -select * from 表名 -``` - -******* - -### 查询前n条信息 - -```sql -select top n * from 表名 -``` - -********* - -### 查询前%的信息 - -```sql -select top n percent * from 表名 -``` - -******* - -## 单条件查询 - -### **SQL中常用运算符** - -```sql -=:等于,比较是否相等及赋值 -!=:比较不等于 ->:比较大于 -<:比较小于 ->=:比较大于等于 -<=:比较小于等于 -IS NULL:比较为空 -IS NOT NULL:比较不为空 -in:比较是否在其中 -like:模糊查询 -BETWEEN...AND...:比较是否在两者之间 50-100 BETWEEN 50 AND 100 -and:逻辑与(两个条件同时成立表达式成立) -or:逻辑或(两个条件有一个成立表达式成立) -not:逻辑非(条件成立,表达式则不成立;条件不成立,表达式则成立) -``` - -****** - -## 函数 - -### 常用时间函数 - -```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 -``` - -******* - -### round函数 - -```sql -round(num,len,[type]) -其中: -num表示需要处理的数字,len表示需要保留的长度,type处理类型(0是默认值代表四舍五入,非0代表直接截取) -select ROUND(123.45454,3) --123.45500 -select ROUND(123.45454,3,1) --123.45400 -``` - -如果不需要后面保留的0,使用CONVERT函数 - -```sql -CONVERT (要转换成的数据类型, 字段) -convert (decimal(12,2),avg(字段)) -``` - -********* - -## 多条件查询 - -### 消除重复行 - -```sql -select distinct 字段1,字段2 from 表名 -``` - -******* - -### 模糊查询 - -```sql -%:代表匹配0个字符、1个字符或多个字符。 -_:代表匹配有且只有1个字符。 -[]:代表匹配范围内 -[^]:代表匹配不在范围内 -``` - -****** - -### 聚合函数 - -```sql -count:求数量 -max:求最大值 -min:求最小值 -sum:求和 -avg:求平均值 -``` - -count(*)与count(字段)的区别 - -- count(*):不排除空值 -- count(字段):排除空值 - -******** - -## 排序查询 - -```sql -select * from 表名 order by 字段 asc/降序desc -(默认是升序可以不用写) -``` - -******* - -## 分组排序 - -聚合函数一般结合分组查询使用 - -```sql -select 字段,聚合函数() 自拟定列名 from 表名 -group by 字段(与上面字段相同) -[having]聚合函数() -``` - -******* - -## 等值链接 - -```sql -select 字段1,字段2(如果不是唯一字段加前缀) -from 表名1 as 别名1 , 表名2 as 别名2 -where 别名.字段=别名.字段 -``` - -******* - -## 内链接 - -```sql -select 表1别名.列1,表2别名.列2 from 表1 别名 -inner join 表2 别名 on 表1别名.列=表2别名.列 -inner join 表3 别名 on 表2别名.列=表3别名.列 -``` - -**内连接与等值连接区别:** - -- 等值连接是内连接的一个子集, inner join ... on 可以 不等值 - -- 内连接相对于等值连接运行效率快, 所以建议使用内连接。 - -******* - -### 左链接 - -```sql -select 表1别名.列1,表2别名.列2 from 表1 别名 -left join 表2 别名 on 表1别名.列=表2别名.列 -``` - -- let join关键字返回**左表**中的所有行,即使在右边表中没有匹配。(没有匹配返回的是空值) -- 右链接关键字换成right join - -********* - -自链接 - -同表不同名 - -```sql -select a.字段,b.字段 -from 表名1 别名a,表名1 别名b -where a.字段 = b.字段 -``` - -自连接查询就是以类似多表对比的方式,实现对同一张表内数据进行复杂的关系表示或关系处理。 \ No newline at end of file -- Gitee From d117a84016a4c850b788b812490d51ebb8356703 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B6=E5=B0=8F=E6=9D=B0?= <2365584215@qq.com> Date: Sun, 4 Sep 2022 13:19:55 +0000 Subject: [PATCH 7/8] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2013?= =?UTF-8?q?=20=E6=B4=AA=E7=81=BF=E8=8A=B3/=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../8.29 day1\344\275\234\344\270\232.md" | 54 ------------- .../8.30 day2 \344\275\234\344\270\232.md" | 78 ------------------- 2 files changed, 132 deletions(-) delete mode 100644 "13 \346\264\252\347\201\277\350\212\263/\344\275\234\344\270\232/8.29 day1\344\275\234\344\270\232.md" delete mode 100644 "13 \346\264\252\347\201\277\350\212\263/\344\275\234\344\270\232/8.30 day2 \344\275\234\344\270\232.md" diff --git "a/13 \346\264\252\347\201\277\350\212\263/\344\275\234\344\270\232/8.29 day1\344\275\234\344\270\232.md" "b/13 \346\264\252\347\201\277\350\212\263/\344\275\234\344\270\232/8.29 day1\344\275\234\344\270\232.md" deleted file mode 100644 index 53287ad..0000000 --- "a/13 \346\264\252\347\201\277\350\212\263/\344\275\234\344\270\232/8.29 day1\344\275\234\344\270\232.md" +++ /dev/null @@ -1,54 +0,0 @@ -# 8.29 day1作业 - -```sql -create database DBTEST -go -use DBTEST -go -create table sectionInfo( - sectionID int primary key identity(1,1), - sectionName varchar(10) not null -) -go -create table userinfo( - userNo int primary key identity(1,1), - userName varchar(10) unique not null check(len(userName)>4), - userSex varchar(2) not null check(userSex='男'or userSex='女'), - userAge int not null check(userAge>1 and userAge<=100), - userAddress varchar(50) default('湖北'), - userSection int references sectionInfo(sectionID) -) -go -create table workInfo( - workId int identity(1,1) primary key not null, - userId int references userinfo(userNo) not null, - workTime datetime not null, - workDescription varchar(40) check(workDescription='迟到' or workDescription='早退' or workDescription='旷工'or workDescription='病假' or workDescription='事假') -) -go -insert sectionInfo values - ('营销部'), - ('市场部'), - ('销售部'), - ('开发部'), - ('售后部') -go -insert userinfo values - ('aaaaa','女',20,'江西','1'), - ('bbbbb','女',20,'江西','1'), - ('ccccc','女',20,'江西','1'), - ('ddddd','女',20,'江西','1'), - ('eeeee','女',20,'江西','1') -go -insert workInfo values - (13,'2022/2/2','迟到'), - (14,'2022/2/2','迟到'), - (15,'2022/2/2','迟到'), - (16,'2022/2/2','迟到'), - (17,'2022/2/2','迟到') - -select * from sectionInfo -select * from userinfo -select * from workInfo -``` - diff --git "a/13 \346\264\252\347\201\277\350\212\263/\344\275\234\344\270\232/8.30 day2 \344\275\234\344\270\232.md" "b/13 \346\264\252\347\201\277\350\212\263/\344\275\234\344\270\232/8.30 day2 \344\275\234\344\270\232.md" deleted file mode 100644 index 1883762..0000000 --- "a/13 \346\264\252\347\201\277\350\212\263/\344\275\234\344\270\232/8.30 day2 \344\275\234\344\270\232.md" +++ /dev/null @@ -1,78 +0,0 @@ -# 8.30 day2 作业 - -```sql ---1. 查询所有行所有列 -select * from People ---2. 指定列查询(姓名,性别,月薪,电话) -select PeopleName,PeopleSex,PeopleSalary,PeoplePhone from People ---3. 指定列查询,并自定义中文列名(姓名,性别,月薪,电话) -select PeopleName 姓名,PeopleSex 性别,PeopleSalary 月薪,PeoplePhone 电话 from People ---4. 查询公司员工所在城市(不需要重复数据) -select distinct PeopleAddress from People ---5. 假设工资普调10%,查询原始工资和调整后的工资,显示(姓名,性别,月薪,加薪后的月薪)(添加列查询)。 -select PeopleName,PeopleSex,PeopleSalary,PeopleSalary*1.1 调整后 from People ---1. 根据指定列(姓名,性别,月薪,电话)查询性别为女的员工信息,并自定义中文列名 -select PeopleName,PeopleSex,PeopleSalary,PeoplePhone from People -where PeopleSex='女' ---2. 查询月薪大于等于10000 的员工信息( 单条件 ) -select PeopleName,PeopleSex,PeopleSalary,PeoplePhone from People -where PeopleSalary >10000 ---3. 查询月薪大于等于10000 的女员工信息(多条件) -select PeopleName,PeopleSex,PeopleSalary,PeoplePhone from People -where PeopleSex='女' and PeopleSalary >10000 ---4. 显示出出身年月在1980-1-1之后,而且月薪大于等于10000的女员工信息。 -select * from People -where PeopleSex='女' and PeopleSalary >10000 and PeopleBirth>1980-1-1 ---5. 显示出月薪大于等于15000 的员工,或者月薪大于等于8000的女员工信息。 -select * from People where PeopleSalary >= 15000 or PeopleSalary >8000 and PeopleSex='女' ---6. 查询月薪在10000-20000 之间员工信息( 多条件 ) -select * from people where PeopleSalary >=10000 and PeopleSalary <= 20000 ---7. 查询出地址在北京或者上海的员工信息 -select * from People where PeopleAddress ='北京' or PeopleAddress='上海' ---8. 查询所有员工信息(根据工资排序,降序排列) -select * from People -order by PeopleSalary desc ---9. 显示所有的员工信息,按照名字的长度进行倒序排列 -select * from People -order by len(PeopleName) desc ---10. 查询工资最高的5个人的信息 -select top 5 * from People -order by PeopleSalary desc ---11. 查询工资最高的10%的员工信息 - ---12. 查询出地址没有填写的员工信息 -select * from People where PeopleAddress is null ---13. 查询出地址已经填写的员工信息 -select * from People where PeopleAddress is not null ---14. 查询所有的80后员工信息 -select * from People -where PeopleBirth>1980-1-1 ---15. 查询年龄在30-40 之间,并且工资在15000-30000 之间的员工信息 -select * from people -where (YEAR(GETDATE())-YEAR(PeopleBirth)) between 30 and 40 ---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 > (select PeopleSalary from People where PeopleName = '赵云') ---18. 查询出和赵云在同一个城市的人 -select * from People where PeopleAddress = (select PeopleAddress from People where PeopleName = '赵云') ---19. 查询出生肖为鼠的人员信息 -select * from people where (year(PeopleBirth)%12)-3 =1 ---20. 查询所有员工信息,添加一列显示属相(鼠,牛,虎,兔,龙,蛇,马,羊,猴,鸡,狗,猪) -select *, - case - when (year(peoplebirth)%12)-3 =1 then '鼠' - when (year(peoplebirth)%12)-3 =2 then '牛' - when (year(peoplebirth)%12)-3 =3 then '虎' - when (year(peoplebirth)%12)-3 =4 then '兔' - when (year(peoplebirth)%12)-3 =5 then '龙' - when (year(peoplebirth)%12)-3 =6 then '蛇' - when (year(peoplebirth)%12)-3 =7 then '马' - when (year(peoplebirth)%12)-3 =8 then '羊' - when (year(peoplebirth)%12)-3 =9 then '猴' - when (year(peoplebirth)%12)-3 =10 then '鸡' - when (year(peoplebirth)%12)-3 =11 then '狗' - when (year(peoplebirth)%12)-3 =12 then '猪' - else '' end 生肖 from People -``` - -- Gitee From bccc2199c918777e264d7e3b8d687f5353dfd104 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B6=E5=B0=8F=E6=9D=B0?= <2365584215@qq.com> Date: Sun, 4 Sep 2022 22:43:24 +0800 Subject: [PATCH 8/8] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E7=AC=94?= =?UTF-8?q?=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\346\254\241\347\254\224\350\256\260.md" | 167 ++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 "39\345\217\266\345\260\217\346\235\260/sql\347\254\254\344\270\200\346\254\241\347\254\224\350\256\260.md" diff --git "a/39\345\217\266\345\260\217\346\235\260/sql\347\254\254\344\270\200\346\254\241\347\254\224\350\256\260.md" "b/39\345\217\266\345\260\217\346\235\260/sql\347\254\254\344\270\200\346\254\241\347\254\224\350\256\260.md" new file mode 100644 index 0000000..38f38c5 --- /dev/null +++ "b/39\345\217\266\345\260\217\346\235\260/sql\347\254\254\344\270\200\346\254\241\347\254\224\350\256\260.md" @@ -0,0 +1,167 @@ +### **SQL约束** + +NOT NULL约束 - 确保列不能具有NULL值。 + +默认值约束 - 在未指定列时为列提供默认值。 + +唯一约束 - 确保列中的所有值都不同。 + +主键 - 唯一标识数据库表中的每一行/记录。 + +外键 - 唯一标识任何其他数据库表中的行/记录。 + +检查约束 - CHECK约束确保列中的所有值都满足特定条件。 + +索引 - 用于非常快速地从数据库创建和检索数据。 + +## DROP TABLE + +`DROP TABLE语句永久删除表的数据和结构,某些数据库系统要求表中的记录必须为空时才能从数据库中删除。 这有助于防止意外删除仍在使用的表。` + +`要删除表中的所有数据,可以使用DELETE或TRUNCATE TABLE语句。` + +`要删除由另一个表的外键约束引用的表,必须在删除表之前禁用或删除外部约束` + +## TURNCATE TABLE + +**使用SQL `TRUNCATE TABLE`语句高效,快速地删除表中的所有数据** + +**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表示需要处理的数字,len表示需要保留的长度,type处理类型(0是默认值代表四舍五入,非0代表直接截取) +select ROUND(123.45454,3) --123.45500 +select ROUND(123.45454,3,1) --123.45400 +``` + + + +#### 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 或yy | +| 季度 | 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 | + +## ORDER BY排序 + +```sql +SELECT + column1, column2 +FROM + table_name +ORDER BY column1 ASC , + column2 DESC; + +``` + +在此语法中,`ORDER BY`子句放在`FROM`子句之后。 如果`SELECT`语句包含`WHERE`子句,则`ORDER BY`子句必须放在WHERE子句之后。 + +要对结果集进行排序,请指定要排序的列以及排序顺序的类型: + +- 升序(使用:`ASC`表示) +- 降序(使用:`DESC`表示) + + + +#### INNER JOIN + +**[内连接子句](https://blog.csdn.net/Pythboy/article/details/www.yiibai.com/sql/sql-left-join.html)消除了与另一个表的行不匹配的行。** + +```sql +SELECT + A.n +FROM A +INNER JOIN B ON B.n = A.n + +INNER JOIN子句可以连接三个或更多表,只要它们具有关系,通常是外键关系 +``` + + + + + + + + + + + -- Gitee