database - SQL SELECT only when all items match -
so have 2 tables:
courses: -course_id (primary key) -course_code -title sections: -section_id (primary key) -course_id (foreign key) -day
each course has number of sections belong it. let's use example.
the tables:
course_id course_code title 1 abc title1 2 bbc title2 section_id course_id day 1 1 monday 2 1 tuesday 3 2 monday 4 2 monday
i want able run query asks courses give me ones of sections fit criteria. in case, let's want see "all courses have of sections on monday". desired output be:
course_id course_code title section_id day 2 bbc title2 3 monday 2 bbc title2 4 monday
notice how entry (2, abc, title1, 1, monday) omitted? can't seem think of way this. in advance!
try this:
select * courses c1 not exists ( select 1 sections c2 c1.course_id = c2.course_id , c2.day <> 'monday' )
Comments
Post a Comment