数据库修仙之路

:select

Scott用户的四张表

主表:dept部门表

薪资分级表:salgrade工资等级表

从表:emp雇员信息表

命令不区分大小写

:查询列(字段)

1.检索单个列

Select ename from emp; --查询员工信息

检索多个列

select deptno,dname from dept; --查询部门表的deptno,dname字段

2.检索所有列

select *from dept; --查询部门的所有信息

3.去除重复

使用distinct去重,确保查询结果的唯一性 :

Select distinct deptno from emp;

4.别名

select ename 雇员姓名 from emp;

select ename as "雇员 姓名" from emp;

select ename "雇员 姓名" from emp;  --as可以省去,这种方式最实用

select ename as 雇员姓名 from emp;

select ename as " Ename" from emp;

:’’ ’’表示原样输出,可以存在空格和区分大小写

  1. 字符串

select ‘a-’ || ‘-b’ from dept; --字符串的拼接

--查询所有员工的姓名添加同一前缀 尚学堂

select '尚学堂'||ename "学生姓名" from emp;

  1. 伪列

select 1+1 from emp; --表中不存在的列,自动拼接在结果集中

 

7.--虚表: oracle中不是真是存在的表,其中没有任何数据  dual  可以省去去重问题

select sysdate from dual;

select 1 from dual;  --oracle中存在dual

8.null

null 遇到数字参与运算的结果为 null,遇到字符串为空串

--nvl(1,2) 如果值1null,函数运算的结果是值2,如果值1不为null,结果就是值1

select ename,nvl(comm,1) from emp;

:查询行(记录)  

执行顺序:先走from再走where最后走select

a)= ><>=<=!=<>between and

b)and ornotunionunion allintersect minus

c)null :is nullis not nullnot is null

d)like :模糊查询 % _ escape('单个字符')

f)in exists(难点) 及子查询

1.比较条件

= ><>=<=!=<>between and

select * from emp where ename='SMITH';

select * from emp where sal  between 1300 and 2500;

2.

andorn

select * from emp where sal >=1300 and sal<=2500;

select ename,deptno from emp where deptno=20 or deptno=10;

3.null

不能使用条件判断,只能使用is

select * from emp where comm is null;

select * from emp where comm is not null;

select * from emp where not comm is null;

  1. 集合操作

UnionUnion AllIntersectMinus

Union,并集(去重) 对两个结果集进行并集操作,不包括重复行同时进行默认规则的排序;

select * from emp where sal>1500

Union

select * from emp where comm is not null;

Union All,全集(不去重) 对两个结果集进行并集操作,包括重复行,不进行排序 ;

select * from emp where sal>1500

Union All

select * from emp where comm is not null;

Intersect,交集(找出重复) 对两个结果集进行交集操作,不包括重复行,同时进行默认规则的排序;

select * from emp where sal>1500

intersect

select * from emp where deptno=30;

Minus,差集(减去重复) 对两个结果集进行差操作,不包括重复行,同时进行默认规则的

select deptno from dept

Minus

select distinct deptno from emp;

5.like:模糊查询

select * from emp where ename like 'SMITH';

求员工姓名以A开头的员工信息

select * from emp where ename like 'A%';

求员工姓名包含A的员工信息

select * from emp where ename like '%A%';

求员工名字第二个字符为A的员工信息

select * from emp where ename like '_A%';

6.inexists

in:

查询信息为1500,2000,2500,3000的员工的信息

select * from emp where sal=1500 or sal=2000 or sal=2500 or sal=3000;

在一些定值之间取任意一个,可以不使用or使用in,in是做定值判断的

select * from emp where sal in(1500,2000,2500,3000);

exists: 存在即保留,存在即合法     exists条件为true,存在记录则返回结果,后续不再继续 比较查询,与查询的字段无关,与记录有关

select*from emp where exists (select deptno from emp where ename='SMITH');

上述代码中exists后面的括号里是真实存在的数据,虽然数据只有一条,但只要它存在就是true,则在前面的select*from emp查询到每一条数据都会走到后面去判断,只要它真实就会输出查询到的每一条数据

注意:

select *

  from emp

 where exists (select deptno

          from dept

         where dname='SALES'

           and emp.deptno = dept.deptno);

--部门名称为'SALES','ACCOUNTING'中的所有员工信息

--要查询的诗句:员工信息

--数据来源:emp

--条件dname in ('SALES','ACCOUNTING')

select *

  from emp

 where exists (select deptno

          from dept

         where dname in ('SALES', 'ACCOUNTING')

           and emp.deptno =dept.deptno);

--表起别名

select *

  from emp e

 where exists (select deptno

          from dept d

         where dname in ('SALES', 'ACCOUNTING')

           and e.deptno =d.deptno);

 -- 所有员工都能拿到

select *

 from emp e

where exists

 (select deptno,dname from dept d where dname in ('SALES',

'ACCOUNTING') and e.deptno!=d.deptno);

--拿到除了'SALES', 'ACCOUNTING'部门的员工信息

select *

 from emp e

where not exists

 (select deptno,dname from dept d where dname in ('SALES',

'ACCOUNTING') and e.deptno=d.deptno);

--有奖金的员工信息

select empno, ename, sal,comm

 from emp e1

where exists (select empno, ename, sal, comm

 from emp e2

 where comm is not null

 and e1.empno = e2.empno);

--有奖金存在的部门的所有员工信息

select empno, ename, sal,deptno

 from emp e1

where exists (select empno, ename, sal, comm,deptno

 from emp e2

 where comm is not null

 and e1.deptno = e2.deptno );

:排序(asc 升序排列 ascend的缩写    desc 降序排列 descend的缩写)

使用 ORDER BY 排序,排序不是真实改变存储结构的顺序,而是获取的集合的顺序

--select *|字段.. from 数据来源 where 条件 order by 排序字段;    desc降序  默认升序 asc升序

--执行流程: from -- where -- select --order by

select*from emp order by sal;

--查询30部门中的所有员工,按照薪资进行降序排序  默认升序

select * from emp where deptno=30 order by sal asc;

--查询30部门中的所有员工,按照薪资进行序排序,如果薪资相同,根据员工编号降序排序

select * from emp where deptno=30 order by sal desc,empno desc;

--根据奖金进行降序排序   nulls first控制所有的null值在最前还是最好

select * from emp order by comm desc nulls last;

猜你喜欢

转载自www.cnblogs.com/greyrhinoceros-1998/p/10867345.html