LEFT JOIN 不从 MS Access 中的左表返回完整行?
Testcases table
---------------
ID Testcase
1 TC-1
2 TC-5
3 TC-8
Tests table
-----------
ID TestCaseID Result Release
1 1 OK 1.1.111
2 3 FAIL 1.1.111
我想要得到的是
Testcase Result
TC-1 OK
TC-5 <empty>
TC-8 FAIL
我得到的是
Testcase Result
TC-1 OK
TC-8 FAIL
查询:
SELECT Testcases.Testcase, Tests.Result
FROM Testcases LEFT JOIN Tests ON Testcases.ID=Tests.TestCaseID
WHERE Tests.Release="1.1.111";
Testcases table
---------------
ID Testcase
1 TC-1
2 TC-5
3 TC-8
Tests table
-----------
ID TestCaseID Result Release
1 1 OK 1.1.111
2 3 FAIL 1.1.111
What I want to get is
Testcase Result
TC-1 OK
TC-5 <empty>
TC-8 FAIL
What I get is
Testcase Result
TC-1 OK
TC-8 FAIL
Query:
SELECT Testcases.Testcase, Tests.Result
FROM Testcases LEFT JOIN Tests ON Testcases.ID=Tests.TestCaseID
WHERE Tests.Release="1.1.111";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将过滤器放入连接条件中,以便将其作为连接的一部分应用,而不是事后过滤。例如:
Put the filter into your join criteria, so that it's applied as part of the join, rather than filtered afterwards. e.g.:
有两种(微妙地)不同的方法可以做到这一点:
并且:
在表
Testcases
中再插入一行,其中ID=4, Testcase=20
并在表
Testcases
中插入一行。 code>Tests,使用TestCaseID=4 Result="Whatever" Release="2.2.37"
查看两个选项之间的差异。简而言之,第一个查询将显示所有测试用例,仅显示具有
Release="1.1.111"
的测试结果,其余测试用例将结果显示为空(NULL)。第二个将仅显示具有
Release="1.1.111"
测试的测试用例。还有所有没有任何测试的测试用例。注意: 第一个查询无法在 Access 的“设计”模式中显示。您可以将其保存为 SQL 模式,但如果您关闭并重新打开它,Access 会出于未知原因删除一些括号。不过你仍然可以运行它。
它(第一个查询)也可以写为:
或者
甚至更好(因为它可以在设计模式下显示):
There are two (subtly) different ways to do that:
and:
Insert one more row into table
Testcases
, withID=4, Testcase=20
and a row into table
Tests
, withTestCaseID=4 Result="Whatever" Release="2.2.37"
to see the difference between the 2 options.In short, first query will show all Testscases, with results shown only for tests having
Release="1.1.111"
, the rest testcases will show Results as empty (NULL).The second will show only Testscases having tests with
Release="1.1.111"
. And also all Testcase without any test.Note: The 1st query cannot be shown in Access' "Design" mode. You can save it in SQL mode but it appears that if you close and reopen it, Access erases some parentheses for unknown reasons. You can still run it though.
It (1st query) can also be written as:
or
or even better (because it can be shown in Design mode):