I am looking for MySQL equivalent or equivalents for the following query:
(select course_id
from section
where semester = 'Fall' and year= 2009)
except
(select course_id
from section
where semester = 'Spring' and year= 2010);
Run Code Online (Sandbox Code Playgroud)
where the section table is:
+-----------+--------+----------+------+----------+-------------+--------------+
| course_id | sec_id | semester | year | building | room_number | time_slot_id |
+-----------+--------+----------+------+----------+-------------+--------------+
| BIO-101 | 1 | Summer | 2009 | Painter | 514 | B |
| BIO-301 | 1 | Summer | 2010 | Painter | 514 | A |
| CS-101 | 1 | Fall | 2009 | Packard | 101 | H |
| CS-101 | 1 | Spring | 2010 | Packard | 101 | F |
| CS-190 | 1 | Spring | 2009 | Taylor | 3128 | E |
| CS-190 | 2 | Spring | 2009 | Taylor | 3128 | A |
| CS-315 | 1 | Spring | 2010 | Watson | 120 | D |
| CS-319 | 1 | Spring | 2010 | Watson | 100 | B |
| CS-319 | 2 | Spring | 2010 | Taylor | 3128 | C |
| CS-347 | 1 | Fall | 2009 | Taylor | 3128 | A |
| EE-181 | 1 | Spring | 2009 | Taylor | 3128 | C |
| EE-302 | 1 | Summer | 2010 | Watson | 327 | C |
| FIN-201 | 1 | Spring | 2010 | Packard | 101 | B |
| HIS-351 | 1 | Spring | 2010 | Painter | 514 | C |
| MU-199 | 1 | Spring | 2010 | Packard | 101 | D |
| PHY-101 | 1 | Fall | 2009 | Watson | 100 | A |
+-----------+--------+----------+------+----------+-------------+--------------+
Run Code Online (Sandbox Code Playgroud)
In other words I want to find all courses taught in the Fall 2009 semester but not in the Spring 2010 semester.
MySQL doesn't support except, so just use not exists or not in:
select courseid
from section sf
where semester = 'Fall' and year = 2009 and not exists
(select 1
from section ss
where sf.courseid = ss.courseid and ss.semester = 'Spring' and ss.year = 2010
);
Run Code Online (Sandbox Code Playgroud)
(我更喜欢,not exists因为它对价值观有更直观的支持NULL。)
这并不准确,因为except删除了重复项。你可以使用select distinct,但我怀疑这是否真的需要。
| 归档时间: |
|
| 查看次数: |
4430 次 |
| 最近记录: |