diff --git "a/23 \351\273\204\346\265\251\344\270\234/20230222 MySQL\346\224\266\347\264\242\345\222\214\350\277\220\347\256\227\347\254\246.md" "b/23 \351\273\204\346\265\251\344\270\234/20230222 MySQL\346\224\266\347\264\242\345\222\214\350\277\220\347\256\227\347\254\246.md" new file mode 100644 index 0000000000000000000000000000000000000000..75ff1a42a359b6de2ff0fee1dad1f3b2548422b0 --- /dev/null +++ "b/23 \351\273\204\346\265\251\344\270\234/20230222 MySQL\346\224\266\347\264\242\345\222\214\350\277\220\347\256\227\347\254\246.md" @@ -0,0 +1,517 @@ +# 1查询 + +## select语句 + +### 1select语句用途: + +用于查询计算结果,或是查询表中内容。 + +### 2基本语法 + +##### 1查询基本值: + +```mysql +select 常量; +select 表达式; +select 函数; +``` + +如: + +```MySQL +SELECT 1; +SELECT 9/2; +SELECT NOW(); +``` + +##### 2从表中查询数据语法: + +```mysql +select 字段名1,字段名2,······,字段名n from 表名 where 条件;#如果查询全部字段就用*来代替字段名,如果要查询的数据有一定条件则在where后面加上筛选条件,from表示数据的来源 +``` + +### 2关于别名 + +在当前select语句中给某个字段或表达式计算结果,或表等取个临时名称,便于当前select语句的编写和理解。这个临时名称称为别名。 + +语法: + +```MySQL +select 字段名1 as "别名1", 字段名2 as "别名2" from 表名称 as 别名; +``` + +##### 注意: + +- 列的别名有空格时,请加双引号。**==列的别名==中没有空格时,双引号可以加也可以不加**。 + +- **表的别名不能加双引号**,表的别名中间不能包含空格。 + +- as大小写都可以,as也完全可以省略。 + + + +### 3结果去重 + +在MySQL总中可以在select语句中使用distinct关键字去重 + +```mysql +select distinct 字段列名 from 表名 where 条件; +``` + +如: + +```mysql +select distinct city from stu_form ; +``` + +# 2运算符 + +## 1算数运算符 + +```mysql +加:+ + 在MySQL +就是求和,没有字符串拼接 +减:- +乘:* +除:/ div(只保留整数部分) + div:两个数相除只保留整数部分 + /:数学中的除 +模:% mod + +mysql中没有 +=等运算符 +``` + +## 2比较运算符 + +```MySQL +大于:> +小于:< +大于等于:>= +小于等于:>= +等于:= 不能用于null判断 +不等于:!= 或 <> 不能用于null判断 +判断是null 用 is null 或 用 <=> null +判断不是null is not null +``` + +## 3模糊匹配比较运算符 + +%:代表任意个字符 + +_:代表一个字符,如果两个下划线代表两个字符 + +## 4逻辑运算符 + +```mysql +逻辑与:&& 或 and +逻辑或:|| 或 or +逻辑非:! 或 not +逻辑异或: xor +#与java类似 +``` + +## 5 判断null + +```MySQL +#(1)判断时 +xx is null +xx is not null +xx <=> null + +#(2)计算时 +ifnull(xx,代替值) 当xx是null时,用代替值计算 +#所有的计算遇到的null都是null +``` + +# 作业 + +## 1select练习 + +# 第1组:user_profile表 + +user_profile表的sql脚本: + +```mysql +create database a CHARSET utf8; +use a; +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'); +select distinct university FROM user_profile; +``` + +![](D:\homework\专业课文件夹\MySQL课件\作业\基础查询与运算符的练习\5、SELECT语句基础练习.assets\image-20220207141745581 - 副本.png) + +解释:id(编号)、device_id(设备ID),gender(性别),age(年龄)、university(大学名称),province(省份) + +## (1)题目:从用户信息表中取出学校的去重数据 + +现在运营需要查看用户来自于哪些学校,请从用户信息表中取出学校的去重数据。 + +![image-20220207141745581](D:\homework\专业课文件夹\MySQL课件\作业\基础查询与运算符的练习\5、SELECT语句基础练习.assets\image-20220207141745581.png) + +```mysql +select distinct university FROM user_profile; +``` + + + +## (2)题目:查看用户明细设备ID数据,并将列名显示为 'user_infos_example' + +现在你需要查看用户明细设备ID数据,并将列名显示为 'user_infos_example',请你从用户信息表取出相应结果。 + +![image-20220207143458749](D:\homework\专业课文件夹\MySQL课件\作业\基础查询与运算符的练习\5、SELECT语句基础练习.assets\image-20220207143458749.png) + +```mysql +select id as "user_infos_example" from user_profile; +``` + +## (3)题目:查询university是北京大学的设备ID + +现在运营想要筛选出所有北京大学的学生进行用户调研,请你从用户信息表中取出满足条件的数据,结果返回设备id和学校。 + +![image-20220207144253185](D:\homework\专业课文件夹\MySQL课件\作业\基础查询与运算符的练习\5、SELECT语句基础练习.assets\image-20220207144253185.png) + +```mysql +select device_id FROM user_profile where university = '北京大学'; +``` + + + +## (4)题目:查询年龄大于24用户的设备ID、性别、年龄、学校 + +现在运营想要针对24岁以上的用户开展分析,请你取出满足条件的设备ID、性别、年龄、学校。 + +![image-20220207144628582](D:\homework\专业课文件夹\MySQL课件\作业\基础查询与运算符的练习\5、SELECT语句基础练习.assets\image-20220207144628582.png) + +```mysql +select device_id,gender,age,university from user_profile where age > 24; +``` + + + +## (5)题目:查询所有用户的设备id、性别、年龄、学校 + +现在运营同学想要用户的设备id对应的性别、年龄和学校的数据,请你取出相应数据 + +![image-20220207172816753](D:\homework\专业课文件夹\MySQL课件\作业\基础查询与运算符的练习\5、SELECT语句基础练习.assets\image-20220207172816753.png) + +```mysql +select device_id,gender,age,university from user_profile; +``` + + + +## (6)题目:查询所有用户的数据 + +现在运营想要查看用户信息表中所有的数据,请你取出相应结果 + +![image-20220207172840781](D:\homework\专业课文件夹\MySQL课件\作业\基础查询与运算符的练习\5、SELECT语句基础练习.assets\image-20220207172840781.png) + +```mysql +select * from user_profile; +``` + +## (7)题目:查询省份是"shanghai"的用户信息 + +现在运营想要查看上海市用户的信息,请你取出相应的结果。 + +根据示例,你的查询应返回以下结果: + +![image-20220210171353387](D:\homework\专业课文件夹\MySQL课件\作业\基础查询与运算符的练习\5、SELECT语句基础练习.assets\image-20220210171353387.png) + +```mysql +select * from user_profile where province = 'shanghai'; +``` + +## (8)题目:查询所有男性用户的设备ID、年龄、学校 + +现在运营想要查看所有男性用户的设备ID、年龄、学校,便于后期做数据分析。 + +根据示例,你的查询应返回如下结果: + +![image-20220210171628263](D:\homework\专业课文件夹\MySQL课件\作业\基础查询与运算符的练习\5、SELECT语句基础练习.assets\image-20220210171628263.png) + +```mysql +select device_id,age,university from user_profile where gender = 'male' +``` + +## (9)题目:从用户信息表中取出省份的去重数据 + +现在运营想要查看所有用户的省份分布情况,请从用户信息表中取出省份的去重数据。 + +根据示例,你的查询应返回如下结果: + +![image-20220210171835938](D:\homework\专业课文件夹\MySQL课件\作业\基础查询与运算符的练习\5、SELECT语句基础练习.assets\image-20220210171835938.png) + +```mysql +select distinct province from user_profile +``` + +# 第1题:user_profile表脚本1 + +user_profile表的sql脚本: + +```mysql +create database a CHARSET utf8; +use a; +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'); +select distinct university FROM user_profile; +``` + +![](D:/homework/专业课文件夹/MySQL课件/作业/基础查询与运算符的练习/6、运算符练习.assets/image-20220207141745581 - 副本.png) + +解释:id(编号)、device_id(设备ID),gender(性别),age(年龄)、university(大学名称),province(省份) + +## (1)题目:查询年龄20岁及以上且23岁及以下用户的设备ID、性别、年龄 + +现在运营想要针对20岁及以上且23岁及以下的用户开展分析,请你取出满足条件的设备ID、性别、年龄。 + +![image-20220207144855013](D:/homework/专业课文件夹/MySQL课件/作业/基础查询与运算符的练习/6、运算符练习.assets/image-20220207144855013.png) + +```mysql +select device_id,gender,age from user_profile where age >= 20 AND age <= 23 +``` + + + +## (2)题目:查询除复旦大学以外的所有用户的设备ID、性别、年龄、大学 + +现在运营想要查看除复旦大学以外的所有用户明细,请你取出相应数据 + +![image-20220207144942510](D:/homework/专业课文件夹/MySQL课件/作业/基础查询与运算符的练习/6、运算符练习.assets/image-20220207144942510.png) + +```mysql +select device_id,gender,age,university,gpa from user_profile where university != '复旦大学'; +``` + + + +## (3)题目:查询年龄不为空的用户的设备ID,性别,年龄,学校的信息 + +现在运营想要对用户的年龄分布开展分析,在分析时想要剔除没有获取到年龄的用户,请你取出所有年龄值不为空的用户的设备ID,性别,年龄,学校的信息。 + +![image-20220207145017152](D:/homework/专业课文件夹/MySQL课件/作业/基础查询与运算符的练习/6、运算符练习.assets/image-20220207145017152.png) + +```mysql +select device_id,gender,age,university from user_profile where age is not null; +``` + +# 2运算符练习 + +# 第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](D:/homework/专业课文件夹/MySQL课件/作业/基础查询与运算符的练习/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](D:/homework/专业课文件夹/MySQL课件/作业/基础查询与运算符的练习/6、运算符练习.assets/image-20220207145044808.png) + +```mysql +select device_id,gender,age,university,gpa from user_profile where gpa > 3.5 and gender = 'male' +``` + + + +## (5)题目:查询学校为北大或GPA在3.7以上(不包括3.7)的用户的设备ID,性别、年龄、学校、gpa + +现在运营想要找到学校为北大或GPA在3.7以上(不包括3.7)的用户进行调研,请你取出相关数据(使用OR实现) + +![image-20220207145119374](D:/homework/专业课文件夹/MySQL课件/作业/基础查询与运算符的练习/6、运算符练习.assets/image-20220207145119374.png) + +```mysql +select device_id,gender,age,university,gpa from user_profile where gpa > 3.7 ; +``` + + + +## (6)题目:查询学校为北大、复旦和山大用户的设备ID,性别、年龄、学校、gpa + +现在运营想要找到学校为北大、复旦和山大的同学进行调研,请你取出相关数据。 + +![image-20220207145152255](D:/homework/专业课文件夹/MySQL课件/作业/基础查询与运算符的练习/6、运算符练习.assets/image-20220207145152255.png) + +```mysql +select device_id,gender,age,university,gpa from user_profile where university = '北京大学' or university = '复旦大学' or university = '山东大学'; +``` + + + +## (7)题目:查询gpa在3.5以上(不包括3.5)的山东大学用户 或 gpa在3.8以上(不包括3.8)的复旦大学同学 + +现在运营想要找到gpa在3.5以上(不包括3.5)的山东大学用户 或 gpa在3.8以上(不包括3.8)的复旦大学同学进行用户调研,请你取出相应数据。 + +![image-20220207145321717](D:/homework/专业课文件夹/MySQL课件/作业/基础查询与运算符的练习/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 or university = '复旦大学' +``` + + + +## (8)题目:所有大学中带有北京的用户信息 + +现在运营想查看所有大学中带有北京的用户的信息,请你取出相应数据。 + +![image-20220207145355354](D:/homework/专业课文件夹/MySQL课件/作业/基础查询与运算符的练习/6、运算符练习.assets/image-20220207145355354.png) + +```mysql +select device_id,age,university from user_profile where university like '北京%'; +``` + +# 作业 + +## 第1题:员工表 + +```mysql +-- ## 第1题:员工表 +create database `work` charset utf8; +use `work` +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); +-- | **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 | +-- +-- **要求1:**查询出薪资在12000~13000之间的员工信息。 +select * from employee where salary between 12000 and 13000; +-- **要求2:**查询出姓“刘”的员工的工号,姓名,家庭住址。 +select id,`name`,addr from employee where name like '刘%' ; +-- **要求3:**将“李四”的家庭住址改为“广东韶关” +update employee set addr = '广东韶关' where `name` = '李四'; +-- **要求4:**查询出名字中带“小”的员工 +select * from employee where `name` like '%小%'; +-- **要求5:**查询出薪资高于11000的男员工信息 +select * from employee where salary > 11000 and sex = '男'; +-- **要求6:**查询没有登记电话号码的员工 +select * from employee where tel is null +-- **要求7:**查询薪资高于12000或者家是广东深圳、广州的男员工 +select * from employee where sex = '男' and salary > 12000 or addr in ('广东深圳' or '广东广州'); +-- **要求8:**查询每个员工的年薪,显示“姓名、年薪” +select name,salary from employee +``` + +## 第2题:国家信息表 + +```MySQL +-- ## 第2题:国家信息表 +create database `homework`; +use homework; +-- 表数据样例: +-- ```mysql +-- +-------------+-----------+---------+------------+--------------+ +-- | 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 | +-- +-------------+-----------+---------+------------+--------------+ +DROP TABLE IF EXISTS `countries_info`; +CREATE TABLE `countries_info`( + `name` VARCHAR(100), + `continent` VARCHAR(100), + `area` INT, + population INT, + gdp BIGINT +); + +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); +-- +-- **要求1:** 查询大国 的国家名称、人口和面积。 +-- 如果一个国家满足下述两个条件之一,则认为该国是 大国 : +-- +-- - 面积至少为 300万平方公里(即,3000000 km2) +-- +-- - 人口至少为 2500 万(即 25000000) +select `name`,area,population from countries_info where area > 3000000 or population >25000000; +-- **要求2:**查询属于亚洲的国家名称、所属大陆、面积、人口和 GDP 值 +select * from countries_info where continent = 'Asia'; +-- **要求3:**查询国土面积不足1万平方公里且人口不走10万人的国家信息 +select * from countries_info where area < 10000 and population < 100000; +-- **要求4:**查询国家名字中包含“o“字母的国家信息 +select * from countries_info where `name` like '%o%'; +-- **要求5:**查询GDP值超过10000000000的国家信息 +select * from countries_info where gdp > 10000000000; +-- **要求6:**查询每个国家的人均贡献GDP值(GDP/人口总数)并显示为“国家名、人口、GDP值、人均贡献GDP值” +select `name`as 国家名,population as 人口,gdp as GDP值,gdp/population as 人均贡献GDP值 from countries_info; +-- **要求7:**查询人均贡献GDP值低于1000的国家信息。 +select * from countries_info where '人均贡献GDP值' < 1000 ; +-- **要求8:**查询每个国家的人均国土面积(面积/人口总数)并显示为“国家名、面积、人口、人均国土面积值” +select `name` as "国家名",area as "面积", population as "人口",area/population as "人均国土面积"from countries_info; +``` +