1

我有这两张这样的桌子

test_table
date        student     test
2012-05-31  Alice       Math
2012-05-31  Alice       Math
2012-05-31  Bob         Math
2012-05-31  Bob         Spelling
2012-06-01  Alice       Math
2012-06-01  Alice       Spelling

score_table
date        student     test        best_daily_score
2012-05-31  Alice       Math        90
2012-05-31  Bob         Math        50
2012-05-31  Bob         Spelling    50
2012-06-01  Alice       Math        95

我想检测到 Alice 的best_daily_score测试Spelling尚未2012-06-01被记录。我正在寻找返回的查询

2012-06-01  Alice       Spelling

这是唯一的一行

SELECT DISTINCT date, student, test FROM test_table

那不在

SELECT DISTINCT date, student, test FROM score_table

这不起作用:

SELECT DISTINCT date, student, test FROM test_table WHERE date, student, test NOT IN (SELECT DISTINCT date, student, test FROM score_table)

我假设因为左侧NOT IN不应该是三件事的列表。

4

1 回答 1

5

试试这个:

SELECT  a.date,
        a.student,
        a.test
  FROM  test_table a 
          LEFT JOIN score_table b
            ON (a.date = b.date) AND
               (a.student = b.student) AND
               (a.test = b.test)
WHERE   b.best_daily_score IS NULL

由于. test_table_ score_table_ LEFT JOIN它匹配date,studenttest. 如果记录上没有找到score_table,那么基本上best_daily_score就可以了NULL。这就是为什么我添加了一个仅显示NULL在的条件best_daily_score

单击此处获取一些演示(SQLFiddle)

于 2012-06-28T00:36:46.080 回答