diff --git "a/14 \345\256\213\345\256\217\346\211\254/20230222\345\205\263\344\272\216\350\277\220\347\256\227\347\254\246\347\232\204\347\254\224\350\256\260\345\217\212\344\275\234\344\270\232.md" "b/14 \345\256\213\345\256\217\346\211\254/20230222\345\205\263\344\272\216\350\277\220\347\256\227\347\254\246\347\232\204\347\254\224\350\256\260\345\217\212\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..9de69b5da5d2c2ff1edff6b966be72ec0cfc4251 --- /dev/null +++ "b/14 \345\256\213\345\256\217\346\211\254/20230222\345\205\263\344\272\216\350\277\220\347\256\227\347\254\246\347\232\204\347\254\224\350\256\260\345\217\212\344\275\234\344\270\232.md" @@ -0,0 +1,194 @@ +# 运算符 + +- 别名:给某个字段或表达式计算结果或表取个临时的名称 + + ```mysql + select 字段1 as "别 名1",字段2 as 别名2 from 表名 as 别名; + -- 当列的别名有空格时一定要加引号,否则可加可不加; + -- 表的别名不能有引号也不能有空格; + -- as可以省略不写 + ``` + +- 结果去重: + + ```mysql + select distinct 字段列表 from 表名 [where 条件] order by 去重字段; #加desc降序排列 + ``` + +- ifnull(字段名,代替值):当字段的值为null时用代替值进行计算 + +## 1、算数运算符 + +类型:+、-、*、/、div、%、mod + +​ /:数学中的除 + +​ div:只保留商的整数部分 + +​ %:求余 x%y + +​ mod:求余 末端(x,y) + +## 2、比较运算符 + +类型:>、<、=、>=、<= + +​ 不等于:!= 或 <> + +​ 判断是null:is null 或者 <=> null + +​ 判断不是null:is not null + +## 3、区间或集合范围运算符 + +区间范围:在x到y的区间范围内—— between x and y + +​ 不在x到y的区间范围内—— not between x and y + +集合范围:在集合范围—— in(x,y,z) + +​ 不在集合范围—— not in(x,y,z) + +注:between...and... 结果包含两端的边界 + +## 4、模糊匹配比较运算符 + +%:表示任意个字符 + +_:表示一个字符,有几个字符就输几个 + +```mysql +select * from 表名 where 字段名 like '%宋%'; #查询数据中包含‘宋’字的 +select * from 表名 where 字段名 like '宋%'; #查询数据中‘宋’字开头的 +select * from 表名 where 字段名 like '%宋'; #查询数据中‘宋’字结尾的 +select * from 表名 where 字段名 like '_宋%'; #有‘宋’字单‘宋’前面只有一个字 +``` + +查询当前数据库的字符集情况:show variabes like '%character%'; + +## 5、逻辑运算符 + +与:&& 或 and + +或:|| 或 or + +非:! 或 not + +异或:xor (只能满足其中一个条件) + +# 作业 + +## 第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 | + +```mysql +-- **要求1:**查询出薪资在12000~13000之间的员工信息。 +select * from employee where salary >= 12000 and salary <= 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 salary > 12000 or addr = '广东深圳' or addr = '广东广州'; +-- **要求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 | ++-------------+-----------+---------+------------+--------------+ +``` + +```mysql +-- **要求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 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; +``` +