Oracle-Select statement _ function

1.1  query statement

1.1.1 select

select  for the data to see query data. grammar

select field1,filed2,.. .

from tablename

[where condition]

 

- Query the names of all employees and employee number

select empno,ename from emp;

 

- Query the employee number of all employees, name, job

select empno,ename,job from emp;

 

- Alias fields as

select ename as "姓名" from emp;

select ename as "姓名",job as "岗位" from emp;

 

- alias must use double quotes , not single quotes

select ename " name ", job " jobs " from emp;

- double quotes may be omitted

select ename 姓名 from emp;

 

- alias table

select emp.ename,emp.job from emp;

select e.ename,e.job from emp e;

 

Wildcard represents all the fields in the query . If the time to check a particular field, do not use * , affect query efficiency.

select empno,ename,job,mgr,hiredate,sal,comm,deptno

from emp;

- * wildcard represents all fields

select * from emp;

 

1.1.2  DISTINCT deduplication 

The repeatability of the record removed, leaving only one.

- Query's trades

select distinct e.job

from emp e;

 

When modifying a multi-field , multiple fields are not the same value was retained.

 

1.1.3  the WHERE clause 

where  represents the condition of the query.

 

[1] =,! =, <>, <,>, <=,> = Relationship operators

<> Indicates not equal

- where clause

 

-- 把部分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

 

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);

 

[2] 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

 

[3] 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

 

 

[4] in/not in list

表示字段值是否在list列表

-- in/not inlist

-- 查询部分号是1020的员工

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);

 

[5] 模糊查询 

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('\');

 

1.2 复杂查询(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

 

-- 查询部门在1020的雇员

select e.*

from emp e

where e.deptno = 10 or e.deptno = 20

 

where andor的执行效率问题

-- 思考:查询条件的顺序对查询速度是否有影响?

 

/*

分析:

and 表示且,条件越多,检索的数据量越来越少

or 表示或,条件越多,检索的数据量越来越多

where 条件的执行顺序从后向前

*/

 

-- 优化后的sql

select e.*

from emp e

where e.sal>=2000 and e.deptno = 10

-- 结论

-- AND:  把检索结果较少的条件放到后面

 

-- 查部门1030的雇员

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'

);

 

1.3 计算字段

我们经常需要把数据中检索出来的信息进行再加工,允许的操作+-*/。通过四个运算得到新的字段(计算字段)

计算字段在数据中不存在。

-- 查询出每个雇员的月薪(收入)

select e.ename,e.sal+e.comm as "收入",e.deptno

from emp e

 

 

注意:很多记录中的commnull,表示不确定的值,经常四则运算后的值也不确定

 

当遇到字段时null,可以通过nvl函数把null转化便于运算的类型。

-- nvl函数优化

select e.ename,e.sal+nvl(e.comm,0) "收入",e.deptno

from emp e

 

1.4 函数

函数一般是在数据上执行的,它给数据的转换和处理提供了方便。只是将取出的数据进行处理,不会改变数据库中的值。

函数根据处理的数据分为单行函数和聚合函数(函数)

 

 

组函数又被称作聚合函数,用于对多行数据进行操作,并返回一个单一的结果,组函数仅可用于选择列表或查询的having子句

单行函数对单个数值进行操作,并返回一个值。

 

dual一个系统表。注意用于测试。

1.5 字符相关

 

-- dual用于测试

select * from dual;

 

-- 1.字符串连接

select concat('aa','12') from dual;

select 'aa'||'12' from dual;

 

-- 2.首字母大写

select initcap('abc') from dual;

--- 把大写转化小写

select lower('ABc') from dual;

select upper('abc') from dual;

 

-- 把所有员工的姓名小写输出

select lower(e.ename),e.empno

from emp e

 

-- 3.填充字符lpad/rpad

select lpad('sxt',5,'*') from dual;

select rpad('sxt',5,'*') from dual;

 

-- 4.去掉空白字符

select '  kallen' from dual;

select ltrim('  kallen',' ') from dual;

select rtrim('  kallen  ',' ') from dual;

-- trim 删除左右两边的字符

select trim('a' from 'abc') from dual;

 

-- 5.求子串 substr(str,loc,len)-->loc1开始

select substr('abcd',2,2) from dual;

 

-- 6.查找字符串

/*

如果找到返回>=1的索引;如果没找到返回0

*/

select instr('abcd','b') from dual;

 

-- 7.求长度

select length('abcd') from dual;

 

需求:

select substr('18612341234',1,3)||'-'||substr('18612341234',4,4)||'-'||substr('18612341234',8,4)

from dual;

Guess you like

Origin www.cnblogs.com/l3344/p/11121713.html