Oracle基本查询

来源:互联网 发布:js toggleclass 编辑:程序博客网 时间:2024/06/08 18:29

SELECT(查询记录)

语法

SELECT [DISTINCT] <column1 [as new name] ,columns2,...>

FROM <table1>

[WHERE <条件>]

[GROUP BY <column_list>]

[HAVING <条件>]

[ORDER BY <column_list> [ASC|DESC]]

 

DISTINCT --去除重复值

WHERE --按照一定的条件查找记录

GROUP BY --分组查找(需要汇总时使用)

HAVING --分组的条件

ORDER BY --对查询结果排序

 

去除重复值distinct

select distinct * 列名 别名 from 表名称;

例:

select dis1tinct job from emp;

where子句查询

select * 具体的列名 from 表名称 where 条件表达式;

例:

查询出工资在1500(不包括1500)之上的雇员信息

select * from emp where sal > 1500

查询奖金

select * from emp where comm is not null;

查询没有奖金

select * from emp where comm is null;

where语句的运算符

AND限定关键字

where <条件1>AND<条件2> 限定条件必须满足

例:

查询工资在1500之上,拿到奖金的雇员信息

select * from emp where sal > 1500 and comm is not null

查询出工资在1500之上,在3000之下的全部雇员信息

select * from emp where sal > 1500 and sal < 3000

 

OR“或”关键字满足其中一个条件

where <条件1>OR<条件2> --两个条件中有一个满足即可

示例:

查询部门编号为10,或工资大于2000

select * from emp where deptno=10 OR sal>2000;

查询工资在1500之上,或者可以领取到奖金的雇员信息

select * from emp where sal > 1500 or comm is not null

 

where NOT <条件> --不满足条件的

示例:

查询部门不是编号为10;

select * from emp where not deptno=10;

 

where IN(条件列表) --所有满足在条件列表中的记录

示例:

在什么之内

select * from 表名where 列名 in (值1,值2,值3);

select * from 表名 where 列名 not in (值1,值2,值3);

例:

查询出指定雇员编号的所有信息(7369,7499,7521)

select * from emp where empno in(7369,7499,7521)

·

过滤语句:between

where BETWEEN .. AND ..  --按范围查找

between x and y,相当于a<=x and a>=y,

示例:

查询工资在包括1500之上,但是在包括3000之下的全部雇员信息

select * from emp where sal=>1500 and sal<=3000;

等同于

select * from emp where sal between 1500 and 3000;

查询1981年雇佣的全部雇员信息

oracle 17-12月-80 = 1980年12月17号

select * from emp where hiredate between '1-1月 -81' and '31-12月 -81';

 

where 字段 LIKE --模糊查询

“%”可以匹配任意长度的内容,包括0个字符;

“_”可以匹配一个长度的内容。

示例1:

查询所有雇员姓名中第二个字母包含“M”的雇员信息

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

查询1981年雇佣的全部雇员信息

select * from emp where hiredate like '%81%';

查询工资中包含6的雇员信息

select * from emp where sal like '%6%';

where 字段 IS [NOT] NULL --查找该字段是[不是]空的记录

 

汇总数据是用的函数

SUM --求和

示例:

查询每个部门总工资

select deptno,sum(sal) from emp group by deptno

AVG --求平均值

查询每个部门平均工资

select deptno,avg(sal) from emp group by deptno

MAX --求最大值

查询每个部门最高工资

select deptno,max(sal) from emp group by deptno

MIN --求最小值

查询每个部门最低工资

select deptno,min(sal) from emp group by deptno

COUNT --求个数

count(列名)查询总记录数DREAM

查询雇员总人数

select count(empno) from emp;

 

子查询

select * | 具体的列名称 from 表名称 where 条件表达式( select * | 具体的列名称 from 表名称 where 条件表达式(...)group by 分组条件 having 分组条件 order by 排序字段 asc|desc) group by 分组条件 having 分组条件 order by 排序字段 asc|desc

示例:

查询出比7654工资要高的全部雇员信息

首先要知道7654雇员的工资是多少

select sal from emp where empno=7654;

上面查询的结果作为最后查询的子查询结果,只要是其他的工资大于上面的查询结果,则表示符合条件。

