From b5555cdeb305f68c38ac144e175861f5cd8dc155 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E4=BD=B3=E6=B3=BD?= <1097559914@qq.com> Date: Fri, 24 Feb 2023 21:43:11 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=89=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...37\345\222\214\347\273\203\344\271\240.md" | 20 + ...2_\350\277\220\347\256\227\347\254\246.md" | 412 ++++++++++++++++++ 2 files changed, 432 insertions(+) create mode 100644 "02 \346\236\227\344\275\263\346\263\275/20230222\346\237\245\350\257\242_\350\277\220\347\256\227\347\254\246.md" diff --git "a/02 \346\236\227\344\275\263\346\263\275/20230220 \347\272\246\346\235\237\345\222\214\347\273\203\344\271\240.md" "b/02 \346\236\227\344\275\263\346\263\275/20230220 \347\272\246\346\235\237\345\222\214\347\273\203\344\271\240.md" index 3cfeb64..826df0f 100644 --- "a/02 \346\236\227\344\275\263\346\263\275/20230220 \347\272\246\346\235\237\345\222\214\347\273\203\344\271\240.md" +++ "b/02 \346\236\227\344\275\263\346\263\275/20230220 \347\272\246\346\235\237\345\222\214\347\273\203\344\271\240.md" @@ -14,10 +14,30 @@ ​ 例子: +```mysql +create table 表名( + 字段名 类型 primary key +); +``` + #### 1.2、not null 约束:非空约束 +​ 限定某个字段/某列的值不为空 + +​ ①建表时 + +```mysql +create table 表名( + +id int not null + +); +``` + + + #### 1.3、check约束:检查约束(5.7不适用) #### 1.4、默认值约束 diff --git "a/02 \346\236\227\344\275\263\346\263\275/20230222\346\237\245\350\257\242_\350\277\220\347\256\227\347\254\246.md" "b/02 \346\236\227\344\275\263\346\263\275/20230222\346\237\245\350\257\242_\350\277\220\347\256\227\347\254\246.md" new file mode 100644 index 0000000..9580c58 --- /dev/null +++ "b/02 \346\236\227\344\275\263\346\263\275/20230222\346\237\245\350\257\242_\350\277\220\347\256\227\347\254\246.md" @@ -0,0 +1,412 @@ +# 20230222查询_运算符 + +### 一、DQL(数据查询语言) + +​ 1、select语句 + +​ ①select语句是用于查询计算结果,或查看从数据表中筛选出的数据。 + +​ ②基本语法: + +```mysql +select 常量; +select 1; #结果1 +select 表达式; +select now();#显示现在时间 +select 函数; +select 5+5; #显示运算结果 +``` + +​ ③从数据表中筛选 + +```mysql +select 字段 from 表名; +select 字段 from 数据库名称.表名称;(跨库查询) +``` + +​ ④从数据表中根据条件筛选数据,需要在from语句加where子句: + +```mysql +select 字段 from 表名称 where 条件; +``` + +​ 2、别名 + +​ ①在当前select语句中给某个字段或表达式计算结果,或表等取个临时名称。 + +​ ②格式: + +```mysql +select 字段 as "别名",字段2 "别名",字段3 别名 from 表名称 as 名称; +``` + +​ (1)字段列别名有空格,要加双引号,若别名中没有空格时,双引号可加,可不加 + +​ (2)表的别名不能加双引号,表的别名不能包含空格 + +​ (3)格式中带as大小写都行,不带也行 + +​ 3、结果去重 + +​ ①可以在查询结果中使用distinct关键字去重 + +​ ②格式: + +```mysql +select distinct 字段 from 表名称; #不带条件 +select distinct 字段 from 表名称 where 条件; +``` + +​ ③ distinct 必须放在select后面,不能放在字段后. + +### 二、运算符 + +​ ①大于:> 、小于:<、大于等于:>=、小于等于:<=、等于:= 不能用于null判断、不等于:!= 不用于null判断 + +​ ②+(加): 1+1; #2、-(减): 2-1; #1、*(乘):2X3;#6、/(除法): 6/3;#2、%,mod(求余):9%2;#1 + +​ ③区间范围:between x and y + +​ 例:取10~20的数据 + +```mysql +select * from 表名 where 字段 between 10 and 20; +not between x and y; +``` + +​ ④集合范围:in(x,x,x):只要在范围内就行 + +​ 例:查询1,2,3数据 + +```mysql +select * from 表名 where 字段 in(1,2,3); +``` + +​ ⑤ %:代表任意个字符 + +```mysql +select * from 表名 where 字段 like "%林%"; +``` + +​ ⑥_:代表一个字符,两个下划线代表两个字符 + +```mysql +select * from 表名 where 字段 like "林__" +``` + +​ ⑦逻辑与:&&或and(并且) + +​ 逻辑或:|| 或 or (或者) + +​ 逻辑非:!或not (取反) + +​ 逻辑异或:xor 两者只能满足其中一个,不能同时满足,不能同时不满足 + +​ ⑧判断 字段是否空 + +```mysql +select * from 表名 where 字段 is null; +select * from 表名 where 字段 <=> null; +``` + +​ ⑨计算时有null值 + +​ 使用ifnull(字段名,代替值) + +## 第1题:员工表 + +```mysql +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); +``` + + + +| **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之间的员工信息。 + +**要求2:**查询出姓“刘”的员工的工号,姓名,家庭住址。 + +**要求3:**将“李四”的家庭住址改为“广东韶关” + +**要求4:**查询出名字中带“小”的员工 + +**要求5:**查询出薪资高于11000的男员工信息 + +**要求6:**查询没有登记电话号码的员工 + +**要求7:**查询薪资高于12000或者家是广东深圳、广州的男员工 + +**要求8:**查询每个员工的年薪,显示“姓名、年薪” + +```mysql +create database s0222; +use s0222; +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); +# **要求1:**查询出薪资在12000~13000之间的员工信息。 +select * from employee where salary>=12000 and salary<=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 ((addr='广东深圳' and addr='广东广州' ) or salary>12000) and sex='男'; +# **要求8:**查询每个员工的年薪,显示“姓名、年薪” +select name 姓名,salary 年薪 from employee; + +``` + +## 第2题:国家信息表 + +countries_info表中存储了国家名称、所属大陆、面积、人口和 GDP 值。 + +```mysql +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); +``` + +表数据样例: + +```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 | ++-------------+-----------+---------+------------+--------------+ +``` + +**要求1:** 查询大国 的国家名称、人口和面积。 + +如果一个国家满足下述两个条件之一,则认为该国是 大国 : + +- 面积至少为 300万平方公里(即,3000000 km2) + +- 人口至少为 2500 万(即 25000000) + +**要求2:**查询属于亚洲的国家名称、所属大陆、面积、人口和 GDP 值 + +**要求3:**查询国土面积不足1万平方公里且人口不走10万人的国家信息 + +**要求4:**查询国家名字中包含“o“字母的国家信息 + +**要求5:**查询GDP值超过10000000000的国家信息 + +**要求6:**查询每个国家的人均贡献GDP值(GDP/人口总数)并显示为“国家名、人口、GDP值、人均贡献GDP值” + +**要求7:**查询人均贡献GDP值低于1000的国家信息。 + +**要求8:**查询每个国家的人均国土面积(面积/人口总数)并显示为“国家名、面积、人口、人均国土面积值” + +```mysql +create database s0222; +use s0222; + +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,population,area 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 国家名,population 人口, gdp DGP值,gdp/population 人均贡献GDP值 from countries_info; +# **要求7:**查询人均贡献GDP值低于1000的国家信息。 +select * from countries_info where (gdp/countries_info.population)<1000; +# **要求8:**查询每个国家的人均国土面积(面积/人口总数)并显示为“国家名、面积、人口、人均国土面积值” +select name 国家名,area 面积,population 人口,area/population 人均国土面积值 from countries_info; +``` + +## 查询练习第一题 + +```mysql +create database s0222; +use s0222; +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'); +# (1)题目:从用户信息表中取出学校的去重数据 +select distinct university from user_profile; +# (2)题目:查看用户明细设备ID数据,并将列名显示为 'user_infos_example' +select device_id user_infos_example from user_profile; +## (3)题目:查询university是北京大学的设备ID +select device_id from user_profile where university='北京大学'; +## (4)题目:查询年龄大于24用户的设备ID、性别、年龄、学校 +select id,gender,age,university from user_profile where age>24; +## (5)题目:查询所有用户的设备id、性别、年龄、学校 +select 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 distinct province from user_profile; + +``` + +## 运算符练习第二题 + +```mysql +create database s0222; +use s0222; +# 第1题:user_profile表脚本1 +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'); +# (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 +create database s0222; +use s0222; +# 第2题:user_profile表脚本2 +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); +# (4)题目:查询男性且GPA在3.5以上(不包括3.5)的用户的设备ID,性别、年龄、学校、gpa +select id,device_id,gender,age,university,gpa from user_profile where gpa>3.5 and gender='male'; +# (5)题目:查询学校为北大或GPA在3.7以上(不包括3.7)的用户的设备ID,性别、年龄、学校、gpa +select device_id,gender,age,university,gpa from user_profile where university='北京大学' or gpa>3.7; +# (6)题目:查询学校为北大、复旦和山大用户的设备ID,性别、年龄、学校、gpa +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)的复旦大学同学 +select * from user_profile where (gpa>3.5 and university='山东大学') or (gpa>3.8 and university='复旦大学'); +# (8)题目:所有大学中带有北京的用户信息 +select * from user_profile where university like "%北京%"; +``` + -- Gitee