From 2bf73eb3f5c27d08fd7400fb01c2a80059e79612 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BB=96=E6=9F=8F=E6=88=90?= <2771940338@qq.com> Date: Mon, 19 Sep 2022 10:44:32 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\344\275\234\344\270\232/2022-9-18.sql" | 65 +++++++++++++++++++ ...7\347\254\246\344\275\277\347\224\250.txt" | 59 +++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 "08\345\273\226\346\237\217\346\210\220--\347\254\254\344\272\214\346\254\241/\344\275\234\344\270\232/2022-9-18.sql" create mode 100644 "08\345\273\226\346\237\217\346\210\220--\347\254\254\344\272\214\346\254\241/\347\254\224\350\256\260/2022-9-18 sql\350\277\220\347\256\227\347\254\246\344\275\277\347\224\250.txt" diff --git "a/08\345\273\226\346\237\217\346\210\220--\347\254\254\344\272\214\346\254\241/\344\275\234\344\270\232/2022-9-18.sql" "b/08\345\273\226\346\237\217\346\210\220--\347\254\254\344\272\214\346\254\241/\344\275\234\344\270\232/2022-9-18.sql" new file mode 100644 index 0000000..2ae050a --- /dev/null +++ "b/08\345\273\226\346\237\217\346\210\220--\347\254\254\344\272\214\346\254\241/\344\275\234\344\270\232/2022-9-18.sql" @@ -0,0 +1,65 @@ +--1. 查询出武汉地区所有的员工信息,要求显示部门名称以及员工的详细资料 + select * from People p + left join Department d + on d.DepartmentId=p.DepartmentId + where PeopleAddress='武汉'; + +--2. 查询出武汉地区所有的员工信息,要求显示部门名称,职级名称以及员工的详细资料 + select * from People p + left join Department d on d.DepartmentId=p.DepartmentId + inner join Rank r on r.RankId=p.RankId + where PeopleAddress='武汉'; + +--3. 根据部门分组统计员工人数,员工工资总和,平均工资,最高工资和最低工资。 + select DepartmentName,COUNT(p.DepartmentId) 员工人数,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 以下的不参与统计,并且根据平均工资降序排列。 + select DepartmentName,COUNT(p.DepartmentId) 员工人数,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 DepartmentName,RankId,COUNT(People.DepartmentId)人数,SUM(PeopleSalary)工资总和,AVG(PeopleSalary)平均工资,MAX(PeopleSalary)最高工资,MIN(PeopleSalary)最低工资 from People + inner join Department on People.DepartmentId=Department.DepartmentId + group by RankId,DepartmentName; + +--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 '马' + when YEAR(PeopleBirth)%12=11 + then '羊' + end 生肖 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) + diff --git "a/08\345\273\226\346\237\217\346\210\220--\347\254\254\344\272\214\346\254\241/\347\254\224\350\256\260/2022-9-18 sql\350\277\220\347\256\227\347\254\246\344\275\277\347\224\250.txt" "b/08\345\273\226\346\237\217\346\210\220--\347\254\254\344\272\214\346\254\241/\347\254\224\350\256\260/2022-9-18 sql\350\277\220\347\256\227\347\254\246\344\275\277\347\224\250.txt" new file mode 100644 index 0000000..cf0be2c --- /dev/null +++ "b/08\345\273\226\346\237\217\346\210\220--\347\254\254\344\272\214\346\254\241/\347\254\224\350\256\260/2022-9-18 sql\350\277\220\347\256\227\347\254\246\344\275\277\347\224\250.txt" @@ -0,0 +1,59 @@ +**SQL中常用运算符** + +```sql +=:等于,比较是否相等及赋值 +!=:比较不等于 +>:比较大于 +<:比较小于 +>=:比较大于等于 +<=:比较小于等于 +IS NULL:比较为空 +IS NOT NULL:比较不为空 +in:比较是否在其中 +like:模糊查询 +BETWEEN...AND...:比较是否在两者之间 50-100 BETWEEN 50 AND 100 +and:逻辑与(两个条件同时成立表达式成立) +or:逻辑或(两个条件有一个成立表达式成立) +not:逻辑非(条件成立,表达式则不成立;条件不成立,表达式则成立) + + +%:代表匹配0个字符、1个字符或多个字符。 +_:代表匹配有且只有1个字符。 +[]:代表匹配范围内 +[^]:代表匹配不在范围内 + + +聚合函数 +count:求数量 +max:求最大值 +min:求最小值 +sum:求和 +avg:求平均值 + + +round(num,len,[type]) +其中: +num表示需要处理的数字,len表示需要保留的长度,type处理类型(0是默认值代表四舍五入,非0代表直接截取) +select ROUND(123.45454,3) --123.45500 +select ROUND(123.45454,3,1) --123.45400 + +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()); --返回当前日期年数 + +语句执行顺序: +(7) SELECT +(8) DISTINCT +(1) FROM +(3) JOIN +(2) ON +(4) WHERE +(5) GROUP BY +(6) HAVING +(9) ORDER BY +(10) LIMIT \ No newline at end of file -- Gitee