SQL
- 条件查询
判断语句
判断条件:
a)、= 、 >、 <、 >=、 <=、 !=、 <>、 between and
b)、and 、or、 not、 union、 union all、 intersect 、minus
c)、null :is null、 is not null、 not is null
d)、like :模糊查询 % _ escape(‘单个字符’)
查询 any(任意一个) some(任意一个) all(所有)
all 大于所有
some >any 大于最小的
假设现在 900,1000,1100
查询工资比我们三个人都高的那些员工的信息
select * from emp where sal>all(900,1000,1100);
- Union
,并集(去重) 对两个结果集进行并集操作,不包括重复行同时进行默认规则的排序;
Union All,全集(不去重) 对两个结果集进行并集操作,包括重复行,不进行排序 ;
Intersect,交集(找出重复) 对两个结果集进行交集操作,不包括重复行,同时进行默认规则的排序;
Minus,差集(减去重复) 对两个结果集进行差操作,不包括重复行,同时进行默认规则的排序
查询工资大于1500 或有奖金的人员姓名
select ename from emp where sal>1500
Union
select ename from emp where comm is not null;
--查询显示不存在雇员的所有部门号。
select deptno from dept Minus select distinct deptno from emp;
对某个字符串值进行匹配 like(效率较低)
结合_ %一起使用 _一个任意字符 %任意个任意字符
```sql
--查询员工姓名中包含字符A的员工信息
select * from emp where ename like '%A%';
--查询员工姓名中第二个字母为A的员工信息
select * from emp where ename like '__A%';
- 排序
排序 order by
select 查询数据 from 数据来源 where 行过滤条件 order by 排序字段1 desc(降序),排序字段2 asc(升序)…;
执行流程: from --where --select–order by
--查询所有员工的薪资,降序排序
select * from emp order by sal; --默认升序
select * from emp order by sal desc;
- 单行函数
日期字符串转换
--to_date(日期字符串,日期转换模板) 'yyyy-mm-dd hh24:mi:ss'
select to_date('2020-0-45 00:00:00','yyyy-mm-dd hh24:mi:ss') from dual;
-- 将日期转为特定格式的字符串 to_char
select to_char(sysdate,'yyyy"年"mm"月"dd hh24:mi:ss') from dual;
判定函数
decode(判定字段,值1,结果1,值2,结果2…,默认值) 根据字段的值判定函数最终的结果,如果以上值判定都不相等,给默认值
- 分组 group by
–select 查询数据 from 数据来源 where 行过滤条件 group by 分组字段1,分组字段2… having 组过滤条件 order by 排序字段…;
–执行流程: from – where – group by – having – select – order by–注意: 如果一旦分组,select后只能为分组字段或者组函数
-- 找出20部门和30部门的最高工资
select max(sal) from emp where deptno in(20,30);
select max(sal),deptno from emp group by deptno having deptno in (20,30); --先分组后过滤
select max(sal),deptno from emp where deptno in (20,30) group by deptno; --先过滤后分组