如何迭代寻找事件记录中的时间冗余?

How to iterate looking for a time redundancy in event records?

用例:

我正在创建一个网页,人们可以在其中订阅一些预定的 public 活动。

数据库table是Persons -> Subscriptions -> Events -> Days -> Activities

限制条件:

此人可以订阅每个可用的活动,只要这些活动在一天或多天内没有一项或多项共同活动即可。

解决方法:

因此,当用户尝试订阅一个事件时,数据库应该迭代 he/she 已经订阅的其他活动事件,寻找同一天的事件,使用 StartMomentEndMoment 这个 Days 的字段 table.

如何:

我是 Oracle 的新手。我习惯了 Firebird 和 MySQL,我以前从不需要使用 iterators。我只是在我的编程代码中做了那个检查之王,但现在我希望这个逻辑在数据库中独立于平台。

谁能说出最聪明的方法是什么,代码不会变得太乱太慢?

数据(简体):

  Table Persons: Id, name(varchar), identify(varchar), phone(varchar);
  Table Subscriptions: Id, personId, eventId, reg_date(date);
  Table Events: Id, description(string), hours, contacts(string);
  Table Days: Id, eventId, day(date), begin(time), end(time), allday(bool);
  Table Activities: Id, dayId, eventId, begin(time), end(time), duration(0 for allday, or minutes), description(varchar);

如果您想遍历表中的每条记录,最简单的方法是使用 for rc in (select * from mytable) loop end loop; 语法。这是语句的隐式游标,请在此处查看更多信息 http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/static.htm#CHDBJBJE 你的逻辑是:

begin
  for rcPersons in (select * from Persons) loop
    for rcSubscriptions in (select * from Subscriptions where personId= rcPersons.Id) loop
       for rcEvents in (select * from Events where id = rcSubscriptions.eventId) loop
          null; --<do some logic here>      
       end loop;
    end loop;
  end loop;
end;

p.s if you provide scripts to create test tables and data it would be easy to find most effective solution. I think it could be done via one SQL, but need more info to be sure