From eb18cf27ca6cc218e6ec9b79e6eb8ffe120b52be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=98=8A=E7=AB=A5?= <10033735+Harshreich@user.noreply.gitee.com> Date: Sun, 18 Sep 2022 14:15:22 +0000 Subject: [PATCH 1/4] 10 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 陈昊童 <10033735+Harshreich@user.noreply.gitee.com> --- .../22.9.18\346\237\245\350\257\242.md" | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 "\351\231\210\346\230\212\347\253\245/\344\275\234\344\270\232/22.9.18\346\237\245\350\257\242.md" diff --git "a/\351\231\210\346\230\212\347\253\245/\344\275\234\344\270\232/22.9.18\346\237\245\350\257\242.md" "b/\351\231\210\346\230\212\347\253\245/\344\275\234\344\270\232/22.9.18\346\237\245\350\257\242.md" new file mode 100644 index 0000000..210d681 --- /dev/null +++ "b/\351\231\210\346\230\212\347\253\245/\344\275\234\344\270\232/22.9.18\346\237\245\350\257\242.md" @@ -0,0 +1,85 @@ +主键:primary key + +自增:indentity(x,y)[从x开始每次增加y] + +非空:not null + +默认:default() + + check约束:chenk(条件) + +唯一:unique + +外键:(foreign key)references 表名(字段名) + +外键:Alter table 表名 add constraint 约束名 foreign key(字段名) references 主键表(字段) + +增加字段:Alter table 表名 add 字段名 条件 + + + +SQL语句执行顺序: + +1.FROM + +2.ON + +3. **JOIN** + +4.**WHERE** + +5.**GROUP** **BY** + +6. HAVING + +7.**SELECT** + +8.**DISTINCT** + +9.**ORDER** **BY** + +10.**LIMIT** + + + +聚合函数: + +1.count:求数量 + +2.**max**:求最大值 + +3.**min**:求最小值 + +4.**sum**:求和 + +5.**avg**:求平均值 + + + +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 \ No newline at end of file -- Gitee From a59a5cb82758740f2e244a7f3f87d35a21e2aac7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=98=8A=E7=AB=A5?= <10033735+Harshreich@user.noreply.gitee.com> Date: Sun, 18 Sep 2022 14:15:29 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20?= =?UTF-8?q?=E9=99=88=E6=98=8A=E7=AB=A5/=E4=BD=9C=E4=B8=9A/22.9.18=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../22.9.18\346\237\245\350\257\242.md" | 85 ------------------- 1 file changed, 85 deletions(-) delete mode 100644 "\351\231\210\346\230\212\347\253\245/\344\275\234\344\270\232/22.9.18\346\237\245\350\257\242.md" diff --git "a/\351\231\210\346\230\212\347\253\245/\344\275\234\344\270\232/22.9.18\346\237\245\350\257\242.md" "b/\351\231\210\346\230\212\347\253\245/\344\275\234\344\270\232/22.9.18\346\237\245\350\257\242.md" deleted file mode 100644 index 210d681..0000000 --- "a/\351\231\210\346\230\212\347\253\245/\344\275\234\344\270\232/22.9.18\346\237\245\350\257\242.md" +++ /dev/null @@ -1,85 +0,0 @@ -主键:primary key - -自增:indentity(x,y)[从x开始每次增加y] - -非空:not null - -默认:default() - - check约束:chenk(条件) - -唯一:unique - -外键:(foreign key)references 表名(字段名) - -外键:Alter table 表名 add constraint 约束名 foreign key(字段名) references 主键表(字段) - -增加字段:Alter table 表名 add 字段名 条件 - - - -SQL语句执行顺序: - -1.FROM - -2.ON - -3. **JOIN** - -4.**WHERE** - -5.**GROUP** **BY** - -6. HAVING - -7.**SELECT** - -8.**DISTINCT** - -9.**ORDER** **BY** - -10.**LIMIT** - - - -聚合函数: - -1.count:求数量 - -2.**max**:求最大值 - -3.**min**:求最小值 - -4.**sum**:求和 - -5.**avg**:求平均值 - - - -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 \ No newline at end of file -- Gitee From 6929cb77448529afb34c9cc62ebd44f9f9c04c5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=98=8A=E7=AB=A5?= <10033735+Harshreich@user.noreply.gitee.com> Date: Sun, 18 Sep 2022 14:15:41 +0000 Subject: [PATCH 3/4] =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 陈昊童 <10033735+Harshreich@user.noreply.gitee.com> --- .../\344\275\234\344\270\232/22.9.18sql.sql" | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 "\351\231\210\346\230\212\347\253\245/\344\275\234\344\270\232/22.9.18sql.sql" diff --git "a/\351\231\210\346\230\212\347\253\245/\344\275\234\344\270\232/22.9.18sql.sql" "b/\351\231\210\346\230\212\347\253\245/\344\275\234\344\270\232/22.9.18sql.sql" new file mode 100644 index 0000000..3ac935f --- /dev/null +++ "b/\351\231\210\346\230\212\347\253\245/\344\275\234\344\270\232/22.9.18sql.sql" @@ -0,0 +1,47 @@ +select * from People; +select * from Department; +select * from Rank; +--1. ѯ人еԱϢҪʾԼԱϸ + select p.*,DepartmentName from People p + inner join Department d on d.DepartmentId = p.DepartmentId + where p.PeopleAddress='人'; +--2. ѯ人еԱϢҪʾƣְԼԱϸ + select p.*,DepartmentName,RankName from People p + inner join Department d on d.DepartmentId = p.DepartmentId + inner join Rank r on r.RankId=p.RankId + where p.PeopleAddress='人'; +--3. ݲŷͳԱԱܺͣƽʣ߹ʺ͹ʡ +select DepartmentName ,count(*)Ա,sum(PeopleSalary)Աܺ,avg(PeopleSalary)ƽ,max(PeopleSalary)߹,min(PeopleSalary)͹ from People p +inner join Department d on d.DepartmentId = p.DepartmentId +group by DepartmentName; +--4. ݲŷͳԱԱܺͣƽʣ߹ʺ͹ʣƽ10000 µIJͳƣҸƽʽС +select DepartmentName ,count(*)Ա,sum(PeopleSalary)Աܺ,avg(PeopleSalary)ƽ,max(PeopleSalary)߹,min(PeopleSalary)͹ from People p +inner join Department d on d.DepartmentId = p.DepartmentId +group by DepartmentName +having avg(PeopleSalary)>=10000 +order by avg(PeopleSalary) asc; + +--5. ݲƣȻְλƣͳԱԱܺͣƽʣ߹ʺ͹ +select DepartmentName ,count(*)Ա,sum(PeopleSalary)Աܺ,avg(PeopleSalary)ƽ,max(PeopleSalary)߹,min(PeopleSalary)͹ from People p +inner join Department d on d.DepartmentId = p.DepartmentId +inner join Rank r on r.RankId=p.RankId +group by DepartmentName,RankName; +--6.ѯз 6.22--7.22 ԱϢ +select * from People +where (cast(MONTH(PeopleBirth) AS VARCHAR(10)) + cast((CASE WHEN DAY(PeopleBirth) < 10 THEN '0' + cast(DAY(PeopleBirth) AS VARCHAR(10)) ELSE cast(DAY(PeopleBirth) AS VARCHAR(10)) END) AS VARCHAR(10)))>=622 +and(cast(MONTH(PeopleBirth) AS VARCHAR(10)) + cast((CASE WHEN DAY(PeopleBirth) < 10 THEN '0' + cast(DAY(PeopleBirth) AS VARCHAR(10)) ELSE cast(DAY(PeopleBirth) AS VARCHAR(10)) END) AS VARCHAR(10)))<=722; +--7.ѯԱϢһʾ(,ţ,,,,,,,,,,) +select *,case +when year(PeopleBirth)%12=0 then '' +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 '' end + from People; \ No newline at end of file -- Gitee From 873e14044f3e9183cc5013c2733dfd6007b524df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=98=8A=E7=AB=A5?= <10033735+Harshreich@user.noreply.gitee.com> Date: Sun, 18 Sep 2022 14:15:59 +0000 Subject: [PATCH 4/4] =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 陈昊童 <10033735+Harshreich@user.noreply.gitee.com> --- .../22.9.18\346\237\245\350\257\242.md" | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 "\351\231\210\346\230\212\347\253\245/\347\254\224\350\256\260/22.9.18\346\237\245\350\257\242.md" diff --git "a/\351\231\210\346\230\212\347\253\245/\347\254\224\350\256\260/22.9.18\346\237\245\350\257\242.md" "b/\351\231\210\346\230\212\347\253\245/\347\254\224\350\256\260/22.9.18\346\237\245\350\257\242.md" new file mode 100644 index 0000000..210d681 --- /dev/null +++ "b/\351\231\210\346\230\212\347\253\245/\347\254\224\350\256\260/22.9.18\346\237\245\350\257\242.md" @@ -0,0 +1,85 @@ +主键:primary key + +自增:indentity(x,y)[从x开始每次增加y] + +非空:not null + +默认:default() + + check约束:chenk(条件) + +唯一:unique + +外键:(foreign key)references 表名(字段名) + +外键:Alter table 表名 add constraint 约束名 foreign key(字段名) references 主键表(字段) + +增加字段:Alter table 表名 add 字段名 条件 + + + +SQL语句执行顺序: + +1.FROM + +2.ON + +3. **JOIN** + +4.**WHERE** + +5.**GROUP** **BY** + +6. HAVING + +7.**SELECT** + +8.**DISTINCT** + +9.**ORDER** **BY** + +10.**LIMIT** + + + +聚合函数: + +1.count:求数量 + +2.**max**:求最大值 + +3.**min**:求最小值 + +4.**sum**:求和 + +5.**avg**:求平均值 + + + +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 \ No newline at end of file -- Gitee