From 647f38942b546e15ca4f61b691e049f2049983e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E9=AB=98=E7=A7=91?= <“18317545175@163.com”> Date: Fri, 24 Feb 2023 12:42:08 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zuoye04.md" | 136 ++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 "42 \345\210\230\351\253\230\347\247\221/zuoye04.md" diff --git "a/42 \345\210\230\351\253\230\347\247\221/zuoye04.md" "b/42 \345\210\230\351\253\230\347\247\221/zuoye04.md" new file mode 100644 index 0000000..a9172f2 --- /dev/null +++ "b/42 \345\210\230\351\253\230\347\247\221/zuoye04.md" @@ -0,0 +1,136 @@ +# 第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(省份) + +## (1)题目:查询年龄20岁及以上且23岁及以下用户的设备ID、性别、年龄 + +现在运营想要针对20岁及以上且23岁及以下的用户开展分析,请你取出满足条件的设备ID、性别、年龄。 + +![image-20220207144855013](6、运算符练习.assets/image-20220207144855013.png) + +```mysql +select device_id,gender,age from user_profile where age between 20 and 23; +``` + +## (2)题目:查询除复旦大学以外的所有用户的设备ID、性别、年龄、大学 + +现在运营想要查看除复旦大学以外的所有用户明细,请你取出相应数据 + +![image-20220207144942510](6、运算符练习.assets/image-20220207144942510.png) + +```mysql +select device_id,gender,age,university from user_profile where university != '复旦大学'; +``` + +## (3)题目:查询年龄不为空的用户的设备ID,性别,年龄,学校的信息 + +现在运营想要对用户的年龄分布开展分析,在分析时想要剔除没有获取到年龄的用户,请你取出所有年龄值不为空的用户的设备ID,性别,年龄,学校的信息。 + +![image-20220207145017152](6、运算符练习.assets/image-20220207145017152.png) + +```mysql +select device_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 + +## (4)题目:查询男性且GPA在3.5以上(不包括3.5)的用户的设备ID,性别、年龄、学校、gpa + +现在运营想要找到男性且GPA在3.5以上(不包括3.5)的用户进行调研,请你取出相关数据。 + +![image-20220207145044808](6、运算符练习.assets/image-20220207145044808.png) + +```mysql +select device_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 device_id,gender,age,university,gpa from user_profile where university='北京大学' or gpa>3.7; +``` + +## (6)题目:查询学校为北大、复旦和山大用户的设备ID,性别、年龄、学校、gpa + +现在运营想要找到学校为北大、复旦和山大的同学进行调研,请你取出相关数据。 + +![image-20220207145152255](6、运算符练习.assets/image-20220207145152255.png) + +```mysql +select device_id,gender,age,university,gpa from user_profile where university='北京大学'and'复旦大学'and'山东大学'; +``` + +## (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 device_id,gender,age,university,gpa 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 device_id,age,university from user_profile where university like '%北京%'; +``` -- Gitee From c4bddf4faba9eefa7546fa0a8e65e8a349990bc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E9=AB=98=E7=A7=91?= <“18317545175@163.com”> Date: Sat, 25 Feb 2023 12:42:40 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zuoye04.md" | 178 +++++++++--------- 1 file changed, 90 insertions(+), 88 deletions(-) diff --git "a/42 \345\210\230\351\253\230\347\247\221/zuoye04.md" "b/42 \345\210\230\351\253\230\347\247\221/zuoye04.md" index a9172f2..6c602ab 100644 --- "a/42 \345\210\230\351\253\230\347\247\221/zuoye04.md" +++ "b/42 \345\210\230\351\253\230\347\247\221/zuoye04.md" @@ -1,136 +1,138 @@ -# 第1题:user_profile表脚本1 - -user_profile表的sql脚本: +## 第1题:员工表 ```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'); +drop table if exists `employee`; +#创建employee表 +CREATE TABLE employee( + id INT, + `name` VARCHAR(20), + sex VARCHAR(20), + tel VARCHAR(20), + addr VARCHAR(50), + salary FLOAT +); + +#添加信息 +INSERT INTO employee(id,`name`,sex,tel,addr,salary)VALUES +(10001,'张一一','男','13456789000','广东韶关',10010.58), +(10002,'刘小红','女','13454319000','广东江门',12010.21), +(10003,'李四','男','0751-1234567','广东佛山',10040.11), +(10004,'刘小强','男','0755-5555555','广东深圳',15010.23), +(10005,'王艳','男',NULL,'广东广州',14050.16); ``` -![](6、运算符练习.assets/image-20220207141745581 - 副本.png) +| **id** | **name** | **sex** | **tel** | **addr** | **salary** | +| ------ | -------- | ------- | ------------ | -------- | ---------- | +| 10001 | 张一一 | 男 | 13456789000 | 广东韶关 | 10010.58 | +| 10002 | 刘小红 | 女 | 13454319000 | 广东江门 | 12010.21 | +| 10003 | 李四 | 男 | 0751-1234567 | 广东佛山 | 10040.11 | +| 10004 | 刘小强 | 男 | 0755-5555555 | 广东深圳 | 15010.23 | +| 10005 | 王艳 | 女 | NULL | 广东广州 | 14050.16 | -解释:id(编号)、device_id(设备ID),gender(性别),age(年龄)、university(大学名称),province(省份) +**要求1:**查询出薪资在12000~13000之间的员工信息。 -## (1)题目:查询年龄20岁及以上且23岁及以下用户的设备ID、性别、年龄 +**要求2:**查询出姓“刘”的员工的工号,姓名,家庭住址。 -现在运营想要针对20岁及以上且23岁及以下的用户开展分析,请你取出满足条件的设备ID、性别、年龄。 +**要求3:**将“李四”的家庭住址改为“广东韶关” -![image-20220207144855013](6、运算符练习.assets/image-20220207144855013.png) +**要求4:**查询出名字中带“小”的员工 -```mysql -select device_id,gender,age from user_profile where age between 20 and 23; -``` +**要求5:**查询出薪资高于11000的男员工信息 -## (2)题目:查询除复旦大学以外的所有用户的设备ID、性别、年龄、大学 +**要求6:**查询没有登记电话号码的员工 -现在运营想要查看除复旦大学以外的所有用户明细,请你取出相应数据 +**要求7:**查询薪资高于12000或者家是广东深圳、广州的男员工 -![image-20220207144942510](6、运算符练习.assets/image-20220207144942510.png) +**要求8:**查询每个员工的年薪,显示“姓名、年薪” ```mysql -select device_id,gender,age,university from user_profile where university != '复旦大学'; -``` +select * from employee where salary between 12000 and 13000; -## (3)题目:查询年龄不为空的用户的设备ID,性别,年龄,学校的信息 +select id,name,addr from employee where name like '%刘%'; -现在运营想要对用户的年龄分布开展分析,在分析时想要剔除没有获取到年龄的用户,请你取出所有年龄值不为空的用户的设备ID,性别,年龄,学校的信息。 +update employee set addr='广东韶关' where name='李四'; -![image-20220207145017152](6、运算符练习.assets/image-20220207145017152.png) +select name from employee where name like '%小%'; -```mysql -select device_id,gender,age,university from user_profile where age is not null ; -``` - -# 第2题:user_profile表脚本2 +select * from employee where salary>11000 and sex='男'; -user_profile表sql脚本 +select * from employee where tel is null ; -```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 -); +select * from employee where salary>12000 or addr ='广东深圳'or'广东广州'; -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 name,salary from employee ; ``` -![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(回答数量) +## 第2题:国家信息表 -例如:第一行表示:id为1的用户的常用信息为使用的设备id为2138,性别为男,年龄21岁,北京大学,gpa为3.4,在过去的30天里面活跃了7天,发帖数量为2,回答数量为12 +countries_info表中存储了国家名称、所属大陆、面积、人口和 GDP 值。 -## (4)题目:查询男性且GPA在3.5以上(不包括3.5)的用户的设备ID,性别、年龄、学校、gpa +```mysql +DROP TABLE IF EXISTS `countries_info`; +CREATE TABLE `countries_info`( + `name` VARCHAR(100), + `continent` VARCHAR(100), + `area` INT, + population INT, + gdp BIGINT +); -现在运营想要找到男性且GPA在3.5以上(不包括3.5)的用户进行调研,请你取出相关数据。 +INSERT INTO countries_info VALUES +('Afghanistan','Asia',652230,25500100,20343000000), +('Albania','Europe',28748,2831741,12960000000), +('Algeria','Africa',2381741,37100000,188681000000), +('Andorra','Europe',468,78115,3712000000), +('Angola','Africa',1246700,20609294,100990000000); +``` -![image-20220207145044808](6、运算符练习.assets/image-20220207145044808.png) +表数据样例: ```mysql -select device_id,gender,age,university,gpa from user_profile where gpa > 3.5; ++-------------+-----------+---------+------------+--------------+ +| name | continent | area | population | gdp | ++-------------+-----------+---------+------------+--------------+ +| Afghanistan | Asia | 652230 | 25500100 | 20343000000 | +| Albania | Europe | 28748 | 2831741 | 12960000000 | +| Algeria | Africa | 2381741 | 37100000 | 188681000000 | +| Andorra | Europe | 468 | 78115 | 3712000000 | +| Angola | Africa | 1246700 | 20609294 | 100990000000 | ++-------------+-----------+---------+------------+--------------+ ``` -## (5)题目:查询学校为北大或GPA在3.7以上(不包括3.7)的用户的设备ID,性别、年龄、学校、gpa +**要求1:** 查询大国 的国家名称、人口和面积。 -现在运营想要找到学校为北大或GPA在3.7以上(不包括3.7)的用户进行调研,请你取出相关数据(使用OR实现) +如果一个国家满足下述两个条件之一,则认为该国是 大国 : -![image-20220207145119374](6、运算符练习.assets/image-20220207145119374.png) +- 面积至少为 300万平方公里(即,3000000 km2) -```mysql -select device_id,gender,age,university,gpa from user_profile where university='北京大学' or gpa>3.7; -``` +- 人口至少为 2500 万(即 25000000) -## (6)题目:查询学校为北大、复旦和山大用户的设备ID,性别、年龄、学校、gpa +**要求2:**查询属于亚洲的国家名称、所属大陆、面积、人口和 GDP 值 -现在运营想要找到学校为北大、复旦和山大的同学进行调研,请你取出相关数据。 +**要求3:**查询国土面积不足1万平方公里且人口不走10万人的国家信息 -![image-20220207145152255](6、运算符练习.assets/image-20220207145152255.png) +**要求4:**查询国家名字中包含“o“字母的国家信息 -```mysql -select device_id,gender,age,university,gpa from user_profile where university='北京大学'and'复旦大学'and'山东大学'; -``` +**要求5:**查询GDP值超过10000000000的国家信息 -## (7)题目:查询gpa在3.5以上(不包括3.5)的山东大学用户 或 gpa在3.8以上(不包括3.8)的复旦大学同学 +**要求6:**查询每个国家的人均贡献GDP值(GDP/人口总数)并显示为“国家名、人口、GDP值、人均贡献GDP值” -现在运营想要找到gpa在3.5以上(不包括3.5)的山东大学用户 或 gpa在3.8以上(不包括3.8)的复旦大学同学进行用户调研,请你取出相应数据。 +**要求7:**查询人均贡献GDP值低于1000的国家信息。 -![image-20220207145321717](6、运算符练习.assets/image-20220207145321717.png) +**要求8:**查询每个国家的人均国土面积(面积/人口总数)并显示为“国家名、面积、人口、人均国土面积值” ```mysql -select device_id,gender,age,university,gpa from user_profile where (gpa>3.5 and university='山东大学')or(gpa>3.8 and university='复旦大学'); -``` +select * from countries_info where continent = 'Asia'; -## (8)题目:所有大学中带有北京的用户信息 +select * from countries_info where area <=10000 and population<=100000; -现在运营想查看所有大学中带有北京的用户的信息,请你取出相应数据。 +select * from countries_info where name like '%o%'; -![image-20220207145355354](6、运算符练习.assets/image-20220207145355354.png) +select * from countries_info where gdp >= 10000000000; -```mysql -select device_id,age,university from user_profile where university like '%北京%'; +select name,population,gdp,(gdp/countries_info.population)as '人均贡献值' from countries_info; + +select * from countries_info where (gdp/countries_info.population)<1000; + +select name,area,population,(area/countries_info.population) from countries_info; ``` -- Gitee