Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL,除同等版本外_Mysql_Sql_Except - Fatal编程技术网

MySQL,除同等版本外

MySQL,除同等版本外,mysql,sql,except,Mysql,Sql,Except,我正在为以下查询寻找MySQL等价物: 选择课程号 从节 其中学期=‘秋季’和年份=2009年 除了 选择课程号 从节 其中学期=春季,年份=2010年; 其中截面表为: +-----------+--------+----------+------+----------+-------------+--------------+ | course_id | sec_id | semester | year | building | room_number | time_slot_id | +-

我正在为以下查询寻找MySQL等价物:

选择课程号 从节 其中学期=‘秋季’和年份=2009年 除了 选择课程号 从节 其中学期=春季,年份=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年春季学期教授的所有课程。

使用not In,因为mysql中不可用

select *
from section 
where courseid not in 
(
select courseid
from section
where semester = 'Spring' and year= 2010
) and semester = 'Fall' and year = 2009
MySQL不支持except,所以只需使用not exists或not in:

我更喜欢不存在,因为它对空值有更直观的支持

这并不准确,因为except会删除重复项。您可以使用select distinct,但我怀疑这是否真的需要。

我会使用“不存在”:


如果可能的话,我喜欢使用连接,这在这里当然是可能的

SELECT sFa.course_id
FROM section AS sFa
LEFT JOIN section AS sSpr 
   ON sFa.course_id = sSpr.course_id
   AND sSpr.semester = 'Spring' AND sSpr.year= 2010
WHERE sFa.semester = 'Fall' 
   AND sFa.year= 2009
   AND sSpr.course_id IS NULL
;

如果您经常以这种方式进行查询,那么创建一个简单、易于索引的列可能是有意义的,它是学期和年份的组合,例如2009年夏季的S2009或2010年春季的P2010。还要确保您对字符串值使用了正确的引号,正如问题中所说的那样,这些引号是智能引号,是错误的。谢谢!你是对的。我只是从书上抄的,忘了编辑。
select s.*
from section s
where semester = 'Fall' and year = 2009 and
      not exists (select 1 
                  from section s1
                  where s.course_id = s1.course_id and 
                        s1.semester = 'Spring' and s1.year = 2010
                 );
SELECT sFa.course_id
FROM section AS sFa
LEFT JOIN section AS sSpr 
   ON sFa.course_id = sSpr.course_id
   AND sSpr.semester = 'Spring' AND sSpr.year= 2010
WHERE sFa.semester = 'Fall' 
   AND sFa.year= 2009
   AND sSpr.course_id IS NULL
;