我有一个包含三个表的数据库:userid_tbl、need_tbl、have_tbl
create table userid_tbl
(user_id varchar2(15) not null primary key);
create table need_tbl
(user_id varchar2(15) not null,
have_item varchar2(100) not null,
foreign key (user_id) references userid_tbl (user_id)
);
create table have_tbl
(user_id varchar2(15) not null,
have_item varchar2(100) not null,
foreign key (user_id) references userid_tbl (user_id)
);
每个用户可以创建无限数量的需求或服务记录,他们可以在数据库中提供给其他人。这些项目来自预定列表。
在对需要和有表进行连接后,我已经匹配了需要和需要,并在如下视图中丢弃了任何用户都无法完成的请求:
Item: Need: Have:
1 Bob George
2 George Herbie
3 Herbie Bob
4 Larry Wally
5 Wally Larry
6 John George
我现在正在尝试编写一个查询,我可以在其中计算满足其请求需求的每个用户的排列数。例如,在上面的示例中,Bob、George 和 Herbie 可以一起满足彼此的需求,Larry 和 Wally 也可以,但 John 不能,因此总数为 2。
我一开始是用 LOOP 来做这件事的,但必须有一种更简单、资源消耗更少的方法来用单个查询来做这件事。