diff --git "a/56 \350\265\265\346\225\217/20230226 \350\277\220\347\256\227\347\254\246\344\275\234\344\270\232.md" "b/56 \350\265\265\346\225\217/20230226 \350\277\220\347\256\227\347\254\246\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..c4f198eddccf6adb582c2fd26369429b4b7bf198 --- /dev/null +++ "b/56 \350\265\265\346\225\217/20230226 \350\277\220\347\256\227\347\254\246\344\275\234\344\270\232.md" @@ -0,0 +1,240 @@ +作业 + +```MySQL +drop table if exists user_profile; +create database zy charset utf8; +use zy; +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)题目:从用户信息表中取出学校的去重数据 +SELECT university FROM user_profile; +select distinct `university` from user_profile; +-- (2)题目:查看用户明细设备ID数据,并将列名显示为 'user_infos_example' +select device_id as user_infos_example from user_profile; +-- (3)题目:查询university是北京大学的设备ID +select device_id,university from user_profile where university = '北京大学'; +-- (4)题目:查询年龄大于24用户的设备ID、性别、年龄、学校 +select device_id,gender,age,university from user_profile where age >24; +-- (5)题目:查询所有用户的设备id、性别、年龄、学校 +select device_id,gender,age,university from user_profile; +-- (6)题目:查询所有用户的数据 +select * from user_profile; +-- (7)题目:查询省份是"shanghai"的用户信息 +select * from user_profile where province = 'Shanghai'; +-- (8)题目:查询所有男性用户的设备ID、年龄、学校 +select device_id,age,university from user_profile where gender = 'male'; +-- (9)题目:从用户信息表中取出省份的去重数据 +SELECT province FROM user_profile; +select distinct province from user_profile +``` + +```MySQL +drop table if exists user_profile; +create database zy2 charset utf8; +use zy2; +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)题目:查询年龄20岁及以上且23岁及以下用户的设备ID、性别、年龄 +select device_id,gender,age from user_profile where age>=20 and age<=23; +-- (2)题目:查询除复旦大学以外的所有用户的设备ID、性别、年龄、大学 +select device_id,gender,age,university from user_profile where university != '复旦大学'; +-- (3)题目:查询年龄不为空的用户的设备ID,性别,年龄,学校的信息 +select device_id,gender,age,university from user_profile where age is not null; +``` + +```mysql +-- ## 第1题:员工表 +create database zy1 charset utf8; +use zy1; + +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); +-- +select * from employee; +-- +-- **要求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 sex = '男' and salary >11000; +-- +-- **要求6:**查询没有登记电话号码的员工 +select * from employee where tel is null; +-- +-- **要求7:**查询薪资高于12000或者家是广东深圳、广州的男员工 +select * from employee where sex = '男' and (salary >12000 or (addr = '广东深圳' and addr = '广东广州')); +-- +-- **要求8:**查询每个员工的年薪,显示“姓名、年薪” +select `name`,salary from employee; +``` + +```MySQL +-- ## 第2题:国家信息表 +-- +-- countries_info表中存储了国家名称、所属大陆、面积、人口和 GDP 值。 +-- +create database zy2 charset utf8; +use zy2; + 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:** 查询大国 的国家名称、人口和面积。 +select `name`,population,`area` FROM `countries_info` where `area` >= 3000000 and population >= 25000000; +-- +-- 如果一个国家满足下述两个条件之一,则认为该国是 大国 : +-- +-- - 面积至少为 300万平方公里(即,3000000 km2) +-- +-- - 人口至少为 2500 万(即 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`,population,gdp,gdp/population '人均gdp' from `countries_info`; +-- +-- **要求7:**查询人均贡献GDP值低于1000的国家信息。 +select * from `countries_info` where gdp/population < 1000; +-- +-- **要求8:**查询每个国家的人均国土面积(面积/人口总数)并显示为“国家名、面积、人口、人均国土面积值” +select `name`,`area`,population,`area`/population '人均国土面积' from `countries_info`; +``` + +笔记 + +``` +# 笔记 + +## DQL + +select语句的基本语法: select 常量; select 表达式; select 函数; + +例:select 1; select 9/2; select NOW(); + +1.从数据中筛选数据的格式: select 字段列表 from 表名称;【where 条件;】 + +2.使用别名: + + 格式:select 字段名 【as】别名 from 表名称 【as】 别名; + + --列的别名有空格时,加双引号。没有时,可加可不加。 + + --表的别名不能加双引号,表的别名中不加空格。 + +3.结果去重: distinct + + 格式: select distinct 字段列表 from 表名称 【where 条件】; + + --distinct只能发在第一个字段前面 --它只支持一个字段。 + +## 运算符 + +1.算数运算符: + +加 + ,减 - , 乘 * ,除 / 或div , (div只保留整数部分) , 模(余) % ,mod + +2.比较运算符: + +大于 > ,小于 < , 大于等于 >= , 小于等于 <= , 等于 = , 不等于 != 。 (等于与不等于不能用于null计算) + +3.区间运算符: + + 区间范围: between x and y not between x and y + + 集合范围: in(x,y,z) not in (x,y,z) # between....and ......(结果包含两边边界) + +4.模糊比较运算符: + + %:代表任意个字符 _:代表一个字符,几个下划线就代表几个字符。 + + 格式:select * from 表名 where 字段名 ' '; + + #查询当前mysql数据库在字符集情况:show variables like ‘%character%’ + +5.逻辑运算符: + + 与: && 或 and + + 或: || 或or + + 非: !或者not + + 异或:xor + +6.关于null值的问题: + +​ 1.判断时: xx is null xx is not null xx<=>null + +​ 2.计算时: if null (xx,代替值) 当xx是null时,用替代值算 +``` + diff --git "a/56 \350\265\265\346\225\217/20230309 \345\255\220\346\237\245\350\257\242 .md" "b/56 \350\265\265\346\225\217/20230309 \345\255\220\346\237\245\350\257\242 .md" new file mode 100644 index 0000000000000000000000000000000000000000..74af8057bc6bc288cbddb56c8d19755304580b10 --- /dev/null +++ "b/56 \350\265\265\346\225\217/20230309 \345\255\220\346\237\245\350\257\242 .md" @@ -0,0 +1,125 @@ +笔记 + +``` +子查询 + +- 嵌套在其他SQL语句中的查询语句。 +- 子查询通常先运行,所以要用()包裹起来。 +- 子查询的运行结果,可以作为其他语句的条件,数据源等。 + +子查询分三种形式 + + select 1 from 2 where 3 + + 1.放在select后面 + + 2.放在where/having后面 + + 3.放在from后面,子查询当做一个临时的表使用,必须要给别名。 + + + +where或having后的子查询,其子查询的结果有三种形式 + + 1.单列单个值,可以直接使用比较运算符 + + 2.单列多个值,可以使用 in ,not in 进行比较 + + 3.单列多个值,可以使用比较运算符,搭配any,all,等关键字与查询结果 子查询结果是单列,可以放在select参与运算,或者是作为字段,也可以放在where / having后当条件。 + +子查询结果是多列,多个值,只能放在from后面当临时表使用,并且必须取别名。 +``` + + + +作业 + +```MySQL + +CREATE DATABASE mn CHARSET utf8; +USE mn; +CREATE table stuinfo( +stuNo varchar(10000), +stuname varchar(5), +stusex varchar(2), +stuage int(3), +stuaddress varchar (10), +stuseat int + + ); +CREATE table stuexam ( +examno int, +stuno varchar(90), +writtrnexam int, +labexam int +) ; + +CREATE table stumarks( +examno int, +stuid varchar(90), +score int +); + INSERT into stuinfo VALUES + ('s2501','张秋利','男', 20,'美国硅谷',1), + ('s2502','李思文','女',18,'武汉湖北',2), + ('s2503','马文才','男',18,'湖南长沙',3), + ('s2504','欧阳俊雄','女',21,'湖北武汉',4), + ('s2505','梅超风','男',16,'湖北武汉',5), + ('s2506','陈旋风','男',19,'美国硅谷',6); + + INSERT into stuexam values + (1,'s2501',50,70), + (2,'s2502',60,65), + (3,'s2503',86,70), + (4,'s2504',40,80), + (5,'s2505',70,85), + (6,'s2506',85,90); + + insert into stumarks VALUES + (1,'s2501',88), + (2,'s2502',92), + (3,'s2503',53), + (4,'s2504',60), + (5,'s2505',99), + (6,'s2506',82); + + + -- 在如图的数据表上完成以下题目 +-- +-- 1.查询出年龄比班上平均年龄大的学生的信息 + select * from stuinfo where stuage >(select avg(stuage ) from stuinfo); + +-- 2.查询出每个学生的学号,姓名,性别和选修课程的最高分(stuMarks) +select +-- +-- 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) + +-- 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +select * from stuinfo where stusex = '男' and stuage >=20; + +-- 5.查询出年龄比所有男生年龄都大的女生的信息 +select * from stuinfo where stusex = '女' and stuage = (select max(stuage) from stuinfo); + +-- 6.查询出所有选修课程都及格的学生的信息 (stuMarks) +-- +-- 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +-- +-- 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +-- +-- 9.将有一门成绩成绩大于90分的学生的基本信息查询出来(stuMarks) +-- +-- 10.查询出平均成绩在80分以上的学生的基本信息(stuMarks) +-- +-- 11.查询出某同学所有考试成绩比“张秋利”同学所有分数都高的学生基本信息(stuMarks) +-- +-- 12.查询出某同学所有考试成绩只需要比“张秋利”同学某个分数高的学生基本信息(stuMarks) + + +-- 13.查询班上比所有男生年龄都要大的女生的信息 + +select * from stuinfo where stusex = '女' and stuage = (select max(stuage) from stuinfo); + +-- 14.查询出只要比某个男生年龄大的女生的信息 +select * from stuinfo where stusex = '女' and stuage > (select min(stuage) from stuinfo where stusex = '男'); +``` +