SQL查询
SQL查询主要分为全查询、部分查询、条件查询、模糊查询等。
数据声明
1.员工信息表
包含:员工编号、员工姓名、岗位工作、上级领导编号、入职日期、工资、奖金、部门编号。
2.部门信息表
包含:部门编号、部门名称、部门地址。
一、全查询
全查询可以查询表中所有数据字段,返回emp表中存储的所有信息。
1. select * from emp;
2. select emp.* from emp;
3. select e.* from emp e;
二、部分查询
根据自生需求,按需查找表中对应字段的数据。
例如查询emp员工表中员工的编号、姓名、工作字段信息。
select empno,ename,job from emp;
select e.empno,e.ename,e.job from emp e;
三、条件查询
在进行表数据查询时,添加相应的限定条件,对查询的信息进行过滤,以达到更加准确的结果。
语法: 表名后面+where+限定条件(and或者or)。
1.算术运算符(+、 -、 *、 /)-条件查询
1.select * from emp where sal>500+2000;
2.select * from emp where (sal-500)>2000;
3.select ename,job,(sal*12) year_sal from emp where sal>2000;
4.select ename,job,(sal/22) day_sal from emp where sal>2000;
2.关系运算符(=、>、>=、<、<=、!=、<>)-条件查询
1.select * from emp where sal<=2000;
2.select * from emp where ename='scott';
3.select * from emp where sal=2800;
4.select * from emp where sal<>2000;
5.select * from emp where sal!=2000;
3.逻辑运算(and、or、not)执行顺序(not > and > or)-条件查询
1.select * from emp where sal<2000 and sal>1500;
2.select * from emp where sal<2000 or comm>300;
3.select * from emp where not sal<2000;
4.select * from emp where not deptno=20 and deptno=30;--结果(30)
5.select * from emp where not (deptno=20 and deptno=30);--判断条件内部为假,not后为真==全查
6.select * from emp where not (deptno=20 or deptno=30);--结果(10)
7.select * from emp where not deptno=20 and deptno=30 or deptno=10;--结果(10,30)
8.select * from emp where not deptno=20 and deptno=30 or deptno=10 and deptno=20;--结果(30)
9.select * from emp where not deptno=20 and deptno=30 or (deptno=10 and deptno=20);--结果(30)
10.select * from emp where deptno=20 and deptno=30 or deptno=10;--前面为假,结果为(10)
11.select * from emp where deptno=20 or deptno=30 and deptno=10;--后面为假,结果为(20)
4.空值null(is null、is not null)-空值查询
1.select * from emp where comm is null;
2.select * from emp where comm is not null;
5.区间查询(between and)等价于 >= and <=
语法:between 小值 and 大值
1.select * from emp where sal between 2500 and 3000;
2.select * from emp where sal>=2500 and sal<=3000;
6.IN()查询,包含查询
语法:IN(值1,值2,值3…)
等价于:-or-
等价于:any(值1,值2,,)
1.select * from emp where job in('ANALYST','MANAGER','CLERK');
2.select * from emp where job='ANALYST' or job='MANAGER' or job='CLERK';
3.select * from emp where job=any('ANALYST','MANAGER','CLERK');
7.NOT IN()查询
语法:IN(值1,值2,值3…)
等价于: !=值1 and !=值2 and!=值
等价于:!=all()
1.select * from emp where job not in('ANALYST','MANAGER','CLERK');
2.select * from emp where job!='ANALYST' and job!='MANAGER' and job!='CLERK';
3.select * from emp where job!=all('ANALYST','MANAGER','CLERK');
四、模糊查询 LIKE
语法: where 字段 like ‘通配符和数据’(有多个用and或or)
1.字段值可以是字符型或者数字类型,不可以是日期类型
2.通配符:
_ 表示此处位置占据具体的一位;
% 表示0位或者多位。
select * from emp where ename like '_A%';
select * from emp where ename like '_A%' or ename like '_C%';
select * from emp where ename like '%M%S%';
select * from emp where empno like '%77%';
--查询出员工名称包含%的员工数据信息 用到转义符 \
select * from emp where ename like '%%%'; --查询条件错误
select * from emp where ename like '%\%%' escape '\'; --指明转义符为'\',转义的字符为 %
--日期转换成普通字符串,匹配查询
select e.* from emp e where to_char(HIREDATE,'YYYYMMDD') like '1981%';
select * from emp where ename like '_A%';
--查询员工名字是4个字母的员工信息
select * from emp where ename like '____';
select * from emp where length(ename)=4;
五、子查询
注意:
- 子查询的select语句不要带分号
- 子查询的select语句用括号括起来
1.子查询结果当条件用
select *
from emp e
where deptno=(select deptno from emp where ename='SCOTT') --将查询结果部门编号当成条件使用
and e.ename!='SCOTT';
2.子查询结果当表用
SELECT D.*,F.*
FROM DEPT D
inner join (SELECT * FROM EMP E WHERE E.ENAME = 'SCOTT') F --当成临时表用,进行表关联
on D.DEPTNO = F.DEPTNO;
in()和 exists()
- exists() 存在为真 , 逻辑:先执行主查询,然后执行子查询
- in() 逻辑:先执行子查询,然后执行主查询
- not exists() 不存在则为真
例:查询SMITH上班地点
--用in()
select f.loc from dept f
where f.deptno in(select deptno from emp where ename='SMITH'); --DALLAS
--用exists()
select f.loc from dept f --主查询
where exists(select 1 from emp e where e.ename='SMITH' and e.deptno=f.deptno); --DALLAS
查出dept表有的但是emp表却没有的部门编号来
--not exists()
select f.deptno from dept f
where not exists(select 1 from emp e where e.deptno=f.deptno); --40
--exists() --错误方式
select f.deptno from dept f
where exists(select 1 from emp e where e.deptno!=f.deptno);--不能用!=;
总结一下:
- 在条件不明确时候用到子查询
- 子查询的结果,可以当做比较的条件来使用
- 子查询的结果,也可以当做一张新的临时表来使用