From ac3a4746b5d3412b963dce6f784a30373d766981 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=98=8E=E6=9D=B0?= <3090356592@qq.com> Date: Wed, 31 Aug 2022 13:24:39 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= 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" | 42 ++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 "22-08-29\345\273\272\350\241\250\347\273\203\344\271\240.md" create mode 100644 "22-08-30-\346\237\245\350\257\242\347\273\203\344\271\240.md" diff --git "a/22-08-29\345\273\272\350\241\250\347\273\203\344\271\240.md" "b/22-08-29\345\273\272\350\241\250\347\273\203\344\271\240.md" new file mode 100644 index 0000000..fb5dd34 --- /dev/null +++ "b/22-08-29\345\273\272\350\241\250\347\273\203\344\271\240.md" @@ -0,0 +1,65 @@ +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/22-08-30-\346\237\245\350\257\242\347\273\203\344\271\240.md" "b/22-08-30-\346\237\245\350\257\242\347\273\203\344\271\240.md" new file mode 100644 index 0000000..4b6f289 --- /dev/null +++ "b/22-08-30-\346\237\245\350\257\242\347\273\203\344\271\240.md" @@ -0,0 +1,42 @@ +--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 + + -- Gitee From cfe58d99c4265fd929896f1400a89dad4062cce7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=98=8E=E6=9D=B0?= <3090356592@qq.com> Date: Wed, 31 Aug 2022 09:36:37 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20=E7=BB=83=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\347\273\203\344\271\240/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "\347\273\203\344\271\240/.keep" diff --git "a/\347\273\203\344\271\240/.keep" "b/\347\273\203\344\271\240/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From dbf930595e9e29800e023feb5ec8692ea92170ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=98=8E=E6=9D=B0?= <3090356592@qq.com> Date: Wed, 31 Aug 2022 20:40:52 +0800 Subject: [PATCH 3/3] 8.31 --- ...72\350\241\250\347\273\203\344\271\240.md" | 0 ...45\350\257\242\347\273\203\344\271\240.md" | 5 +- ...74\345\220\210\347\273\203\344\271\240.md" | 32 ++ .../sql\345\244\215\344\271\240.md" | 283 ++++++++++++++++++ "\347\273\203\344\271\240/.keep" | 0 5 files changed, 318 insertions(+), 2 deletions(-) rename "22-08-29\345\273\272\350\241\250\347\273\203\344\271\240.md" => "15\345\220\264\346\230\216\346\235\260/220829\345\273\272\350\241\250\347\273\203\344\271\240.md" (100%) rename "22-08-30-\346\237\245\350\257\242\347\273\203\344\271\240.md" => "15\345\220\264\346\230\216\346\235\260/220830-\346\237\245\350\257\242\347\273\203\344\271\240.md" (99%) create 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" create 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" delete mode 100644 "\347\273\203\344\271\240/.keep" diff --git "a/22-08-29\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" similarity index 100% rename from "22-08-29\345\273\272\350\241\250\347\273\203\344\271\240.md" rename to "15\345\220\264\346\230\216\346\235\260/220829\345\273\272\350\241\250\347\273\203\344\271\240.md" diff --git "a/22-08-30-\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" similarity index 99% rename from "22-08-30-\346\237\245\350\257\242\347\273\203\344\271\240.md" rename to "15\345\220\264\346\230\216\346\235\260/220830-\346\237\245\350\257\242\347\273\203\344\271\240.md" index 4b6f289..f5641f2 100644 --- "a/22-08-30-\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" @@ -13,7 +13,6 @@ select * from People where PeopleSalary >( select PeopleSalary from People where PeopleName='赵云' ) - --18. 查询出和赵云在同一个城市的人 select * from People where PeopleAddress in( select PeopleAddress from People where PeopleName='赵云' @@ -21,6 +20,8 @@ select PeopleAddress from People where PeopleName='赵云' --19. 查询出生肖为鼠的人员信息 select * from People where year(PeopleBirth) % 12 = 4 --20. 查询所有员工信息,添加一列显示属相(鼠,牛,虎,兔,龙,蛇,马,羊,猴,鸡,狗,猪) + +``` select *, case year(PeopleBirth) % 12 when 4 then '鼠' @@ -38,5 +39,5 @@ case year(PeopleBirth) % 12 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" new file mode 100644 index 0000000..e0bcae5 --- /dev/null +++ "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" @@ -0,0 +1,32 @@ +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" new file mode 100644 index 0000000..6a157ee --- /dev/null +++ "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" @@ -0,0 +1,283 @@ + + +## 创表约束 + +### 主键 + +```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 diff --git "a/\347\273\203\344\271\240/.keep" "b/\347\273\203\344\271\240/.keep" deleted file mode 100644 index e69de29..0000000 -- Gitee