connect by 使用实例

简介: 在实际开发中  connect by可以替代plsql或java中的循环间化开发代码,提高开发效率。如下是我在工作中遇到一个实际解决的问题 具体场景: 原系统是一个管理宿舍信息的系统,dorm_room用于存放宿舍的房间信息具体表结...

在实际开发中  connect by可以替代plsql或java中的循环间化开发代码,提高开发效率。如下是我在工作中遇到一个实际解决的问题

具体场景:

原系统是一个管理宿舍信息的系统,dorm_room用于存放宿舍的房间信息具体表结构是
create table dorm_room(bno varchar2(2),fno varchar2(2),rno varchar2(2),bednum varchar2(2));
这里存放了楼栋号,层号,房间号,及房间中床位总数
具体数据如下
insert into dorm_room values(1,1,1,4);
insert into dorm_room values(1,1,2,4);
insert into dorm_room values(1,1,3,7);
也就是说有三个房间,前两个房间床位数都是4,最后一个房间的床位数是7
目前的需求是根据床位数显示出每个房间中所有床位的数据。期望结果是
1  1  1  1
1  1  1  2
1  1  1  3
1  1  1  4
1  1  2  1
1  1  2  2
1  1  2  3
1  1  2  4
1  1  3  1
1  1  3  2
1  1  3  3
1  1  3  4
1  1  3  5
1  1  3  6
1  1  3  7

如果用pl/sql根据每行的床位数判断循环次数,显示出每个房间的所有床位数,编写起来必然要书写较多行代码,因此选用
sql中的connect by的方式生成出所有的房间的床位数,具体sql为

select distinct bno,fno,rno,t.l
from (
select row_number()over(partition by bno,fno,rno order by level) rn,
       bno,fno,rno,level l
from dorm_room
connect by level ) t
order by bno,fno,rno

显示结果

BN FN RN          L
-- -- -- ----------
1  1  1           1
1  1  1           2
1  1  1           3
1  1  1           4
1  1  2           1
1  1  2           2
1  1  2           3
1  1  2           4
1  1  3           1
1  1  3           2
1  1  3           3

BN FN RN          L
-- -- -- ----------
1  1  3           4
1  1  3           5
1  1  3           6
1  1  3           7



目录
相关文章
|
8月前
|
NoSQL Redis 开发工具
redisCould not connect to Redis at 127.0.0.16379 Connection refused错误解析
redisCould not connect to Redis at 127.0.0.16379 Connection refused错误解析
78 0
|
5月前
|
NoSQL MongoDB
connect-mongo无法连接到mongodb报错Error: failed to connect to [undefined:27017]
connect-mongo无法连接到mongodb报错Error: failed to connect to [undefined:27017]
|
8月前
|
网络协议 Linux 网络安全
使用frp时遇到的问题connect: connection refuseddial tcp xxxx:7000: connect: connection refused
最近在做的项目需要用到frp来做代理连接本地内网机,卡在最后启动客户端的时候,提示报错:login to server failed: dial tcp xxxx:7000: connect: connection refuseddial tcp xxxx:7000: connect: connection refused!!找了很多尝试的办法,现在给大家列一下希望对大家有帮助。
1412 0
|
关系型数据库 MySQL 数据库连接
mysql报错:Data source rejected establishment of connection, message from server: \“Too many connectio
最近在做压力测试嘛,需要逐步增加用户量做验证,每个用户单独创建数据库进行连接,就要不断去创建数据库,这个报错也很容易理解,mysql连接数不够用了
244 0
mysql报错:Data source rejected establishment of connection, message from server: \“Too many connectio
|
网络协议 NoSQL Redis
Could not create server TCP listening socket 127.0.0.16379 bind 操作成功
Could not create server TCP listening socket 127.0.0.16379 bind 操作成功
Could not create server TCP listening socket 127.0.0.16379 bind 操作成功
|
NoSQL MongoDB
运行 mongo 出现 Error: couldn‘t connect to server 127.0.0.1:27017, connection attempt failed
运行 mongo 出现 Error: couldn‘t connect to server 127.0.0.1:27017, connection attempt failed
504 0
运行 mongo 出现 Error: couldn‘t connect to server 127.0.0.1:27017, connection attempt failed
Failed to connect to 127.0.0.1 port 1080: Connection refused package 问题解决方法
Failed to connect to 127.0.0.1 port 1080: Connection refused package 问题解决方法
644 0
Failed to connect to 127.0.0.1 port 1080: Connection refused package 问题解决方法
|
Android开发
解决as connection refused: connect错误
解决as connection refused: connect错误
解决as connection refused: connect错误
|
JavaScript 中间件 Go