在mySQL中选择不同三元组的补码
我有两张这样的桌子在mySQL中选择不同三元组的补码,mysql,sql,database,Mysql,Sql,Database,我有两张这样的桌子 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
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在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的左侧不应该是一个包含三件事的列表。尝试一下:
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_表到score_表的所有记录,无论它是否存在。它匹配日期、学生和测试。如果在score_表中找不到该记录,那么基本上,best_daily_分数将为空。这就是为什么我添加了一个条件,在best_daily_分数上只显示NULL
尝试一下:
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_表到score_表的所有记录,无论它是否存在。它匹配日期、学生和测试。如果在score_表中找不到该记录,那么基本上,best_daily_分数将为空。这就是为什么我添加了一个条件,在best_daily_分数上只显示NULL