03.oracle的sql语句

03.oracle的sql语句

查询语句

-- 查询员工表中部门编号设计
select EMPNO,ENAME,JOB from emp where DEPTNO = 10;
-- distinct 去重查询 必须放到开头,多个字段的时候,每个字段都不一样才去重
select distinct DEPTNO from emp;
-- 查询过程中可以个列添加别名
select e.EMPNO 雇员编号,e.ENAME as 雇员名称,e.JOB 雇员工作 from EMP e where e.DEPTNO = 10;
--查询所有的字段可以使用*;但是在项目中不要使用
select * from EMP;

/*
=,!=,<>,<,>,<=,>=,any,some,all
is null,is not null
between x and y
in(list),not in(list)
exists(sub-query)
like  _ ,%,escape ‘\‘   _\% escape ‘\’

*/

-- =
select * from EMP where DEPTNO = 20;
-- !=
select *
from EMP where DEPTNO != 20;
-- <> 不等于
select * from EMP where DEPTNO <> 20;
-- <
select *
from EMP where SAL < 1500;
-- >
select *
from EMP where SAL > 1500;

-- <=
select *
from EMP where SAL <= 1500;
-- >=
select *
from EMP where SAL >= 1500;
-- any 其中的任意一个
select *
from EMP where SAL > any(1000,1500,3000);
-- some 与any效果一样 只要其中的一个条件成立即可
select *
from EMP where SAL > some(1000,1500,3000);
-- all 大于所有的值都会成立

select *
from EMP where SAL >all(1000,1500,3000);
-- is null;null在sql中有个特殊含义 null != null不能使用,不能使用!= ; = 判断,需要使用is is not;
select *
from EMP where comm is null ;
-- is not null
select * from EMP where COMM is not null;
select *
from EMP where null is null ;
-- between x and y,包含 x y;
select *
from EMP where SAL between 1500 and 3000;

select *
from EMP where SAL>= 1500 and SAL <= 3000;
-- 对一些值 进行等值判断可以使用 in 和 not in
-- in(list)
select *
from EMP where DEPTNO in (10,20);

-- and和or者这样的关键字,and相当与操作,or相当与或操作
-- and和or可能出现在同一个sql中,需要注意and和or的优先级
-- and的优先级高与于or的优先级,or的操作需要使用括号来保证优先级
select *
from EMP where DEPTNO = 10 or DEPTNO = 20;
-- not in(list)
select *
from EMP where DEPTNO not in(10,20);

select *
from EMP where DEPTNO != 10 and DEPTNO != 20;
/*
    exists (sub-query),当exists中的子查询语句,能查到对应结果的时候,
    意味着条件满足
    相当于双层for循环,
*/
-- 查询部门内编号为10和20的员工,要求使用exists实现
select *
from emp where DEPTNO = 10 or DEPTNO  = 20;
select *
    from EMP e
where exists(select d.DEPTNO from DEPT d where (d.DEPTNO = 10 or d.DEPTNO = 20) and e.DEPTNO = d.DEPTNO);

/*
模糊查询:
like  _ ,%,escape ‘\‘   _\% escape ‘\’

在like的语句中,需要使用占位符或者通配符
_,某个字符或者数字仅出现一次
%,任意字符出现任意次数
escape,使用转义字符,可以自己规定转义字符

使用like的时候要慎重,因为like的效率比较低
使用like可以参考使用索引,但是要求不能以%开头
涉及到大文本的检索的时候,可以使用某些框架 luence,solr,elastic search
*/

-- 查询s开头的的用户
select *
from EMP where ENAME like ('S%');
-- 查询名字以S开头,且倒数第二个字为T
select *
from EMP where ENAME like ('S%T_');
-- 查询名字中带有%的用户
select * from EMP where ENAME like('%\%%') escape ('\');

/*
  order by进行排序操作
默认情况下完成的是升序的操作,
asc:是默认的排序方式,表示升序
desc:降序的排序方式

排序是按照自然顺序进行排序的
如果是数值,那么按照从大到小
如果是字符串,那么按照字典序排序

在进行排序的时候可以指定多个字段,而且多个字段可以使用不同的排序方式

每次在执行order by的时候相当于是做了全排序,思考全排序的效率
会比较耗费系统的资源,因此选择在业务不太繁忙的时候进行
 */

select *
from EMP order by SAL;

select *
from EMP order by SAL desc;

select *
from emp order by ENAME;

select *
from EMP ORDER BY SAL desc ,ENAME asc ;

-- 使用计算字段
-- 字符串连接
select 'my name is '||ENAME from emp;
select concat('my name is ',ENAME) 姓名
from EMP;

-- 计算所有员工的年薪
select ENAME,(e.SAL+COMM)*12 from EMP e;
/*
    null是比价特殊的存在;null做任何计算都是null,因此要进行非空判断
    引入nvl,nvl(a1,a2),如果是a1为空的,则返回a2;如果不是空则返回a1
*/
select e.ENAME,(e.SAL+nvl(e.COMM,0))*12 年薪
from EMP e;
-- dual是oracle中的虚拟表,没有实际的数据,可以用来测试
select 100+null
from DUAL;

-- 交集 并集 差集计算
-- A
select *
from EMP where DEPTNO = 30;
-- B
select *
from EMP where SAL >3000;
-- 并集: 将两个表中所有的数据显示出来,但是没有重复的数据只显示一次
select *
from EMP where DEPTNO = 30 union
select *
from EMP where SAL >1000;
-- 全集: 两个表全部数据,不进行去重操作
select *
from EMP where DEPTNO = 30 union all
select *
from EMP where SAL >1000;

-- 交集: 两个集合中交叉显示的数据集,只显示一次
select *
from EMP where DEPTNO = 30 intersect
select *
from EMP where SAL >1000;
-- 差集: 包含集合A集合而不包含集合B的数据,跟A和B的顺序相关
select *
from EMP where DEPTNO = 30 minus
select *
from EMP where SAL >1000;

猜你喜欢

转载自blog.csdn.net/Bruce_Zhang0828/article/details/112720281