A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

本帖最后由 htb52110 于 2017-12-28 21:24 编辑

5、Oracle的查询
5.1、单表查询
5.1.1、简单条件查询
5.1.1.1、精确查询
语法:
select * from 表名 where 列名=值;
5.1.1.2、模糊查询
语法:
select * from 表名where 字段 like '%值%'
注:%的位置在后,是指以某个‘值’开头;
% 的位置在前,是指以某个‘值’结尾;
两边都有%。是指包含某个‘值’的结果;
5.1.1.3、and运算符
语法:
select * from 表名 where 字段1=‘值1’  and 字段2=‘值2’;
解释:
查询的结果必须同时满足 字段1=‘值1’  和 字段2=‘值2’
5.1.1.4、or运算符
语法:
select * from 表名 where 字段1=‘值1’  or 字段2=‘值2’;
解释:
查询的结果满足 字段1=‘值1’  或 字段2=‘值2’两者之一即可
5.1.1.5、and 与 or 运算符混合使用
语法:
select * from t_owners where (字段1=值1 or 字段2=值2) and 字段3=值3;
注意:
and 的优先级比 or 大,所以我们需要用 ( ) 来改变优先级。
5.1.1.6、范围查询
语法1:
select * from 表名 where 字段>=10000 and 字段<=20000
语法2:
select * from 表名 where 字段 between 10000 and 20000
5.1.1.7、空值查询
语法1(查询某字段为空的结果):
select * from 表名t where 字段is null;
语法2(查询某字段不为空的结果):
select * from 表名 t where 字段 is not null;
5.1.2、去掉重复记录
语法:
select distinct 字段 from 表名;
解释:
查询表中的数据,去掉重复的字段的值;
5.1.3、排序查询
5.1.3.1、升序查询
语法:
select * from 表名order by 字段;
解释:
        查询该表中的数据,并按照该字段进行升序排序;
