我对此选择有疑问。如果有人帮忙,那就太棒了:
有一些名为BUNDLE, COURSE and BUNDLE_COURSE
BUNDLE
包含price, discount
等信息。
COURSE
包含title, icon_path
等信息
BUNDLE_COURSE
表是链接表,一个包含更多COURSES
。
示例:
BUNDLE | BUNDLE_COURSE| COURSE
--------+--------------+----------
1 | 1,5 | 5
2 | 1,6 | 6
| 1,7 | 7
| 2,5 | 8
| 2,7 |
| 2,8 |
我只需要获得BUNDLES,其中所有课程都符合条件
(例如:course_date > CURRENT_DATE
)
我该怎么做?
答案 0 :(得分:0)
您可以选择所有不存在不符合标准的课程包:
select *
from bundles b
where not exists
(
select *
from courses c
where c.id = b.course
and course_date < getdate() -- Note inverse condition
and ...
)
答案 1 :(得分:0)
您可以使用聚合和having
子句执行此操作。以下是使用您的条件的示例:
select b.bundleid
from bundle b join
bundle_course bc
on b.bundleid = bc.bundleid join
course c
on bc.courseid = c.courseid
group by b.bundleid
having min(c.coursedate) >= CURRENT_DATE;
如果您想添加更多条件,那很容易。例如,如果您想要具有上述条件的捆绑包和至少三个课程:
having min(c.coursedate) >= CURRENT_DATE and
count(*) >= 3;
如果您希望至少有一门课程在标题中加上“SQL”字样:
having min(c.coursedate) >= CURRENT_DATE and
count(*) >= 3 and
sum(case when c.title like '%SQL%' then 1 else 0 end) > 0;