网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
- 1
例1: 本命年员工
select id,name from employee where age in (24,36,48,60);
- 1
例2: 某列表中员工 (char varing[]类型不支持)
select \* from employee where name in ('张三','李四','王五');
- 1
2.05 范围查询--BETWEEN AND 的用法
语法:在值A到B之间(包括A,B)
select \*|字段名 from 表名 where 字段 between A and B;
- 1
例1:
select name from employee where age between 18 and 20;
- 1
2.06 模糊查询--LIKE 的用法
语法:字段包含字符串strA的记录,一般搭配 % 匹配任意字符。
select \*|字段名 from 表名 where 字段 like strA;
- 1
例1:所有姓张的,(char varing[]类型不支持)
select \* from employee where name like '张%';
- 1
例2:所有山东人,(char varing[]类型不支持)
select \* from employee where hometown like '%山东%';
- 1
2.07 多条件查询--AND 、 OR 的用法
语法:and并且关系,or或者关系
select \*|字段名 from 表名 where 字段1 条件1 and 字段2 条件2;
- 1
select \*|字段名 from 表名 where 字段1 条件1 or 字段2 条件2;
- 1
例1: 四十岁以上(且)姓张的.
select \* from employee where name like '张%' and age >=40;
- 1
例2: 四十岁以上或者薪水过万的.
select \* from employee where salary >=10000 or age >=40;
- 1
2.07x 多表查询
语法:与条件联合使用,否则将造成 m*n 倍增.m,n为记录数.
select 表名1.字段1,别名2.字段2 from 表名1,表名2 [别名2] <条件>;
- 1
例:
select u.name,t.name from user u,team t where t.id=u.team\_id;
- 1
2.08 空值查询 IS NULL
语法: IS NULL
select \*|字段名 from 表名 where 字段1 条件1 and 字段2 条件2;
- 1
例2: 邮箱为空的.
select \* from employee where email is null;
- 1
2.09 分组查询:--GROUP BY的用法
GROUP BY 查询分组情况
/不会——————————-
语法: GROUP BY 字段1,按 字段1 分组。HAVING+条件 可以对分组进行筛选。
select \*|字段名 from 表名 group by 字段;
- 1
例1: 按性别分组.
select \* from employee group by sexy;
- 1
例1: .
select id,name,age from employee group by age having count(age)>=10;
- 1
ERROR*
column “mytable.ename” must appear in the GROUP BY clause or be used in an aggregate function
——————————-不会/
添坑:
事实上group by 会将select <字段1> 结果集中<字段1>相同的记录合并,
所以,select <字段> 指定的字段必须
- 放入group by后,或者
- 放入having后聚合函数中.
而且,group by 一般用来统计记录的分组情况,比如:分组,种类,部门.
结合having 聚合
select esex,count(\*) as total from employee group by esex order by esex DESC;
- 1
esex | total |
---|---|
F | 19 |
M | 23 |
2.10 查询结果:去重复--DISTINCT的用法
DISTINCT <字段> ,返回<字段>不同的记录
select distinct \*|单字段名 from 表名;
- 1
例1:
select distinct \*|单字段名 from 表名;
- 1
2.11 查询结果:排序--ORDER BY 的用法
ORDER BY 对查询结果排序
语法: ASC升序、DESC降序。[ASC|DESC]
select \*|字段名 from 表名 order by 字段1 ASC|DESC;
- 1
例1:按年龄增排序;查询所有员工.
select \* from employee order by age ASC;
- 1
例2:与GROUP BY联用;按性别分组,按年龄排序.
select \* from employee group by sex order by age ASC;
- 1
2.12 查询结果:限制数量--LIMIT 的用法
语法: LIMIT A [OFFSET B]; 限制查询数量A个,偏移量B个.(从第B条往后A条数据)
select \*|字段名 from 表名 limit A offset B;
- 1
例1:每页100条数据,偏移量300。(相当于第4页)
select \* from employee limit 100 offset 300;
- 1
三、聚合函数查询
聚合函数
函数 | 描述 |
---|---|
COUNT() | 统计某列的行数,*则是表的字段数,指定列某记录为null时此条将被忽略 |
AVG() | 统计某列的平均值,多列需要多次使用avg() |
MAX() | 求某列的最大值,支持日期和字符串比较 |
MIN() | 求某列的最小值,同max() |
SUM() | 求某列的和,指定列某记录值为null时此条将被忽略 |
3.01 COUNT()函数
语法: [as str] 别名str (放到 str 列中)
select [<字段>,]count(字段) [as str] from 表名;
- 1
例1:统计所有数据数量
select count(\*) as total from employee;
- 1
例2:按性别统计员工数量
select esex,count(\*) as total from employee group by esex;
- 1
3.02 SUM()函数
语法: [as str] 别名str (放到 str 列中)
select [<字段>,]sum(字段) [as str] from 表名;
- 1
例1:统计薪水总和
select sum(salary) as total from employee;
- 1
例2:统计不同性别的薪水情况
select sex,sum(salary) as total from employee group by sex;
- 1
3.03 AVG()函数
语法: [as str] 别名str (放到 str 列中)
select [<字段>,]avg(字段) [as str] from 表名;
- 1
例1:统计年龄平均值
select avg(age) as average from employee;
- 1
例2:统计不同性别的年龄平均值
select sex,avg(age) as total from employee group by sex;
- 1
3.04 MAX()函数
语法: [as str] 别名str (放到 str 列中)
select [<字段>,]max(字段) [as str] from 表名;
- 1
例1:统计薪水最大值
select max(salary) as maximum from employee;
- 1
例2:统计不同性别的薪水最大值
select sex,max(salary) as maximum from employee group by sex;
- 1
3.05 MIN()函数
同MAX()的用法。
四、连接查询
4.01 内连接查询
- 1
4.02 外连接查询
4.03 复合条件连接查询
五、子查询
5.01 ANY、SOME
满足任意一个即可,any some 等价
where id > any (1,2,3)
where age > some (select age from table2…)
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
4.01 内连接查询
- 1
4.02 外连接查询
4.03 复合条件连接查询
五、子查询
5.01 ANY、SOME
满足任意一个即可,any some 等价
where id > any (1,2,3)
where age > some (select age from table2…)
[外链图片转存中…(img-ILRwAhJm-1715816329946)]
[外链图片转存中…(img-SeqBPwch-1715816329947)]
[外链图片转存中…(img-8yTjp8Kb-1715816329947)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新