在assosiative表上的mysql外连接(未选中)

时间:2016-01-09 04:10:42

标签: mysql left-join where outer-join

我有3个表='测试','测试','主题'

表格测试

idtest | Test
1      | Test wave 1
2      | Test Wave 2

表格测试

idtest | idsubject
1      | 1
1      | 2
1      | 3
2      | 1

表主题

  idsubject| subject
    1      | Math
    2      | Bio
    3      | Sport
    4      | Literature

我需要的是选择外部(某些测试中未选择的主题), 例如,在idtest = 1上,结果将是文献

 idsubject | subject
    4      | Literature

不知何故,我已经尝试过使用left,outer didn&work,而不是结果(选择/选择)主题。

SELECT subject.idsubject, subject FROM subject right OUTER JOIN test_quo ON (subject.idsubject = test_quo.idsubject) where test_quo.idtest=1'

查询我会得到结果

idsubject | subject
       1  | Math
       2  | Bio
       3  | Sport

1 个答案:

答案 0 :(得分:0)

我相信你需要下面的东西。它会给你"未被选中"所有测试的科目。

ParseQuery<ParseObject> roomQuery = ParseQuery.getQuery("Room");
        roomQuery.whereEqualTo("course" , courseName);
        roomQuery.whereEqualTo("number" , Integer.parseInt(courseNumber));

DEMO

仅针对SELECT subject.idsubject, subject FROM subject LEFT OUTER JOIN test_quo ON (subject.idsubject = test_quo.idsubject) WHERE ISNULL(test_quo.idsubject) 我想出了以下相关的子查询:

idtest=1

Another Demo

和(最后)和OUTER JOIN相同:

SELECT s.idsubject, subject 
FROM subject s
WHERE NOT EXISTS (SELECT 1 
                  FROM test_quo t 
                  WHERE s.idsubject = t.idsubject AND t.idtest = 1)

Demo with one more subject