2

我有四张桌子:

CREATE TABLE days (
day text priamry key
);

这几天monday跑到saturday

CREATE TABLE times (
time time Primary Key,
peak text 
);

时间从上午 8:00 到晚上 9:00 以小时时段运行,例如 8:00、9:00、10:00、11:00

活动

CREATE TABLE activities (
activity text primary key
);

和规划师

CREATE TABLE planner (
day text foriegn key references days (day)
time time foriegn key references times (time)
activity text foriegn key references activities (activity)
member bigint
primary key (day, time, member)
);

计划表将包含以下数据:

friday, 09:00, squash_court1 , 2
friday, 09:00, squash_court2 , 3
friday, 09:00, squash_court3 , 1

我想要做的我列出了这三个法院的所有未预订时间所以我会有一个像这样的列表

time  activity
08:00 squash_court1
10:00 squash_court1
...rest of times...
08:00 squash_court2
10:00 squash_court2
...rest of times...
08:00 squash_court3
10:00 squash_court3
...rest of times...

这些在 8:00 到 10:00 之间不是 9:00 的原因是因为它已被预订

编辑

目前我有基本的加入:

SELECT time , activity FROM times, activities;

我现在需要的只是WHERE删除表中预订的子句planner

感谢您对此事的任何建议。

4

1 回答 1

3

我现在需要的只是WHERE删除表中预订的子句planner

这可以通过多种方式完成。LEFT JOIN / WHERE .. IS NULL通常在 PostgreSQL 中产生最快的计划:

SELECT t.time, a.activity
FROM   (days d CROSS JOIN  times t CROSS JOIN activities a)
LEFT   JOIN planner p ON (p.day, p.time, p.activity)
                       = (d.day, t.time, a.activity)
WHERE  p.activity IS NULL;

我使用括号来明确您想要CROSS JOIN(与表格之间的逗号相同)daystimesactivities首先。这些括号是多余的,因为默认情况下表是从左到右连接的。

JOIN条件只是以下形式的较短形式:

 ON p.day = d.day
AND p.time = t.time
AND p.activity = a.activity

另一种方法是NOT EXISTS半连接:

SELECT t.time, a.activity
FROM   (days d CROSS JOIN  times t CROSS JOIN activities a)
WHERE  NOT EXISTS (
   SELECT *
   FROM   planner 
   WHERE (p.day, p.time, p.activity)
       = (d.day, t.time, a.activity)
   );
于 2012-05-06T22:27:11.727 回答