From 1518578006252b3d73c0e7edf8b60244988f34cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E4=BC=9F=E5=B1=B1?= <2609838563@qq.com> Date: Fri, 24 Feb 2023 20:10:34 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E5=9B=9B=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...14\350\277\220\347\256\227\347\254\246.md" | 494 ++++++++++++++++++ 1 file changed, 494 insertions(+) create mode 100644 "39 \345\247\234\344\274\237\345\261\261/20230224 select\350\257\255\345\217\245\345\222\214\350\277\220\347\256\227\347\254\246.md" diff --git "a/39 \345\247\234\344\274\237\345\261\261/20230224 select\350\257\255\345\217\245\345\222\214\350\277\220\347\256\227\347\254\246.md" "b/39 \345\247\234\344\274\237\345\261\261/20230224 select\350\257\255\345\217\245\345\222\214\350\277\220\347\256\227\347\254\246.md" new file mode 100644 index 0000000..e662b60 --- /dev/null +++ "b/39 \345\247\234\344\274\237\345\261\261/20230224 select\350\257\255\345\217\245\345\222\214\350\277\220\347\256\227\347\254\246.md" @@ -0,0 +1,494 @@ +## SELECT语句 + +SELECT语句是用于查看计算结果、或者查看从数据表中筛选出的数据的。 + +SELECT语句的基本语法: + +``` +SELECT 常量; +SELECT 表达式; +SELECT 函数; +``` + +例如: + +``` +SELECT 1; +SELECT 9/2; +SELECT NOW(); +``` + +如果要从数据表中筛选数据,需要加FROM子句。FROM指定数据来源。字段列表筛选列。 + +``` +SELECT 字段列表 FROM 表名称; +``` + +如果要从数据表中根据条件筛选数据,需要加FROM和WHERE子句。WHERE筛选行。 + +``` +SELECT 字段列表 FROM 表名称 WHERE 条件; +``` + +完整的SELECT语句后面可以跟7个子句,后面会逐一讲解。 + +## 使用别名 + +在当前select语句中给某个字段或表达式计算结果,或表等取个临时名称,便于当前select语句的编写和理解。这个临时名称称为别名。 + +``` +select 字段名1 as "别名1", 字段名2 as "别名2" from 表名称 as 别名; +``` + +- 列的别名有空格时,请加双引号。**==列的别名==中没有空格时,双引号可以加也可以不加**。 +- ==**表的别名不能加双引号**==,表的别名中间不能包含空格。 +- as大小写都可以,as也完全可以省略。 + +### 结果去重 + +mysql可以在查询结果中使用distinct关键字去重。 + +distanct只能放在第一个字段之前 + +``` +select distinct 字段列表 from 表名称 【where 条件】; + +select distinct did from t_employee; +``` + +# 运算符 + +## 算术运算符(掌握) + +``` +加:+ + 在MySQL +就是求和,没有字符串拼接 +减:- +乘:* +除:/ div(只保留整数部分) + div:两个数相除只保留整数部分 + /:数学中的除 +模:% mod + +mysql中没有 +=等运算符 + +#select 表达式 +select 1+1; +update t_employee set salary = salary+100 where eid=27; + +select 9/2, 9 div 2; + +mysql> select 9/2, 9 div 2; ++--------+---------+ +| 9/2 | 9 div 2 | ++--------+---------+ +| 4.5000 | 4 | ++--------+---------+ +1 row in set (0.00 sec) + +select 9.5 / 1.5 , 9.5 div 1.5; + +mysql> select 9.5 / 1.5 , 9.5 div 1.5; ++-----------+-------------+ +| 9.5 / 1.5 | 9.5 div 1.5 | ++-----------+-------------+ +| 6.33333 | 6 | ++-----------+-------------+ +1 row in set (0.00 sec) + +select 9 % 2, 9 mod 2; +select 9.5 % 1.5 , 9.5 mod 1.5; + +select 'hello' + 'world'; +mysql> select 'hello' + 'world'; ++-------------------+ +| 'hello' + 'world' | ++-------------------+ +| 0 | ++-------------------+ +1 row in set, 2 warnings (0.00 sec) +``` + +## 比较运算符 + +``` +大于:> +小于:< +大于等于:>= +小于等于:>= +等于:= 不能用于null判断 +不等于:!= 或 <> 不能用于null判断 +判断是null 用is +判断不是null 用<=> + + +#查询薪资高于15000的员工姓名和薪资 +select ename,salary from t_employee where salary>15000; + +mysql> select ename,salary from t_employee where salary>15000; ++--------+--------+ +| ename | salary | ++--------+--------+ +| 孙洪亮 | 28000 | +| 贾宝玉 | 15700 | +| 黄冰茹 | 15678 | +| 李冰冰 | 18760 | +| 谢吉娜 | 18978 | +| 舒淇格 | 16788 | +| 章嘉怡 | 15099 | ++--------+--------+ +7 rows in set (0.00 sec) + +#查询薪资正好是9000的员工姓名和薪资 +select ename,salary from t_employee where salary = 9000; +select ename,salary from t_employee where salary == 9000;#错误,不支持== #注意Java中判断用==,mysql判断用= + +mysql> select ename,salary from t_employee where salary == 9000; +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 near '== 9000' at line 1 + +#查询工作地址 work_place不是北京的 +select * from t_employee where work_place != '北京'; +select * from t_employee where work_place <> '北京'; + +#查询员工表中部门编号不是1 +select * from t_employee where did != 1; +select * from t_employee where did <> 1; + +#查询奖金比例是NULL +select * from t_employee where commission_pct = null; + +mysql> select * from t_employee where commission_pct = null; #无法用=null判断 +Empty set (0.00 sec) +#mysql中只要有null值参与运算和比较,结果就是null,底层就是0,表示条件不成立。 + +#查询奖金比例是NULL +select * from t_employee where commission_pct <=> null; +select * from t_employee where commission_pct is null; + +#查询“李冰冰”、“周旭飞”、“李易峰”这几个员工的信息 +select * from t_employee where ename in ('李冰冰','周旭飞','李易峰'); + +#查询部门编号为2、3的员工信息 +select * from t_employee where did in(2,3); + +#查询部门编号不是2、3的员工信息 +select * from t_employee where did not in(2,3); + +#查询薪资在[10000,15000]之间 +select * from t_employee where salary between 10000 and 15000; + +#查询姓名中第二个字是'冰'的员工 +select * from t_employee where ename like '冰'; #这么写等价于 ename='冰' +select * from t_employee where ename like '_冰%'; +#这么写匹配的是第二个字是冰,后面可能没有第三个字,或者有好几个字 + +update t_employee set ename = '王冰' where ename = '李冰冰'; + +select * from t_employee where ename like '_冰_'; +#这么写匹配的是第二个字是冰,后面有第三个字,且只有三个字 + +#查询员工的姓名、薪资、奖金比例、实发工资 +#实发工资 = 薪资 + 薪资 * 奖金比例 +select ename as 姓名, +salary as 薪资, +commission_pct as 奖金比例, +salary + salary * commission_pct as 实发工资 +from t_employee; + +#NULL在mysql中比较和计算都有特殊性,所有的计算遇到的null都是null。 +#实发工资 = 薪资 + 薪资 * 奖金比例 +select ename as 姓名, +salary as 薪资, +commission_pct as 奖金比例, +salary + salary * ifnull(commission_pct,0) as 实发工资 +from t_employee; +``` + +## 区间或集合范围比较运算符 + +``` +区间范围:between x and y + not between x and y +集合范围:in (x,x,x) + not in(x,x,x) + between ... and ... 结果包含两端的边界 + +#查询薪资在[10000,15000] +select * from t_employee where salary>=10000 && salary<=15000; +select * from t_employee where salary between 10000 and 15000; + +#查询籍贯在这几个地方的 +select * from t_employee where work_place in ('北京', '浙江', '江西'); + +#查询薪资不在[10000,15000] +select * from t_employee where salary not between 10000 and 15000; + +#查询籍贯不在这几个地方的 +select * from t_employee where work_place not in ('北京', '浙江', '江西'); +``` + +## 模糊匹配比较运算符 + +%:代表任意个字符 + +_:代表一个字符,如果两个下划线代表两个字符 + +``` +#查询名字中包含'冰'字 +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 '_冰%'; + +#查询当前mysql数据库的字符集情况 +show variables like '%character%'; +``` + +## 逻辑运算符 + +``` +逻辑与:&& 或 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值的问题 + +``` +#(1)判断时 +xx is null +xx is not null +xx <=> null + +#(2)计算时 +ifnull(xx,代替值) 当xx是null时,用代替值计算 +``` + +**所有的计算遇到的null都是null** + +``` +#查询奖金比例为null的员工 +select * from t_employee where commission_pct = null; #失败 +select * from t_employee where commission_pct = NULL; #失败 +select * from t_employee where commission_pct = 'NULL'; #失败 + +select * from t_employee where commission_pct is null; #成功 +select * from t_employee where commission_pct <=> null; #成功 <=>安全等于 + +#查询员工的实发工资,实发工资 = 薪资 + 薪资 * 奖金比例 +select ename , salary + salary * commission_pct "实发工资" from t_employee; #失败,当commission_pct为null,结果都为null + +select ename ,salary , commission_pct, salary + salary * ifnull(commission_pct,0) "实发工资" from t_employee; +``` + +-- ## 第1题:员工表 + +```MYSQL +CREATE database niubi charset utf8; +use niubi; +``` + +-- #创建employee表 + +```MYSQL +CREATE TABLE employee( + id INT, +`name` VARCHAR(20), + sex VARCHAR(20), + tel VARCHAR(20), + addr VARCHAR(50), + salary FLOAT + ); +``` + +-- #添加信息 + +```MYSQL +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之间的员工信息。 + +```MYSQL +SELECT *from employee where salary >12000 and salary <13000; +``` + +-- **要求2:**查询出姓“刘”的员工的工号,姓名,家庭住址。 + +```MYSQL +SELECT id,`name`,addr from employee where `name` like '刘%%'; +``` + +-- **要求3:**将“李四”的家庭住址改为“广东韶关” + +```MYSQL +UPDATE employee set addr ='广东韶关' where `name`='李四'; +SELECT * from employee ;-- **要求4:**查询出名字中带“小”的员工 +``` + +```MYSQL +SELECT *from employee where `name` like '%小%'; +``` + +-- **要求5:**查询出薪资高于11000的男员工信息 + +```MYSQL +SELECT *from employee where salary >10000 AND sex ='男'; +``` + +-- **要求6:**查询没有登记电话号码的员工 + +```MYSQL +SELECT *from employee where tel is null; +``` + + +-- **要求7:**查询薪资高于12000或者家是广东深圳、广州的男员工 + +```MYSQL +SELECT *from employee where salary >12000 || (addr ='广东深圳' AND addr ='广州' and sex ='男' ); +``` + + +-- **要求8:**查询每个员工的年薪,显示“姓名、年薪” + +```MYSQL +SELECT `name` as '姓名' , salary as '年薪' from employee; +``` + + + + + +-- ## 第2题:国家信息表 +-- countries_info表中存储了国家名称、所属大陆、面积、人口和 GDP 值。 + +```MYSQL +CREATE database countries_info charset utf8; +use countries_info; +CREATE TABLE `countries_info`( +`name` VARCHAR(100), +`continent` VARCHAR(100), +`area` INT, +population INT, +gdp BIGINT +); +``` + + + +```MYSQL +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); +select * from countries_info ; +``` + +-- 表数据样例: +-- +-------------+-----------+---------+------------+--------------+ +-- | 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:** 查询大国 的国家名称、人口和面积。 + +```MYSQL +SELECT `name`,population, `area` from `countries_info` where `area`>3000000 || population >25000000; +``` + +-- 如果一个国家满足下述两个条件之一,则认为该国是 大国 : +-- - 面积至少为 300万平方公里(即,3000000 km2) +-- - 人口至少为 2500 万(即 25000000) +-- **要求2:**查询属于亚洲的国家名称、所属大陆、面积、人口和 GDP 值 + +```MYSQL +SELECT `name`,`continent`,`area`,population,gdp from countries_info where `continent`='Asia'; +``` + +-- **要求3:**查询国土面积不足1万平方公里且人口不走10万人的国家信息 + +```MYSQL +SELECT *from countries_info WHERE `area`<10000 and population<100000; +``` + +-- **要求4:**查询国家名字中包含“o“字母的国家信息 + +```MYSQL +SELECT *from countries_info WHERE `name` like '%%%o%%%'; +``` + +-- **要求5:**查询GDP值超过10000000000的国家信息 + +```MYSQL +SELECT *from countries_info WHERE gdp>10000000000; +``` + + +-- **要求6:**查询每个国家的人均贡献GDP值(GDP/人口总数)并显示为“国家名、人口、GDP值、人均贡献GDP值” + +```MYSQL +SELECT `name` as '国家名',population as '人口',gdp as 'GDP值', gdp/population as '人均贡献GDP值' from countries_info ; +``` + + +-- **要求7:**查询人均贡献GDP值低于1000的国家信息。 + +```MYSQL +SELECT * from countries_info WHERE gdp/population<1000; +``` + +-- **要求8:**查询每个国家的人均国土面积(面积/人口总数)并显示为“国家名、面积、人口、人均国土面积值” + +```MYSQL +SELECT `name` as '国家名',`area` as '面积',population as '人口',`area`/population as '人均国土面积值' from countries_info ; +``` \ No newline at end of file -- Gitee