学习MySQL有一段时间了,写下这篇博客,一来为了总结笔记方便以后查阅,二来希望和大家交流,有不对的地方希望各位批评指正。(本文中用到的数据库下载:https://download.csdn.net/my)
#######################################################################################################
首先我想说一下这张表,这是power node 那个老师讲的,真的对初学者很有用(表:以上改为以下)
1、简单的查询
a) 单字段 select 字段名 from 表名; select ename from emp;
b) 多字段 select 字段名,字段名 ,... from 表名; select empno, ename from emp;
c) 查询全部字段 select * from 表名; select * from emp;
2、条件查询(稍微复杂一点点)
select 字段名 from 表名 where 条件;
翻译:从某表中在某条件下,查询某些内容;
where后面可以有哪些操作呢?如下:
运算符 |
说明 |
= |
等于 |
<>或!= |
不等于 |
< |
小于 |
<= |
小于等于 |
> |
大于 |
>= |
大于等于 |
between … and …. |
两个值之间,等同于 >= and <= |
is null |
为null(is not null 不为空) |
and |
并且 and的优先级比or高 |
or |
或者 |
in |
包含,相当于多个or(not in不在这个范围中) |
not |
not可以取非,主要用在is 或in中 |
like |
like称为模糊查询,支持%或下划线匹配 %匹配任意个字符 下划线,一个下划线只匹配一个字符 |
上表中的操作,挑几个说说:
a) MySQL中如果一个字段为字符型,那么对其操作时需要加单引号或双引号:
select empno, ename from emp where job=’manager’; 或
select empno, ename from emp where job="manager";
b) 说一下 between ... and ..... # 格式 between 小 and 大
查询薪水为1600到3000的员工
select empno, ename, sal from emp where sal >= 1600 and sal <= 3000;
select empno, ename, sal from emp where sal between 1600 and 3000;
c) is null
Null为空,但不是空串,为null可以设置这个字段不填值,如果查询为null的字段,采用is null
查询津贴为空的员工
select * from emp where comm is null;
错误写法:select * from emp where comm=null;
总结:也就是说判断是否为null必须用 is 不能用 = 。
d) in 比较重要,来聊聊
比较普通的用法,in后面接具体的内容:
查询出job为manager或者job为salesman的员工
select * from emp where job in ('manager','salesman');
查询出薪水包含1600和薪水包含3000的员工
select * from emp where sal in(1600, 3000);
稍微有点意思的用法,in后面接查询语句
找出学过1号课程的且学过2号课程的学生
select
s.sname
from
s join sc on s.sno = sc.sno
where
sc.cno = 1 and s.sname in ( select s.sname from s join sc on s.sno = sc.sno where sc.cno = 2);
你看看这里,in后面接的是什么? 其实后面接的是一个表。
e) not 没有太多好说的
not in select * from emp where sal not in (1600, 3000);
is not null select * from emp where comm is not null;
f) 最后说一下 like
Like可以实现模糊查询,like支持%和下划线 _ 匹配
查询姓名以M开头所有的员工; select * from emp where ename like 'M%';
查询姓名中包含O的所有的员工; select * from emp where ename like '%O%';
查询姓名中第二个字符为A的所有员工; select * from emp where ename like '_A%';
总结:
第一条:Like 中的表达式必须放到单引号中偶or双引号中;
第二条:%表示匹配任意个数字符;
第三条:下划线 _ 表示只匹配一个字符;
3、分组查询
分组查询主要涉:group by . . . having . . . ; 按照 。。。分组,如果分组后还不满意加having 后面接限制条件
a) 分组查询及多字段分组查询
取得每个工作岗位的工资合计,要求显示岗位名称和工资合计
select job, sum(sal) from emp group by job;
按照工作岗位和部门编码分组,取得的工资合计
select job,deptno, sum(sal) from emp group by job,deptno;
注意:从这个语句中你能看出啥?
i1) group by 后面能跟多个字段,这样接的含义表示,将表emp按照job分组,分完之后在每个job组里面再按照 deptno分组;
i2) 如果使用了order by,order by必须放到group by后面;
即: select job, sum(sal) from emp group by job order by job ;
b) 分组查询后不满意+having
使用场景 :如果需要对分组数据再进行过滤需要使用having子句
找出岗位的平均工资大于2000的岗位
select job, avg(sal) from emp group by job having avg(sal) >2000;
c) 说到分组查询后,不得不说的一个东西——分组函数(或者叫聚合函数、多行处理函数)
来来来,说分组函数前先看一个例子:
emp group by job(当然这个写法是错的,我这里只是想表示,对emp表按照job分组,分组结果示意图如下)
从上图可以看出,分组后每一种工作都有一个新的表了(只是这样理解) ;
如果我们不做任何处理,直接显示全部的分组结果会是怎样的呢?
select * from emp group by job;
从上图中可以看出,它只是显示了每个组中的第一条记录;
所以说我们在分组后,是不能完全的显示出每组的所有记录的(也许有只是我不知道),但是可以用分组函数,去求得每组的一些特征,其实我个人感觉,分组的一个很重要的目的就是使用分组函数,去获得每组的一些特征。
好好好,现在说说分组函数:
count |
取得记录数 |
sum |
求和 |
avg |
取平均 |
max |
取最大的数 |
min |
取最小的数 |
注意:
a) 分组函数自动忽略空值,不需要手动的加where条件排除空值。
select count(comm) from emp; comm这个字段中不为空的元素总数。
b) 分组函数不能直接使用在where关键字后面; 其实就是说一般用在select和having后面
select avg(sal) from emp; #### select max(sal) from emp; ##### select min(sal) from emp;
组合分组函数 select count(*),sum(sal),avg(sal),max(sal),min(sal) from emp;
说白了,其实分组函数就是对一个字段多个数据进行处理。