5.1.3.2、降序查询
语法:
select * from 表名order by 字段desc;
解释:
查询该表中的数据,并按照该字段进行降序排序;
5.1.4、基于伪列的查询
5.1.4.1、伪列的概念
在 Oracle 的表的使用过程中,实际表中还有一些附加的列,称为伪列。伪列就像表中的列一样,但是在表中并不存储。伪列只能查询,不能进行增删改操作。
5.1.4.2、ROWID
表中的每一行在数据文件中都有一个物理地址,ROWID 伪列返回的就是该行的物理地址。使用 ROWID 可以快速的定位表中的某一行。ROWID 值可以唯一的标识表中的一行。由于 ROWID 返回的是该行的物理地址,因此使用 ROWID 可以显示行是如何存储的。
语法:
select rowID,别名.* from 表名 别名
根据rowid查询结果(查询速度最快)
select rowID,t.*from T_AREA t where ROWID='AAAM1uAAGAAAAD8AAC';
5.1.4.3、ROWNUM
在查询的结果集中,ROWNUM 为结果集中每一行标识一个行号,第一行返回 1,第二行返回 2,以此类推。通过 ROWNUM 伪列可以限制查询结果集中返回的行数。
查询语句:
select rownum,别名.* from 表名 别名
5.1.5、聚合统计
ORACLE 的聚合统计是通过分组函数来实现的,与 MYSQL 一致
5.1.5.1、聚合函数
a、求和SUM()
语法:
select sum(字段) from 表名 where 条件='值';
解释:
根据条件查询字段和总和;
b、求平均值 AVG()
语法:
select avg(字段) from 表名 where 条件='值';
解释:
根据条件查询该字段的平均值;
c、求最大值 MAX()
语法:
select max(字段) from 表名 where 条件='值';
解释:
根据条件查出该字段的最大值;
d、最小值 MIN()
语法:
select min(字段) from 表名 where 条件='值';
解释:
根据条件查询出该字段的最小值;
e、统计记录个数 COUNT()
语法:
select count(*) from 表名t where 条件=值;
解释:
根据条件查询个总个数;
5.1.5.2、分组聚合 Group by
语法:
select 字段1,sum(字段2) from 表名 group by 字段1;
解释:
查询出字段1和字段2的和,并按照字段1进行分组;
5.1.5.3、分组后条件查询 having
语法:
select 字段1,sum(字段2) from 表名 group by 字段1 having sum(字段2)>条件;
解释:
查询出字段1和字段2的和,并按照字段1进行分组,并根据结果进行筛选符合条件的信息;
5.2、连接查询
5.2.1、多表内连接查询
语法:
select o.id 业主编号,o.name 业主名称,ot.name 业主类型
from T_OWNERS o,T_OWNERTYPE ot
where o.ownertypeid=ot.id;
解释:
查询两个表中的数据,其中在T_OWNERS 表中的ownertypeid是T_OWNERTYPE 表的外键,根据外键相等,查询出结果。
注意:
以上仅列出两个表的内连接查询,多表语法一致,要求外键相同即可;
5.2.2、左外连接查询
语法:
SELECT ow.id,ow.name,ac.year ,ac.month,ac.money FROM
T_OWNERS ow,T_ACCOUNT ac
WHERE ow.id=ac.owneruuid(+);
解释:
按照 ORACLE 提供的语法,如果是左外连接,就在右表所在的条件一端填上(+)
5.2.3、右外连接查询
语法:
select ow.id,ow.name,ac.year,ac.month,ac.money from
T_OWNERS ow , T_ACCOUNT ac
where ow.id(+) =ac.owneruuid
解释:
按照 ORACLE 提供的语法,如果是右外连接,就在左表所在的条件一端填上(+)
5.3、子查询
5.3.1、where 子句中的子查询
5.3.1.1、单行子查询
只返回一条数据,
单行操作符:
=        :等于
>        :大于
>=        :大于等于
<        :小于
<=        :小于等于
<>        :不等于
语句:
select * from T_ACCOUNT
where year='2012' and month='01' and usenum>
( select avg(usenum) from T_ACCOUNT where year='2012' and
month='01' )
解释:
从一个子查询的结果中查询数据;
5.3.1.2、多行子查询
返回多条数据
多行操作符
in        :等于列表中的任何一个
any        :和子查询返回的任意一个值比较
all        :和子查询返回的所有值比较
a、in运算
需求:查询地址编号为 1 、3、4 的业主记录
分析:如果我们用 or 运算符编写,SQL 非常繁琐,所以我们用 in 来进行查询
语句如下:
select * from T_OWNERS where addressid in ( 1,3,4 );
5.3.2、from 子句中的子查询
from 子句的子查询为多行子查询
需求:查询显示业主编号,业主名称,业主类型名称,条件为业主类型为”居民”,
使用子查询实现。
语法:
select * from (select o.id 业主编号,o.name 业主名称,ot.name 业主类型
from T_OWNERS o,T_OWNERTYPE ot where o.ownertypeid=ot.id)
where 业主类型='居民';
5.3.3、select 子句中的子查询
select 子句的子查询必须为单行子查询
(1)需求:列出业主信息,包括 ID,名称,所属地址。
语句:
select id,name,
(select name from t_address where id=addressid) addressname
from t_owners;
5.4、分页查询
5.4.1、简单的分页查询
select  * from ( select rownum r,t.* from (select * from t_account order by usenum) t) where r>10 and r<=20
5.4.2、基于分析函数的分页查询
select * from (select row_number() over(order by usenum) r,t.* from t_account t) where  r>10 and r<=20                  

5.4.3、基于集合运算的分页查询
select rownum,t.* from t_account t where rownum < 20
minus
select rownum,t.* from t_account t where rownum < 10 ;
6、单行函数
        字符函数   length  substr   ||  concat
        数值函数   round  trunc  mod
        日期函数   add_month  last_day  trunc
        转换函数   to_char  to_date  to_number
        其他函数   nvl nvl2 decode  case when  then
oracle函数大全(分类显示).chm (70.1 KB, 下载次数: 2, 售价: 2 黑马币)
7、行列转换
select (select name from T_AREA where id= areaid ) 区域,
sum( case when month='01' then money else 0 end) 一月,
sum( case when month='02' then money else 0 end) 二月,
sum( case when month='03' then money else 0 end) 三月,
sum( case when month='04' then money else 0 end) 四月,
sum( case when month='05' then money else 0 end) 五月,
sum( case when month='06' then money else 0 end) 六月,
sum( case when month='07' then money else 0 end) 七月,
sum( case when month='08' then money else 0 end) 八月,
sum( case when month='09' then money else 0 end) 九月,
sum( case

attachment.png (2.83 KB, 下载次数: 22)

attachment.png

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马