[亲测]Oracle查询--单表查询,连接查询(一)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_43652509/article/details/86292696

ORACLE查询(单表查询,连接查询)

一、单表查询

(一)简单条件查询

1. 查询水表编号为 30408 的业主记录 ( 精确查询 )
select * from t_owners where watermeter= 30408;

结果:
在这里插入图片描述

2. 查询业主名称包含“刘”的业主记录 (模糊查询)
select *  from t_owners where name like  '%刘%';

结果:
在这里插入图片描述

3. 查询业主名称包含“刘”的并且门牌号包含5 的业主记录(and 运算符 )
select * from t_owners where name like '%刘%' and housenumber like '%5%';

结果:
在这里插入图片描述

4. 查询业主名称包含“刘”的或者门牌号包含5 的业主记录(or 运算符)
select * from t_owners where name like '%刘%' or housenumber like '%5%' ;

结果:
在这里插入图片描述

5. 查询业主名称包含“刘”的或者门牌号包含5的业主记录,并且地址编号为 3 的记录(and 和 or 运算符混合使用)
select * from t_owners where (name like '%刘%' or housenumber like '%5%') and addressid like '%3%';

结果:
在这里插入图片描述

6. 查询台账记录中用水字数大于等于10000 ,并且小于等于20000 的记录( 范围查询 )
--方式一
select * from t_account where usenum between 10000 and 20000;
--方式二
select * from t_account where usenum >= 10000 and usenum <= 20000;  

结果:
在这里插入图片描述

7. 查询T_PRICETABLE 表中 MAXNUM为空的记录(空值查询 )
select * from t_pricetable where maxnum is null;

结果:
在这里插入图片描述

8. 查询T_PRICETABLE 表中 MAXNUM不为空的记录(非空查询 )
select * from t_pricetable where maxnum is not null;

结果:
在这里插入图片描述

(二)去掉重复记录

需求:查询业主表中的地址ID,不重复显示
--方式一
select distinct addressid from t_owners;
--方式二
select distinct(addressid) from t_owners;

结果:
在这里插入图片描述

(三)排序查询

1.升序排序
需求:对 T_ACCOUNT 表按使用量进行升序排序
select * from t_account order by usenum asc;

结果:
在这里插入图片描述

2.降序排序
需求:对 T_ACCOUNT 表按使用量进行降序排序
select * from t_account order by usenum desc;

结果:
在这里插入图片描述

(四)伪列只能用来查,不能做增删改操作

1 .ROWID :物理文件上唯一区别这条记录的唯一标识
查询 T_AREA 表的 ROWID 值和 T_AREA的值
select rowid , t.* from t_area t;

结果:
在这里插入图片描述

用 ROWID 作为查询条件
select rowid ,t.* from t_area t where rowid = 'AAADW+AAFAAAADcAAA';

结果:
在这里插入图片描述

2.ROWNUM 为结果集中每一行标识一个行号
查询 T_AREA 表的 ROWNUM 值和 T_AREA的值
select rownum ,t.* from t_area t ;

结果:
在这里插入图片描述

(五)聚合统计

1 . 聚合函数
1 统计2012 年所有用户的用水量总和
select sum(usenum) from t_account where year = '2012';

结果:
在这里插入图片描述

2 统计2012 年所有用水量(字数)的平均值
select avg(usenum) from t_account where year = '2012';

结果:
在这里插入图片描述

3 统计2012 年最高用水量(字数)
select max(usenum) from t_account where year = '2012';

结果:
在这里插入图片描述

4 统计2012 年最低用水量(字数)
select min(usenum) from t_account where year = '2012';

结果:
在这里插入图片描述

5 统计业主类型ID 为 1 的业主数量
select count(month) from t_account where ownertype = 1; 

结果:
在这里插入图片描述

2.分组聚合 Group by
按区域分组统计2012 年水费合计数
select areaid , sum(areaid) from t_account where year = '2012' group by areaid;

结果:
在这里插入图片描述

3 .分组后条件查询 having
查询 2012 年水费合计大于 169000 的区域及水费合计
select areaid , sum(money)  from t_account where year = '2012' group by areaid having sum(money) > 169000;

结果:
在这里插入图片描述

二、连接查询

(一)多表内连接查询
1. 查询显示业主编号,业主名称,业主类型名称
select os.id , os.name , oe.name 
       from t_ownertype oe , t_owners os 
       where os.ownertypeid = oe.id;

结果:
在这里插入图片描述

2 .查询显示业主编号,业主名称、地址和业主类型
select os.id , os.name  , ad.name, oe.name 
       from t_ownertype oe , t_owners os , t_address ad 
       where os.ownertypeid = oe.id and  os.addressid = ad.id;

结果:
在这里插入图片描述

3. 查询显示业主编号、业主名称、地址、所属区域、业主类型
select os.id , os.name , ad.name , ar.name , oe.name 
       from t_ownertype oe , t_owners os , t_area ar , t_address ad
       where os.ownertypeid = oe.id and os.addressid = ad.id and  ad.areaid = ar.id;

结果:
在这里插入图片描述

4. 查询显示业主编号、业主名称、地址、所属区域、收费员、业主类型
select os.id , os.name , ad.name ,ar.name , op.name ,oe.name 
       from t_ownertype oe , t_owners os , t_area ar , t_address ad ,t_operator op 
       where os.ownertypeid = oe.id and os.addressid = ad.id and ad.areaid = ar.id and ad.operatorid = op.id;

结果:
在这里插入图片描述

(二)左外连接查询
1 .查询业主的账务记录,显示业主编号、名称、年、月、金额。如果此业主没有账务记录也要列出姓名
--   MySQL
select os.id , os.name , ac.year , ac.month,ac.money 
       from t_owners os left join t_account ac on os.id = ac.owneruuid;
--   Oracle
select os.id ,os.name , ac.year , ac.month , ac.money 
       from t_owners os , t_account ac where os.id = ac.owneruuid(+);

结果:
在这里插入图片描述

(三)右外连接查询
1 . 查询业主的账务记录,显示业主编号、名称、年、月、金额。如果账务记录没有对应的业主信息,也要列出记录
 -- MySQL
select os.id , os.name ,ac.year , ac.month , ac.money
       from t_owners os right join t_account ac on os.id = ac.owneruuid;
 -- Oracle
select os.id ,os.name , ac.year , ac.month , ac.money
       from t_owners os, t_account ac where os.id(+) = ac.owneruuid; 

结果:
在这里插入图片描述

以下推荐个人文章
[亲测]Oracle数据库安装与配置

[亲测]Oracle数据库操作

[亲测]Oracle表的创建、修改与删除

[亲测]Oracle数据库约束

[亲测]Oracle数据增删改

[亲测]Oracle查询–单表查询,连接查询(一)

[亲测]Oracle查询–子查询,分页查询(二)

[亲测]Oracle查询–单行函数–PL/SQL,分析函数,集合运算(三)

Oracle执行计划Explain Plan 如何使用

[亲测]数据库优化

猜你喜欢

转载自blog.csdn.net/qq_43652509/article/details/86292696