diff --git "a/20 \345\206\257\351\224\220/20230222 SELECT\350\257\255\345\217\245\345\237\272\346\234\254\347\273\203\344\271\240.md" "b/20 \345\206\257\351\224\220/20230222 SELECT\350\257\255\345\217\245\345\237\272\346\234\254\347\273\203\344\271\240.md" new file mode 100644 index 0000000000000000000000000000000000000000..d984755079e9b185651e121b88f6891626545ac5 --- /dev/null +++ "b/20 \345\206\257\351\224\220/20230222 SELECT\350\257\255\345\217\245\345\237\272\346\234\254\347\273\203\344\271\240.md" @@ -0,0 +1,224 @@ +# 第1组:user_profile表 + +user_profile表的sql脚本: + +```mysql +drop table if exists user_profile; +CREATE TABLE `user_profile` ( +`id` int, +`device_id` int, +`gender` varchar(14), +`age` int , +`university` varchar(32), +`province` varchar(32)); + + +INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学','BeiJing'); +INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai'); +INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing'); +INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang'); +INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong'); +``` + +![](5、SELECT语句基础练习.assets/image-20220207141745581 - 副本.png) + +解释:id(编号)、device_id(设备ID),gender(性别),age(年龄)、university(大学名称),province(省份) + +```mysql +CREATE DATABASE class3 charset utf8; +use class3; +CREATE TABLE user_profile ( +`id` int, +`device_id` int, +`gender` varchar(14), +`age` int , +`university` varchar(32), +`province` varchar(32) +); +INSERT into user_profile VALUES(1,2138,'male',21,'北京大学','BeiJing'); +INSERT into user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai'); +INSERT into user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing'); +INSERT into user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang'); +INSERT into user_profile VALUES(5,5432,'male',25,'山东大学','Shandong'); +``` + +## (1)题目:从用户信息表中取出学校的去重数据 + +现在运营需要查看用户来自于哪些学校,请从用户信息表中取出学校的去重数据。 + +![image-20220207141745581](5、SELECT语句基础练习.assets/image-20220207141745581.png) + +```mysql +SELECT DISTINCT university FROM user_profile ; +``` + +## (2)题目:查看用户明细设备ID数据,并将列名显示为 'user_infos_example' + +现在你需要查看用户明细设备ID数据,并将列名显示为 'user_infos_example',请你从用户信息表取出相应结果。 + +![image-20220207143458749](5、SELECT语句基础练习.assets/image-20220207143458749.png) + +```mysql +SELECT device_id FROM user_profile ; +``` + +## (3)题目:查询university是北京大学的设备ID + +现在运营想要筛选出所有北京大学的学生进行用户调研,请你从用户信息表中取出满足条件的数据,结果返回设备id和学校。 + +![image-20220207144253185](5、SELECT语句基础练习.assets/image-20220207144253185.png) + +```mysql +SELECT device_id , university FROM user_profile WHERE university ='北京大学'; +``` + +## (4)题目:查询年龄大于24用户的设备ID、性别、年龄、学校 + +现在运营想要针对24岁以上的用户开展分析,请你取出满足条件的设备ID、性别、年龄、学校。 + +![image-20220207144628582](5、SELECT语句基础练习.assets/image-20220207144628582.png) + +```mysql +SELECT device_id , gender , age , university FROM user_profile WHERE age>24; +``` + +## (5)题目:查询所有用户的设备id、性别、年龄、学校 + +现在运营同学想要用户的设备id对应的性别、年龄和学校的数据,请你取出相应数据 + +![image-20220207172816753](5、SELECT语句基础练习.assets/image-20220207172816753.png) + +```mysql +SELECT device_id , gender , age , university FROM user_profile; +``` + +## (6)题目:查询所有用户的数据 + +现在运营想要查看用户信息表中所有的数据,请你取出相应结果 + +![image-20220207172840781](5、SELECT语句基础练习.assets/image-20220207172840781.png) + +```mysql +SELECT * FROM user_profile; +``` + +## (7)题目:查询省份是"shanghai"的用户信息 + +现在运营想要查看上海市用户的信息,请你取出相应的结果。 + +根据示例,你的查询应返回以下结果: + +![image-20220210171353387](5、SELECT语句基础练习.assets/image-20220210171353387.png) + +```mysql +SELECT id ,device_id , gender , age , university , province FROM user_profile WHERE province ='shanghai'; +``` + +## (8)题目:查询所有男性用户的设备ID、年龄、学校 + +现在运营想要查看所有男性用户的设备ID、年龄、学校,便于后期做数据分析。 + +根据示例,你的查询应返回如下结果: + +![image-20220210171628263](5、SELECT语句基础练习.assets/image-20220210171628263.png) + +```mysql +SELECT device_id , age , university FROM user_profile WHERE gender = 'male'; +``` + +## (9)题目:从用户信息表中取出省份的去重数据 + +现在运营想要查看所有用户的省份分布情况,请从用户信息表中取出省份的去重数据。 + +根据示例,你的查询应返回如下结果: + +![image-20220210171835938](5、SELECT语句基础练习.assets/image-20220210171835938.png) + +```mysql + +``` + +第8章 DQL + +因为查询语句使用的非常的频繁,所以很多人把查询语句单拎出来一类,DQL(数据查询语言),DR(获取)L。 + +## 8.1 SELECT语句 + +SELECT语句是用于查看计算结果、或者查看从数据表中筛选出的数据的。 + +SELECT语句的基本语法: + + SELECT 常量; + SELECT 表达式; + SELECT 函数; + +例如: + + SELECT 1; + SELECT 9/2; + SELECT NOW(); + +如果要从数据表中筛选数据,需要加FROM子句。FROM指定数据来源。字段列表筛选列。 + + SELECT 字段列表 FROM 表名称; + +如果要从数据表中根据条件筛选数据,需要加FROM和WHERE子句。WHERE筛选行。 + + SELECT 字段列表 FROM 表名称 WHERE 条件; + +完整的SELECT语句后面可以跟7个子句,后面会逐一讲解。 + +## 8.2 使用别名 + +在当前select语句中给某个字段或表达式计算结果,或表等取个临时名称,便于当前select语句的编写和理解。这个临时名称称为别名。 + +```mysql +select 字段名1 as "别名1", 字段名2 as "别名2" from 表名称 as 别名; +``` + +* 列的别名有空格时,请加双引号。**==列的别名==中没有空格时,双引号可以加也可以不加**。 + +* ==**表的别名不能加双引号**==,表的别名中间不能包含空格。 + +* as大小写都可以,as也完全可以省略。 + + ```mysql + mysql> select * from student; + +------+------+ + | id | name | + +------+------+ + | 1 | 张三 | + | 2 | 李四 | + +------+------+ + 2 rows in set (0.00 sec) + + mysql> select id "学号",name "姓名" from student; + +------+------+ + | 学号 | 姓名 | + +------+------+ + | 1 | 张三 | + | 2 | 李四 | + +------+------+ + 2 rows in set (0.00 sec) + + mysql> select id 学号,name 姓名 from student; + +------+------+ + | 学号 | 姓名 | + +------+------+ + | 1 | 张三 | + | 2 | 李四 | + +------+------+ + 2 rows in set (0.00 sec) + + mysql> select id 学 号,name 姓 名 from student; + ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use n + ear '号,name 姓 名 from student' at line 1 + ``` + +## 8.3 结果去重 + +mysql可以在查询结果中使用distinct关键字去重。 + + select distinct 字段列表 from 表名称 【where 条件】; + + select distinct did from t_employee; diff --git "a/20 \345\206\257\351\224\220/20230222 \350\277\220\347\256\227\347\254\246\347\273\203\344\271\240.md" "b/20 \345\206\257\351\224\220/20230222 \350\277\220\347\256\227\347\254\246\347\273\203\344\271\240.md" new file mode 100644 index 0000000000000000000000000000000000000000..d35e5a1af495fd528de1504d0c83859a20027c9e --- /dev/null +++ "b/20 \345\206\257\351\224\220/20230222 \350\277\220\347\256\227\347\254\246\347\273\203\344\271\240.md" @@ -0,0 +1,448 @@ +# 第1题:user_profile表脚本1 + +user_profile表的sql脚本: + +```mysql +drop table if exists user_profile; +CREATE TABLE `user_profile` ( +`id` int, +`device_id` int, +`gender` varchar(14), +`age` int , +`university` varchar(32), +`province` varchar(32)); + +INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学','BeiJing'); +INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai'); +INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing'); +INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang'); +INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong'); +``` + +![](6、运算符练习.assets/image-20220207141745581 - 副本.png) + +解释:id(编号)、device_id(设备ID),gender(性别),age(年龄)、university(大学名称),province(省份) + +```mysql +CREATE DATABASE djj CHARSET utf8; +use djj ; +CREATE TABLE user_profile ( +`id` int, +`device_id` int, +`gender` varchar(14), +`age` int , +`university` varchar(32), +`province` varchar(32) +); +INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学','BeiJing'); +INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学','Shanghai'); +INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学','BeiJing'); +INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学','ZheJiang'); +INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学','Shandong'); +SELECT * FROM user_profile; +``` + + + +## (1)题目:查询年龄20岁及以上且23岁及以下用户的设备ID、性别、年龄 + +现在运营想要针对20岁及以上且23岁及以下的用户开展分析,请你取出满足条件的设备ID、性别、年龄。 + +![image-20220207144855013](6、运算符练习.assets/image-20220207144855013.png) + +```mysql +SELECT id,gender,age FROM user_profile WHERE age>=20; +``` + +## (2)题目:查询除复旦大学以外的所有用户的设备ID、性别、年龄、大学 + +现在运营想要查看除复旦大学以外的所有用户明细,请你取出相应数据 + +![image-20220207144942510](6、运算符练习.assets/image-20220207144942510.png) + +```mysql +SELECT id,gender,age,university FROM user_profile WHERE university not in ('复旦大学'); +``` + +## (3)题目:查询年龄不为空的用户的设备ID,性别,年龄,学校的信息 + +现在运营想要对用户的年龄分布开展分析,在分析时想要剔除没有获取到年龄的用户,请你取出所有年龄值不为空的用户的设备ID,性别,年龄,学校的信息。 + +![image-20220207145017152](6、运算符练习.assets/image-20220207145017152.png) + +```mysql +-- SELECT id,gender,age,university FROM user_profile WHERE age is null ;查询为空的代码 +SELECT id,gender,age,university FROM user_profile WHERE age is not null ; +``` + +# 第2题:user_profile表脚本2 + +user_profile表sql脚本 + +```mysql +drop table if exists user_profile; +CREATE TABLE `user_profile` ( +`id` int , +`device_id` int, +`gender` varchar(14), +`age` int , +`university` varchar(32), +`gpa` float, +`active_days_within_30` float, +`question_cnt` float, +`answer_cnt` float +); + +INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12); +INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25); +INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30); +INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2); +INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70); +INSERT INTO user_profile VALUES(6,2131,'male',28,'北京师范大学',3.3,15,7,13); +``` + +![image-20220207145932876](6、运算符练习.assets/image-20220207145932876.png) + +解释:id(编号)、device_id(设备ID),gender(性别),age(年龄)、university(大学名称),province(省份),gpa(平均成绩)、active_days_within_30(30天内活跃天数)、question_cnt(发帖数量)、answer_cnt(回答数量) + +例如:第一行表示:id为1的用户的常用信息为使用的设备id为2138,性别为男,年龄21岁,北京大学,gpa为3.4,在过去的30天里面活跃了7天,发帖数量为2,回答数量为12 + +```mysql +CREATE DATABASE dpp CHARSET utf8; +use dpp ; +CREATE TABLE `user_profile` ( +`id` int , +`device_id` int, +`gender` varchar(14), +`age` int , +`university` varchar(32), +`gpa` float, +`active_days_within_30` float, +`question_cnt` float, +`answer_cnt` float +); +INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12); +INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25); +INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30); +INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2); +INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70); +INSERT INTO user_profile VALUES(6,2131,'male',28,'北京师范大学',3.3,15,7,13); +SELECT * FROM user_profile ; +``` + + + +## (4)题目:查询男性且GPA在3.5以上(不包括3.5)的用户的设备ID,性别、年龄、学校、gpa + +现在运营想要找到男性且GPA在3.5以上(不包括3.5)的用户进行调研,请你取出相关数据。 + +![image-20220207145044808](6、运算符练习.assets/image-20220207145044808.png) + +```mysql +SELECT id,gender,age,university,gpa FROM user_profile WHERE gpa>3.5; +``` + +## (5)题目:查询学校为北大或GPA在3.7以上(不包括3.7)的用户的设备ID,性别、年龄、学校、gpa + +现在运营想要找到学校为北大或GPA在3.7以上(不包括3.7)的用户进行调研,请你取出相关数据(使用OR实现) + +![image-20220207145119374](6、运算符练习.assets/image-20220207145119374.png) + +```mysql +SELECT id,gender,age,university,gpa FROM user_profile WHERE gpa>3.7; +``` + +## (6)题目:查询学校为北大、复旦和山大用户的设备ID,性别、年龄、学校、gpa + +现在运营想要找到学校为北大、复旦和山大的同学进行调研,请你取出相关数据。 + +![image-20220207145152255](6、运算符练习.assets/image-20220207145152255.png) + +```mysql +SELECT id,gender,age,university,gpa FROM user_profile WHERE university in ('北京大学','复旦大学','山东大学'); +``` + +## (7)题目:查询gpa在3.5以上(不包括3.5)的山东大学用户 或 gpa在3.8以上(不包括3.8)的复旦大学同学 + +现在运营想要找到gpa在3.5以上(不包括3.5)的山东大学用户 或 gpa在3.8以上(不包括3.8)的复旦大学同学进行用户调研,请你取出相应数据。 + +![image-20220207145321717](6、运算符练习.assets/image-20220207145321717.png) + +```mysql +select id,device_id,gender,age,university,gpa,active_days_within_30,question_cnt,answer_cnt from user_profile where gpa>3.5 and university= '山东大学' or gpa>3.8 and university='复旦大学'; +``` + +## (8)题目:所有大学中带有北京的用户信息 + +现在运营想查看所有大学中带有北京的用户的信息,请你取出相应数据。 + +![image-20220207145355354](6、运算符练习.assets/image-20220207145355354.png) + +```mysql +SELECT * FROM user_profile WHERE university='北京大学'; +``` + +第9章 运算符 + +## 9.1 算术运算符(掌握) + +```mysql +加:+ + 在MySQL +就是求和,没有字符串拼接 +减:- +乘:* +除:/ div(只保留整数部分) + div:两个数相除只保留整数部分 + /:数学中的除 +模:% mod + +mysql中没有 +=等运算符 + +#select 表达式 +select 1+1; +update t_employee set salary = salary+100 where eid=27; + +select 9/2, 9 div 2; + +mysql> select 9/2, 9 div 2; ++--------+---------+ +| 9/2 | 9 div 2 | ++--------+---------+ +| 4.5000 | 4 | ++--------+---------+ +1 row in set (0.00 sec) + +select 9.5 / 1.5 , 9.5 div 1.5; + +mysql> select 9.5 / 1.5 , 9.5 div 1.5; ++-----------+-------------+ +| 9.5 / 1.5 | 9.5 div 1.5 | ++-----------+-------------+ +| 6.33333 | 6 | ++-----------+-------------+ +1 row in set (0.00 sec) + +select 9 % 2, 9 mod 2; +select 9.5 % 1.5 , 9.5 mod 1.5; + +select 'hello' + 'world'; +mysql> select 'hello' + 'world'; ++-------------------+ +| 'hello' + 'world' | ++-------------------+ +| 0 | ++-------------------+ +1 row in set, 2 warnings (0.00 sec) +``` + +## 9.2 比较运算符(掌握) + +```mysql +大于:> +小于:< +大于等于:>= +小于等于:>= +等于:= 不能用于null判断 +不等于:!= 或 <> 不能用于null判断 +判断是null 用 is null 或 用 <=> null +判断不是null is not null + + +#查询薪资高于15000的员工姓名和薪资 +select ename,salary from t_employee where salary>15000; + +mysql> select ename,salary from t_employee where salary>15000; ++--------+--------+ +| ename | salary | ++--------+--------+ +| 孙洪亮 | 28000 | +| 贾宝玉 | 15700 | +| 黄冰茹 | 15678 | +| 李冰冰 | 18760 | +| 谢吉娜 | 18978 | +| 舒淇格 | 16788 | +| 章嘉怡 | 15099 | ++--------+--------+ +7 rows in set (0.00 sec) + +#查询薪资正好是9000的员工姓名和薪资 +select ename,salary from t_employee where salary = 9000; +select ename,salary from t_employee where salary == 9000;#错误,不支持== #注意Java中判断用==,mysql判断用= + +mysql> select ename,salary from t_employee where salary == 9000; +ERROR 1064 (42000): You have an error in your SQL syntax; + check the manual that corresponds to your MySQL server version for the right syntax to use near '== 9000' at line 1 + +#查询工作地址 work_place不是北京的 +select * from t_employee where work_place != '北京'; +select * from t_employee where work_place <> '北京'; + +#查询员工表中部门编号不是1 +select * from t_employee where did != 1; +select * from t_employee where did <> 1; + +#查询奖金比例是NULL +select * from t_employee where commission_pct = null; + +mysql> select * from t_employee where commission_pct = null; #无法用=null判断 +Empty set (0.00 sec) +#mysql中只要有null值参与运算和比较,结果就是null,底层就是0,表示条件不成立。 + +#查询奖金比例是NULL +select * from t_employee where commission_pct <=> null; +select * from t_employee where commission_pct is null; + +#查询“李冰冰”、“周旭飞”、“李易峰”这几个员工的信息 +select * from t_employee where ename in ('李冰冰','周旭飞','李易峰'); + +#查询部门编号为2、3的员工信息 +select * from t_employee where did in(2,3); + +#查询部门编号不是2、3的员工信息 +select * from t_employee where did not in(2,3); + +#查询薪资在[10000,15000]之间 +select * from t_employee where salary between 10000 and 15000; + +#查询姓名中第二个字是'冰'的员工 +select * from t_employee where ename like '冰'; #这么写等价于 ename='冰' +select * from t_employee where ename like '_冰%'; +#这么写匹配的是第二个字是冰,后面可能没有第三个字,或者有好几个字 + +update t_employee set ename = '王冰' where ename = '李冰冰'; + +select * from t_employee where ename like '_冰_'; +#这么写匹配的是第二个字是冰,后面有第三个字,且只有三个字 + +#查询员工的姓名、薪资、奖金比例、实发工资 +#实发工资 = 薪资 + 薪资 * 奖金比例 +select ename as 姓名, +salary as 薪资, +commission_pct as 奖金比例, +salary + salary * commission_pct as 实发工资 +from t_employee; + +#NULL在mysql中比较和计算都有特殊性,所有的计算遇到的null都是null。 +#实发工资 = 薪资 + 薪资 * 奖金比例 +select ename as 姓名, +salary as 薪资, +commission_pct as 奖金比例, +salary + salary * ifnull(commission_pct,0) as 实发工资 +from t_employee; +``` + +## 9.3 区间或集合范围比较运算符(掌握) + +```mysql +区间范围:between x and y + not between x and y +集合范围:in (x,x,x) + not in(x,x,x) + between ... and ... 结果包含两端的边界 + +#查询薪资在[10000,15000] +select * from t_employee where salary>=10000 && salary<=15000; +select * from t_employee where salary between 10000 and 15000; + +#查询籍贯在这几个地方的 +select * from t_employee where work_place in ('北京', '浙江', '江西'); + +#查询薪资不在[10000,15000] +select * from t_employee where salary not between 10000 and 15000; + +#查询籍贯不在这几个地方的 +select * from t_employee where work_place not in ('北京', '浙江', '江西'); +``` + +## 9.4 模糊匹配比较运算符(掌握) + +%:代表任意个字符 + +_:代表一个字符,如果两个下划线代表两个字符 + +```mysql +#查询名字中包含'冰'字 +select * from t_employee where ename like '%冰%'; + +#查询名字以‘雷'结尾的 +select * from t_employee where ename like '%远'; + +#查询名字以’李'开头 +select * from t_employee where ename like '李%'; + +#查询名字有冰这个字,但是冰的前面只能有1个字 +select * from t_employee where ename like '_冰%'; + +#查询当前mysql数据库的字符集情况 +show variables like '%character%'; +``` + +## 9.5 逻辑运算符(掌握) + +```mysql +逻辑与:&& 或 and +逻辑或:|| 或 or +逻辑非:! 或 not +逻辑异或: xor + +#查询薪资高于15000,并且性别是男的员工 +select * from t_employee where salary>15000 and gender='男'; +select * from t_employee where salary>15000 && gender='男'; + +select * from t_employee where salary>15000 & gender='男';#错误得不到值 &按位与 +select * from t_employee where (salary>15000) & (gender='男'); + +#查询薪资高于15000,或者did为1的员工 +select * from t_employee where salary>15000 or did = 1; +select * from t_employee where salary>15000 || did = 1; + +#查询薪资不在[15000,20000]范围的 +select * from t_employee where salary not between 15000 and 20000; +select * from t_employee where !(salary between 15000 and 20000); + +#查询薪资高于15000,或者did为1的员工,两者只能满足其一 +select * from t_employee where salary>15000 xor did = 1; +select * from t_employee where (salary>15000) ^ (did = 1); +``` + +## 9.6 关于null值的问题(掌握) + +```mysql +#(1)判断时 +xx is null +xx is not null +xx <=> null + +#(2)计算时 +ifnull(xx,代替值) 当xx是null时,用代替值计算 +``` + +**所有的计算遇到的null都是null** + +```mysql +#查询奖金比例为null的员工 +select * from t_employee where commission_pct = null; #失败 +select * from t_employee where commission_pct = NULL; #失败 +select * from t_employee where commission_pct = 'NULL'; #失败 + +select * from t_employee where commission_pct is null; #成功 +select * from t_employee where commission_pct <=> null; #成功 <=>安全等于 + +#查询员工的实发工资,实发工资 = 薪资 + 薪资 * 奖金比例 +select ename , salary + salary * commission_pct "实发工资" from t_employee; #失败,当commission_pct为null,结果都为null + +select ename ,salary , commission_pct, salary + salary * ifnull(commission_pct,0) "实发工资" from t_employee; +``` + +## 9.7 位运算符(了解) + +基本不用,知道一下 + +```mysql +左移:<< +右移:>> +按位与:& +按位或:| +按位异或:^ +按位取反:~ +```