select * from emp where sal > (select sal from emp where empno=7654);

所有的子查询语句必须在“()”中编写。

子查询在操作上分为三类:

单列子查询:返回的结果是某列的一个内容,出现的几率最高

单行子查询:返回多个列,有可能是一条完整的记录

多行子查询:返回多条记录

 

查询出工资比7654高,同时与7788从事相同工作的全部雇员信息

·-查询出7654的工资

select sal from emp where empno=7654;

·-查询出7788的工作名称

select job from emp where empno=7788;

·-总和查找

select * from emp where sal > (select sal from emp where empno = 7654) and job = (select job from emp where empno=7788)

 

运算符

ANY

any操作符的一般用法:=any(与in操作符的功能完全一样)> any(比里面最小的值要大) < any(比里面最大的值要小)

示例:

找出比deptno=30的员工最低工资高的其他部门的员工

select * from emp where sal>ANY(select sal from emp where deptno=30) and deptno<>30;

--ALL

all操作符的一般用法: > all(比最大值要大)、< all(比最小值要小)

--找出比deptno=30的员工最高工资高的其他部门的员工

select * from emp where sal>ALL(select sal from emp where deptno=30) and deptno<>30;

 

多表查询

语法

select *|具体的列名 from 表名称1,表名称2 where 条件表达式 order by 排序字段 asc|desc;

表名设置别名

select * from emp e,dept d where e.deptno=d.deptno;

查询出雇员的编号、雇员姓名、部门编号、部门名称及部门位置

select e.empno,e.ename,e.deptno,d.dname,d.loc from emp e,dept d where e.deptno=d.deptno;

要求查询出每个雇员的姓名,工作,雇员的直接上级领导的姓名

select e.ename,e.job,m.ename from emp e,emp m where e.mgr=m.empno;

此处查询将emp表做自身的关联,别名两次。

继续扩展之前的程序,要求将雇员素在部门名称同时列出

select e.ename,e.job,m.ename,d.dname from emp e,emp m,dept d where e.mgr=m.empno and e.deptno=d.deptno;

先确定工资等级表

select * from sal grade;

在查询出每个雇员的姓名,工资,部门名称和工资等级

select e.ename,e.sal,d.dname,s.grade from emp e,dept d,salgrade s where e.deptno=d.deptno and e.sal between s.losal and s.hisal;

 

·左、右连接

(+)在=左边表示右连接,查询时以=右边的表作为标准

(+)在=右边表示左连接,查询时以=左边的表作为标准

右连接

查看两张表关联后的完整信息

select e.empno,e.ename,d.deptno,d.dname,d.loc from emp e,dept d where e.deptno(+)=d.deptno;

例左连接

查询雇员的编号,姓名及其领导的编号、姓名(包含没有领导的雇员)

select e.empno,e.ename,m.ename from emp e,emp m where e.mgr=m.empno(+);

·交叉连接(cross join):产生笛卡尔积

select * from emp cross join dept;--56

·自然连接(natural join):自动进行关联字段的匹配

select * from emp natural join dept;

相当于

select * from emp,dept where emp.deptno=dept.deptno;

using子句:直接关联的操作列

select * from emp e join dept d using(deptno) where deptno=30;

on子句,用户自己编写连接的条件

select * from emp e join dept d on(e.deptno=d.deptno) where d.deptno=30;

左连接(左外连接)、右连接(右外连接:

left join,right join  select e.ename,d.deptno,d.dname,d.loc from emp e right outer join dept d on(e.deptno=d.deptno);

连接查询

SELECT <字段列表> from <table1,table2> WHERE table1.字段[(+)]=table2.字段[(+)]

 

示例

select empno,ename,dname from emp,dept where emp.deptno=dept.deptno;

 

查询指定行数的数据

SELECT <字段列表> from <table_name> WHERE ROWNUM<行数;

示例:

select * from emp where rownum<=10;--查询前10行记录

注意ROWNUM只能为1 因此不能写 select * from emp where rownum between 20 and 30;

 

要查第几行的数据可以使用以下方法:

select * from emp where rownum<=3 and empno not in (select empno from emp where rownum<=3);

结果可以返回整个数据的3-6行;

0 0
原创粉丝点击