select语句_函数

1.1 查询语句

1.1.1 select

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

select field1,filed2,.. .

from tablename

[where condition]

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

表的别名:

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

* 通配符表示查询所有字段。如果要查特定字段时,不要使用*,影响查询效率

1 select e.* from emp e;

1.1.2 distinct 去重

重复性的记录去掉,只保留一条。

修饰多字段时,多个字段的值都不一样才保留。

查询工种:

1 select distinct e.job from emp e;

1.1.3 where 子句

where 表示查询的条件。

【1】 =,!= ,<>,<,>,<=,>= 关系运算符

<> 表示不等于

查询底薪大于等于1000的员工:

1 select e.* from emp e where e.sal >= 1000;

any/some/all (list)

any/some(list) 满足list列表的任意一个条件

all(list) 满足list列表的中所有条件

1 select e.* from emp e where e.sal>any(some)(800,1000);
1 select e.* from emp e where e.sal>all(800,1000);

 null

【2】null 在sql中表示是不确定 => 可以认为没有值

1 select e.* from emp e where e.comm is (not) null;

【3】between x and y 

表示一个值位于[x,y]区间,x/y 一般都是数字。

1 select e.* from emp e where e.sal between 1000 and 5000;

【4】 in/not in list

表示字段值是否在list列表

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

【5】模糊查询 

like 关键字用于模糊查询,其中

%:表示任意字符出现多次(0),

_:表示任意字符出现1

1 (已c开头)
2 select e.* from emp e where e.ename like 'c%';
3 (第二位为c)
4 select e.* from emp e where e.ename like '_c%';
5 (含有c)
6 select e.* from emp e where e.ename like '%c%';
7 (含有%的)
8 select e.* from emp e where e.ename like '%\%%' escape('\');

1.2 复杂查询(and/or)

where 后面条件可以跟多个通过and 或者 or 连接

and:且、并且

or: 或、或者

查询部门10且薪资大于等2000的雇员:

1 select e.* from emp e where e.deptno = 10 and e.sal >= 2000;

查询部门在1020的雇员:

1 select e.* from emp e where e.deptno = 10 or e.deptno = 20

where 中andor的执行效率问题:

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

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

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

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

如果and  or同时存在and先执行。

案例:

使用in查询部门名称为 SALES RESEARCH 的雇员姓名、工资、部门编号    

思考:部门名称位于dept,雇员信息位于emp

1 select e.ename,e.sal,e.deptno
2 from emp e
3 where e.deptno in 
4 (
5 select d.deptno
6 from dept d
7 where d.dname = 'SALES' or d.dname = 'RESEARCH'
8 );

1.3 计算字段

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

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

1 select e.ename,e.sal+e.comm as "收入",e.deptno from emp e;

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

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

(当comm值为null时,使用nvl函数:如果e.comm为null,则取后一个值,不为null,则取e.comm)

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

1.4 函数

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

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

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

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

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

 1 --字符串连接
 2 select concat('aa','12') from dual;
 3 select 'aa'||'12' from dual;
 4 
 5 --首字母大写
 6 select initcap('abcd') from dual;
 7 
 8 --把大写转化小写
 9 select lower('ABC') from dual;
10 --把小写转化大写
11 select upper('abcd') from dual;
12 
13 --填充字符lpad/rpad
14 select lpad('abc',5,'/') from dual;
15 
16 --去掉空白字符
17 select ltrim('  hellow',' ') from daul;
18 select rtrim('hellow  ',' ') from daul;
19 --删除左右两边
20 select trim('p' from 'pop') from daul;
21 
22 --求子串 substr(str,loc,len)-->loc从1开始,len表示截取长度
23 select substr('saber',1,2) from daul;
24 
25 --查找字符串  找到返回>=1的索引;如果没找到返回0
26 select instr('abcd','b') from dual;
27 
28 --求长度
29 select length('abcd') from dual;
30 
31 小需求
32 select substr('18112345678',1,3)||'-'||substr('18112345678',4,4)||'-'||substr('18112345678',8) from  daul;
33 输出结果为:181-1234-5678

 

猜你喜欢

转载自www.cnblogs.com/LSZJZ/p/10859342.html