我正在为以下查询寻找mysql等价物:
(select course_id
from section
where semester = 'Fall' and year= 2009)
except
(select course_id
from section
where semester = 'Spring' and year= 2010);
其中截面表为:
+-----------+--------+----------+------+----------+-------------+--------------+
| 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 |
+-----------+--------+----------+------+----------+-------------+--------------+
换句话说,我想找到2009年秋季学期教授的所有课程,但不是2010年 Spring 学期。
4条答案
按热度按时间wztqucjr1#
mysql不支持
except
,所以就用not exists
或者not in
:(我更喜欢
not exists
因为它对NULL
值。)这不准确,因为
except
删除重复项。你可以用select distinct
但我怀疑这是否真的需要。iszxjhcz2#
使用
not in
因为except
在mysql中不可用w6mmgewl3#
我会用
not exists
:bf1o4zei4#
如果可能的话,我喜欢使用连接,在这里这当然是可能的。