From e73a51283661c4396f5367e50c1dff8ed1443650 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=9D=B0?= <3242096341@qq.com> Date: Wed, 22 Feb 2023 23:51:33 +0800 Subject: [PATCH] =?UTF-8?q?=E9=99=88=E6=9D=B0=E7=9A=84=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\345\222\214\344\275\234\344\270\232 .md" | 242 ++++++++++++++++++ 1 file changed, 242 insertions(+) create mode 100644 "07 \351\231\210\346\235\260/20230222 SELECT\350\257\255\345\217\245\347\232\204\347\254\224\350\256\260\345\222\214\344\275\234\344\270\232 .md" diff --git "a/07 \351\231\210\346\235\260/20230222 SELECT\350\257\255\345\217\245\347\232\204\347\254\224\350\256\260\345\222\214\344\275\234\344\270\232 .md" "b/07 \351\231\210\346\235\260/20230222 SELECT\350\257\255\345\217\245\347\232\204\347\254\224\350\256\260\345\222\214\344\275\234\344\270\232 .md" new file mode 100644 index 0000000..78e430d --- /dev/null +++ "b/07 \351\231\210\346\235\260/20230222 SELECT\350\257\255\345\217\245\347\232\204\347\254\224\350\256\260\345\222\214\344\275\234\344\270\232 .md" @@ -0,0 +1,242 @@ +# 笔记 + +## SELECT 单表语句 + +获取电脑时间: + +```mqsql +select now(); +``` + +\# mysql 没有布尔类型,用1表示true 用0表示false + +```mysql +> select 2<1; +> 1 +``` + +##### 查看某个表的数据 + +``` +select * from 表名称; #前提是选择好了,针对哪个库操作,否则会报 No database selected错误 ,相对路径 +select * from 数据库名.表名称; 相当于使用了绝对路径 +``` + +##### 查看某些列的数据 + +``` +select 字段列表 from 【数据库名.】表名称; +例如: +select eid,ename,salary from mxdx.t_employee; +``` + +##### 查看某些行/列的数据 + +``` +select * from 表名称 where 某些行/列的条件 +``` + +select 查询得到的结果,是一个临时表,不影响原来表。类似抄了一份 + + + +##### 给查询结果 列 取别名 + +``` +select 字段名 [as] 别名 from 表名; +例如: +select eid as cid,ename name,salary gz from t_employee where ename = '蔡徐坤'; +``` + +| 编号(cid) | 姓名(name) | salary(gz) | +| :---------: | :----------: | :----------: | +| 19 | 蔡徐坤 | 7879 | + + + +##### 给表取别名 (不要用引号) + +有什么用?跨表联合查询时用得上 + +``` +select * from 表名 as 别名; +``` + + + +##### 关键字:distinct + +mysql可以在查询结果中使用**distinct**关键字去重。 + +```MySQL +select distinct 字段列表 from 表名称 [where 条件]; +``` + +distinct 只能放在字段的前面 + +``` +select distinct work_place,ename from t_employee ; + +select work_place,ename from t_employee ; +``` + + 只能第一个字段生效,所以通常只在查询一个字段的时候使用 + + + +## 运算符 + +##### 算术运算符 + +``` +> SELECT 9/2; +> 4.5 +``` + +``` +> SELECT 9 DIV 2; +> 4 只取整数部分 +``` + +``` +> SELECT 9%2; 取余 +> 1 +``` + +等于:= 不能用于null判断 id = null 因为null是特殊类型。它与任何类型做运算,结果都是null + +不等于:!= 或 <> 不能用于null判断 !在运算表示取相反值 + +<=> ,is 可以判断null,其不可参与运算,否则返回空 + + + +##### 区间范围 + +``` +(not)between x and y; +包含两端 between 10 and 20 [10,20] 例:10<= 值 <=20 +``` + +##### 集合范围 + +``` +(not)in (x,x,x) ; +只要在这个范围就行 例:in (1,2,3) +#查询不是1,2,3部门的员工 +select * from t_employee where !(did in (2,3,4)); +select * from t_employee where did not in (2,3,4); +``` + +##### like + +%:代表任意个字符 0个 1个 N个 + +_:代表一个字符,如果两个下划线代表两个字符 + +```mysql +查询名字中包含3个字,最后一个是“雨”字 的员工 +select * from t_employee where ename like '_雨'; +``` + +##### 逻辑 与、或、非、异或 + +逻辑与:&& 或 and 并且 + +逻辑或:|| 或 or 或者 + +逻辑非:! 或 not 不是 (取反) + +逻辑异或: xor 两者只能满足其中一个,不能同时满足,不能同时不满足 + + + + + +# 作业 + +#### 第一题 + +```MySQL +create database test01 charset utf8; +use test01; +#创建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 表名称 set 字段名 = 值, 字段名 = 值 where 条件; #给满足条件的行修改 +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 as 姓名,salary*12 as 年薪 from employee ; +``` + +#### 第二题 + +```mysql +create database test02 charset utf8; +use test02; +CREATE TABLE `countries_info`( + `name` VARCHAR(100) comment'国家名称', + `continent` VARCHAR(100) comment'所属大陆', + `area` INT comment'面积', + population INT comment'人口', + gdp BIGINT comment'GDP 值' +); + +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`; +#**要求1:** 查询大国的国家名称、人口和面积。 +#如果一个国家满足下述两个条件之一,则认为该国是 大国 : + +#- 面积至少为 300万平方公里(即,3000000 km2) +#- 人口至少为 2500 万(即 25000000) +select `name`,continent,area,population,gdp from `countries_info` where area >=3000000 or population >=25000000; + +#**要求2:**查询属于亚洲的国家名称、所属大陆、面积、人口和 GDP 值 +select `name`,continent,area,population,gdp 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 gdp/population as 国家名、人口、GDP值、人均贡献GDP值 from `countries_info`; +#**要求7:**查询人均贡献GDP值低于1000的国家信息。 +select * from `countries_info` where gdp/population <1000; +#**要求8:**查询每个国家的人均国土面积(面积/人口总数)并显示为“国家名、面积、人口、人均国土面积值” +select area/population as 国家名、面积、人口、人均国土面积值 from `countries_info`; +``` + -- Gitee