加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长百科 > 正文

Oracle数据库2--Select查询语句

发布时间:2021-03-09 16:39:11 所属栏目:站长百科 来源:网络整理
导读:1.Select查询 select 用于从数据看查询数据。语法: select field1,filed2,.. . from tablename [where condition] -- 查询全部员工的名字和雇员号 select empno,ename from emp; -- 查询全部员工的雇员号、姓名、岗亭 select empno,ename,job from emp; --

1.Select查询

select 用于从数据看查询数据。语法:

select field1,filed2,.. .

from tablename

[where condition]

-- 查询全部员工的名字和雇员号
select empno,ename from emp;

-- 查询全部员工的雇员号、姓名、岗亭
select empno,ename,job from emp;

-- 字段的别名 as
select ename as "姓名" from emp;
select ename as "姓名",job as "岗亭" from emp;

-- 别名必然要用双引号,不能用单引号
select ename "姓名",job "岗亭" from emp;
-- 双引号可以省略
select ename 姓名 from emp;

-- 表的别名
select emp.ename,emp.job from emp;
select e.ename,e.job from emp e;

2.where语句

where 暗示查询的前提。

[1] =,!=,<>,<,>,<=,>= 相关运算符

-- where 子句

-- 把部门10的雇员查询出来
select * 
from emp 
where deptno = 10;

-- 把名称为smith的雇员
select e.*
from emp e
where e.ename = ‘SMITH‘;

-- 查询底薪大于便是1000的员工
select e.*
from emp e
where e.sal >= 1000;

select e.*
from emp e
where e.sal <> 800

?【2】any/some/all (list)

any/some(list) 满意list列表中的恣意一个前提

all(list) 满意list列表的中全部前提

-- any some all

-- 查询薪资大于1000可能薪资大于800的雇员
select e.*
from emp e
where e.sal > some(1000,800);

-- 查询薪资大于1000
select e.*
from emp e
where e.sal > all(1000,800);

【3】null

null 在sql中暗示的是不确定 => 可以以为没有值

-- null/not null
-- 查询没有补助的雇员
select e.*
from emp e
where e.comm is null

select e.*
from emp e
where e.comm is not null

【4】between x and y?

暗示一个值位于[x,y]区间,x/y 一样平常都是数字。

-- between x and y
-- 查询薪资在1000-5000之间的雇员
select e.*
from emp e
where e.sal between 1000 and 5000

-- 查询薪资在(3000,5000]之间的雇员
select e.*
from emp e
where e.sal between 3000.01 and 5000

【5】?in/not in list

暗示字段值是否在list列表中

-- in/not in(list)
-- 查询部门号是10和20的员工
select e.*
from emp e
where e.deptno in(10,20);

select e.*
from emp e
where e.deptno not in(10,20);

-- 查询薪资是1000,2000,5000的员工
select e.*
from emp e
where e.sal in (1000,2000,5000);

【6】恍惚查询

like 要害字用于恍惚查询,个中

%:暗示恣意字符呈现多次(含0次),

_:暗示恣意字符呈现1次。

escape(‘x’) 暗示指定转义字符为x,一样平常指定为

-- 查询名字是c开头的雇员

select e.*
from emp e
where e.ename like ‘c%‘;

-- 查询名字中第二个字母是M的雇员
select e.*
from emp e
where e.ename like ‘_M%‘

-- 查询名字中含有M的雇员
select e.*
from emp e
where e.ename like ‘%M%‘;

-- 查询名字中含有%的雇员
select e.*
from emp e
where e.ename like ‘%%%‘ escape(‘‘);

3.伟大查询(and/or)

where 后头的前提可以跟多个通过and 可能 or 毗连

and:且、而且

or: 或、可能

-- 查询部分10且薪资大于等2000的雇员
select e.*
from emp e
where e.deptno = 10 and e.sal >= 2000;

-- 查询名字中含M且薪资大于1000的雇员
select e.*
from emp e
where e.ename like ‘%M%‘ and e.sal > 1000

-- 查询部分在10或20的雇员
select e.*
from emp e
where e.deptno = 10 or e.deptno = 20

where 中and、or的执行服从题目

-- 思索:查询前提的次序对查询速率是否有影响?

/*
说明:
and 暗示且,前提越多,检索的数据量越来越少
or 暗示或,前提越多,检索的数据量越来越多
where 前提的执行次序从后向前
*/

-- 优化后的sql
select e.*
from emp e
where e.sal>=2000 and e.deptno = 10
-- 结论
-- AND:  把检索功效较少的前提放到后头

-- 查部分10或30的雇员
select e.*
from emp e
where e.deptno = 10 or e.deptno = 30;

and 和 or同时存在时,and先执行。

案例:

--行使in查询部分名称为 SALES 和 RESEARCH 的雇员姓名、人为、部分编号
-- 思索:部分名称位于dept,雇员信息位于emp表
select e.ename,e.sal,e.deptno
from emp e
where e.deptno in 
(
select d.deptno
from dept d
where d.dname = ‘SALES‘ or d.dname = ‘RESEARCH‘
);

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读