From 8acefe363c511800115e05e8370c84ae689c6aae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=B7=91=E8=8A=B3?= <2025575070@qq.com> Date: Sun, 18 Sep 2022 19:42:41 +0800 Subject: [PATCH] 9.16 --- .../9-16\344\275\234\344\270\232.sql" | 67 ++++++++++++++ ...4\350\256\260-\346\237\245\350\257\242.md" | 43 ++++++++- .../\344\275\234\344\270\232.sql" | 88 ------------------- 3 files changed, 109 insertions(+), 89 deletions(-) create mode 100644 "24\345\274\240\346\267\221\350\212\263/9-16\344\275\234\344\270\232.sql" rename "\345\274\240\346\267\221\350\212\263/\347\254\224\350\256\260.md" => "24\345\274\240\346\267\221\350\212\263/9-16\347\254\224\350\256\260-\346\237\245\350\257\242.md" (56%) delete mode 100644 "\345\274\240\346\267\221\350\212\263/\344\275\234\344\270\232.sql" diff --git "a/24\345\274\240\346\267\221\350\212\263/9-16\344\275\234\344\270\232.sql" "b/24\345\274\240\346\267\221\350\212\263/9-16\344\275\234\344\270\232.sql" new file mode 100644 index 0000000..947b800 --- /dev/null +++ "b/24\345\274\240\346\267\221\350\212\263/9-16\344\275\234\344\270\232.sql" @@ -0,0 +1,67 @@ + + +use DBTEST1; + +--1. 查询出武汉地区所有的员工信息,要求显示部门名称以及员工的详细资料 +select p.*,d.DepartmentName from People p +join Department d on d.DepartmentId = p.DepartmentId +where PeopleAddress = '武汉'; + + + + +--2. 查询出武汉地区所有的员工信息,要求显示部门名称,职级名称以及员工的详细资料 +select p.*,d.DepartmentName,r.RankName from People p +join Department d on d.DepartmentId = p.DepartmentId +join Rank r on r.RankId = p.RankId +where PeopleAddress = '武汉'; + + +--3. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资。 + +select count(PeopleId)员工人数,SUM(PeopleSalary)工资总和,AVG(PeopleSalary)平均工资,MAX(PeopleSalary)最高工资,MIN(PeopleSalary)最低工资 from People p +join Department d on d.DepartmentId = p.DepartmentId +group by d.DepartmentId; + + + +--4. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资,平均工资在10000 以下的不参与统计,并且根据平均工资降序排列。 +select count(PeopleId)员工人数,SUM(PeopleSalary)工资总和,MAX(PeopleSalary)最高工资,MIN(PeopleSalary)最低工资 from People p +join Department d on d.DepartmentId = p.DepartmentId +group by d.DepartmentId +having avg(PeopleSalary) >= 10000 +order by avg(PeopleSalary) desc; + + +--5. 根据部门名称,然后根据职位名称,分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资 +select d.DepartmentName,r.RankName,count(PeopleId)员工人数,SUM(PeopleSalary)工资总和,AVG(PeopleSalary)平均工资,MAX(PeopleSalary)最高工资,MIN(PeopleSalary)最低工资 from People p +join Department d on d.DepartmentId = p.DepartmentId +join Rank r on r.RankId = p.RankId +group by d.DepartmentName,r.RankName; + + +--16.**查询出巨蟹 6.22--7.22 的员工信息** +select * from People +where CONCAT(RIGHT(month(PeopleBirth)+100,2),RIGHT(day(PeopleBirth)+100,2)) > '0622' +and CONCAT(RIGHT(month(PeopleBirth)+100,2),RIGHT(day(PeopleBirth)+100,2)) < '0722' + + +--20.查询所有员工信息,添加一列显示属相(鼠,牛,虎,兔,龙,蛇,马,羊,猴,鸡,狗,猪) +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 '狗' + else '猪' + end +)属相 +from People; \ No newline at end of file diff --git "a/\345\274\240\346\267\221\350\212\263/\347\254\224\350\256\260.md" "b/24\345\274\240\346\267\221\350\212\263/9-16\347\254\224\350\256\260-\346\237\245\350\257\242.md" similarity index 56% rename from "\345\274\240\346\267\221\350\212\263/\347\254\224\350\256\260.md" rename to "24\345\274\240\346\267\221\350\212\263/9-16\347\254\224\350\256\260-\346\237\245\350\257\242.md" index bbee2b0..c2fdea3 100644 --- "a/\345\274\240\346\267\221\350\212\263/\347\254\224\350\256\260.md" +++ "b/24\345\274\240\346\267\221\350\212\263/9-16\347\254\224\350\256\260-\346\237\245\350\257\242.md" @@ -42,4 +42,45 @@ drop table 琛ㄥ悕; 妫鏌ョ害鏉 check() -澶栭敭 references \ No newline at end of file +澶栭敭 references + + + +### --9.16 + +涓嶉噸澶 distinct + +鎺掑簭 order by (desc 闄嶅簭 asc 鍗囧簭) + +**count()** + +count(*):绌哄间篃缁熻 + +count(瀛楁)锛氬綋鍓嶅瓧娈电殑绌哄间笉缁熻 + +**淇濈暀灏忔暟鐐瑰悗鍑犱綅** + +```sql +round(灏忔暟,淇濈暀浣嶆暟); +decimal(5,2); +``` + +**寮哄埗杞崲** + +```sql +CONVERT(VARCHAR(19),GETDATE()); +CAST('9.0' AS decimal); +``` + +**鍒犻櫎** +drop 鍒犻櫎鏁村紶琛 +truncate鍒犻櫎鏁村紶琛紝琛ㄨ繕鍦 + + + + + + + + + diff --git "a/\345\274\240\346\267\221\350\212\263/\344\275\234\344\270\232.sql" "b/\345\274\240\346\267\221\350\212\263/\344\275\234\344\270\232.sql" deleted file mode 100644 index 60fb8b1..0000000 --- "a/\345\274\240\346\267\221\350\212\263/\344\275\234\344\270\232.sql" +++ /dev/null @@ -1,88 +0,0 @@ - - -- 创建数据库 DBTEST - create database DBTEST; - - use DBTEST; - - /* 创建表 - 部门信息表(sectionInfo) - 部门编号 sectionID int 标识列 主键 - 部门名称 sectionName varchar(10) 不能为空 */ - - create table sectionInfo( - sectionID int identity(0,1) primary key, - sectionName varchar(10) not null, - ); - - --添加5条测试数据 - insert into sectionInfo(sectionName) values - ('开发部'), - ('测试部'), - ('美工部'), - ('狗策划部'), - ('宣发部'); - - select * from sectionInfo; - - /* - 员工信息表(userInfo) - 员工编号 userNo int 标识列 主键 不允许为空 - 员工姓名 userName varchar(10) 唯一约束 不允许为空 长度必须大于4 - 员工性别 userSex varchar(2) 不允许为空 只能是男或女 - 员工年龄 userAge int 不能为空 范围在1-100之间 - 员工地址 userAddress varchar(50) 默认值为“湖北” - 员工部门 userSection int 外键,引用部门信息表的部门编号*/ - drop table userInfo; - - create table userInfo( - userNo int identity(0,1) primary key not null, - userName varchar(10) unique not null check(len(userName) > 2), - 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) - ); - - --添加5条测试数据 - insert into userInfo(userName,userSex,userAge,userAddress,userSection) values - ('李铁梅','女',25,'上海',0), - ('王小明','男',25,'湖北',1), - ('王大锤','女',25,'上海',2), - ('眼睛小哥','男',38,'火星',3), - ('乔鲁偌','男',15,'霓虹',4); - - select * from sectionInfo; - - - /* - 员工考勤表(workInfo) - 考勤编号 workId int 标识列 主键 不能为空 - 考勤员工 userId int 外键 引用员工信息表的员工编号 不能为空 - 考勤时间 workTime datetime 不能为空 - 考勤说明 workDescription varchar(40) 不能为空 内容只能是“迟到”,“早退”,“旷工”,“病假”,“事假”中的一种 */ - drop table workInfo; - - create table workInfo( - workId int identity(0,1) primary key 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 = '事假') - ); - - --添加5条测试数据 - insert into workInfo(userId,workTime,workDescription) values - (0,'2022-01-05 15:25:46','迟到'), - (1,'2022-02-15 16:44:44','早退'), - (2,'2022-03-29 12:25:46','旷工'), - (3,'2022-04-25 08:45:46','病假'), - (4,'2022-06-18 19:25:46','事假'); - - select * from workInfo; - - - - - - - - -- Gitee