diff --git "a/31 \346\235\216\346\254\243/20230222 Select&\350\277\220\347\256\227\347\254\246.md" "b/31 \346\235\216\346\254\243/20230222 Select&\350\277\220\347\256\227\347\254\246.md" new file mode 100644 index 0000000000000000000000000000000000000000..e043ed4fc1402c47f0f2d34053910dad55ad2c8d --- /dev/null +++ "b/31 \346\235\216\346\254\243/20230222 Select&\350\277\220\347\256\227\347\254\246.md" @@ -0,0 +1,302 @@ +# 作业 + +## 第1题:员工表 + +| **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 | + +```sql +create database front charset utf8; +use front; +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 && salary<=13000; +-- **要求2:**查询出姓“刘”的员工的工号,姓名,家庭住址。 +select id,name,addr from employee where name like '刘%'; +-- **要求3:**将“李四”的家庭住址改为“广东韶关” + update employee set addr='广东韶关' where name='李四'; +-- **要求4:**查询出名字中带“小”的员工 +select name from employee where name like '%小%'; +-- **要求5:**查询出薪资高于11000的男员工信息 + select * from employee where salary>11000 and sex='男'; +-- **要求6:**查询没有登记电话号码的员工 +select name from employee where tel is null; +-- **要求7:**查询薪资高于12000或者家是广东深圳、广州的男员工 +select name from employee where salary>12000 and sex='男'and (addr='广东深圳'or addr='广东广州'); +-- **要求8:**查询每个员工的年薪,显示“姓名、年薪” +select name 姓名,salary*12 年薪 from employee; +``` + +## 第2题:国家信息表 + +countries_info表中存储了国家名称、所属大陆、面积、人口和 GDP 值。 + +```sql ++-------------+-----------+---------+------------+--------------+ +-- | 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 | +-- +-------------+-----------+---------+------------+--------------+ +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 or 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<1000 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; + + +``` + +# DQL + +## SELECT语句 + +```sql +-- 基本语法: +SELECT 常量; +SELECT 表达式; +SELECT 函数; + +-- 例子: +SELECT 1; +SELECT 9/2; +SELECT NOW(); + +-- 筛选数据时: +SELECT 字段列表 FROM 表名称; + +-- 根据条件筛选数据时: +SELECT 字段列表 FROM 表名称 WHERE 条件; +``` + +## 使用别名 + +```sql +-- 结构: +select 字段名1 as "别名1", 字段名2 as "别名2" from 表名称 as 别名; +-- 1.列的别名有空格时,请加双引号。**==列的别名==中没有空格时,双引号可以加也可以不加**。 +-- 2.==**表的别名不能加双引号**==,表的别名中间不能包含空格。 +-- 3.as大小写都可以,as也完全可以省略。 + +-- 例子: +select id "学号",name "姓名" from student; +select id 学号,name 姓名 from student; + +-- 报错: +select id 学 号,name 姓 名 from student; +ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use n +ear '号,name 姓 名 from student' at line 1 +``` + +## 结果去重 + +```sql +-- 结构: +select distinct 字段列表 from 表名称 【where 条件】; + +-- 例子: +select distinct did from t_employee; +``` + +# 运算符 + +## 算术运算符 + +```sql +加:+ + 在MySQL +就是求和,没有字符串拼接 +减:- +乘:* +除:/ div(只保留整数部分) + div:两个数相除只保留整数部分 + /:数学中的除 +模:% mod(余数) +mysql中没有 +=等运算符 + +-- 例子: +select 1+1;#2 +update t_employee set salary = salary+100 where eid=27; +select 9/2, 9 div 2; #4.5000 4 +select 9 % 2, 9 mod 2;#1 +select 9.5 % 1.5 , 9.5 mod 1.5;# 0.5 + + + +``` + +## 比较运算符 + +```sql +大于:> +小于:< +大于等于:>= +小于等于:>= +等于:= 不能用于null判断 +不等于:!= 或 <> 不能用于null判断 +判断是null 用 is null 或 用 <=> null +判断不是null is not null + +-- 例子: +#查询薪资高于15000的员工姓名和薪资 +select ename,salary from t_employee where salary>15000; +#查询薪资正好是9000的员工姓名和薪资 +select ename,salary from t_employee where salary = 9000; +#查询工作地址 work_place不是北京的 +select * from t_employee where work_place != '北京'; +select * from t_employee where work_place <> '北京'; +#查询奖金比例是NULL +select * from t_employee where commission_pct = null; +#实发工资 = 薪资 + 薪资 * 奖金比例 +select ename as 姓名, +salary as 薪资, +commission_pct as 奖金比例, +salary + salary * commission_pct as 实发工资 +from t_employee; +``` + +## 区间或集合范围比较运算符 + +```sql +区间范围:between x and y + not between x and y +集合范围:in (x,x,x) + not in(x,x,x) + between ... and ... 结果包含两端的边界 + + -- 例子: +#查询部门编号为2、3的员工信息 +select * from t_employee where did in(2,3); +#查询薪资在[10000,15000]之间 +select * from t_employee where salary between 10000 and 15000; + +``` + +## 模糊匹配比较运算符 + +```sql +%:代表任意个字符 +_:代表一个字符,如果两个下划线代表两个字符 + + -- 例子: +#查询名字中包含'冰'字 +select * from t_employee where ename like '%冰%'; +#查询名字以‘雷'结尾的 +select * from t_employee where ename like '%远'; +#查询名字以’李'开头 +select * from t_employee where ename like '李%'; +#查询名字有冰这个字,但是冰的前面只能有1个字 +select * from t_employee where ename like '_冰%'; +``` + +## 逻辑运算符 + +```sql +逻辑与:&& 或 and +逻辑或:|| 或 or +逻辑非:! 或 not +逻辑异或: xor + + -- 例子: +#查询薪资高于15000,并且性别是男的员工 +select * from t_employee where salary>15000 and gender='男'; +select * from t_employee where salary>15000 && gender='男'; + +select * from t_employee where salary>15000 & gender='男';#错误得不到值 &按位与 +select * from t_employee where (salary>15000) & (gender='男'); +#查询薪资高于15000,或者did为1的员工 +select * from t_employee where salary>15000 or did = 1; +select * from t_employee where salary>15000 || did = 1; +#查询薪资不在[15000,20000]范围的 +select * from t_employee where salary not between 15000 and 20000; +select * from t_employee where !(salary between 15000 and 20000); +#查询薪资高于15000,或者did为1的员工,两者只能满足其一 +select * from t_employee where salary>15000 xor did = 1; +select * from t_employee where (salary>15000) ^ (did = 1); + +``` + +## 关于null值的问题 + +```sql +#(1)判断时 +xx is null +xx is not null +xx <=> null + +#(2)计算时 +ifnull(xx,代替值) 当xx是null时,用代替值计算 +``` + +## 位运算符 + +```sql +左移:<< +右移:>> +按位与:& +按位或:| +按位异或:^ +按位取反:~ +``` + + +