注释
select * 字段名字1(as)别名,字段名字2 别名2… from 表名 别名;select 后的内容称为结果集
–查询所有的员工编号和员工名称,员工上级的编号
–要查询的数据:empno,ename,mgr
–数据的来源:emp
–条件:无
select empno,ename,mgr from emp;
distinct 去重
查询出所有员工所在的部门的部门标号,每个部门编号只能出现一次
select distinct deptno from emp;
按条件查询 过滤行记录
select … from … where 过滤行记录条件;
执行顺序:from where select 先拿出一条数据来判断是否满足where的条件,如果满足就保留在结果集中给获取相对应字段的值
select * from emp where not deptno = 20;—not 取反
or and not (或,且,取反)
like 模糊匹配 经常用于与%,_ 配合使用
% 任意个任意字符
_ 一个任意字符
查询员工姓名中包含子符A的员工信息
select * from emp where ename like’%A%’;
查询员工姓名中第二个字母为A的员工信息
select * from emp where ename like ‘_A%’;
emp.deptno 值代每一次从emp表中拿出的这条要比较的数据的部门编号
排序 select * 字段1 别名,字段2 别名 … from 数据来源 where 行过滤条件 order by 排序字段1 desc,排序字段2(asc)…nulls first | last;
order by … desc;
from—where—select----order by
组函数 | 多行函数 | 聚合函数:多条记录返回一个结果 对以确定的结果集使用组函数
count()统计 sum() 和 max() 最大 avg()平均数
如果select 后一旦有使用组函数,只能和其他组函数,获取分组字段
分组 group by 分组字段,分组字段
select 查询的数据 from 数据来源 where 行过滤条件 group by 分组字段 having 组过滤信息 order by 排序字段;
顺序:from—where—group by—having—select—order by
where 中不能使用组函数

被折叠的 条评论
为什么被折叠?



