LEFT JOIN 不从 MS Access 中的左表返回完整行?

发布于 2024-11-01 17:39:22 字数 566 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寒尘 2024-11-08 17:39:23

将过滤器放入连接条件中,以便将其作为连接的一部分应用,而不是事后过滤。例如:

SELECT Testcases.Testcase, Tests.Result
FROM (Testcases LEFT JOIN Tests ON ((Testcases.ID=Tests.TestCaseID)
 AND (Tests.Release="1.1.111")))

Put the filter into your join criteria, so that it's applied as part of the join, rather than filtered afterwards. e.g.:

SELECT Testcases.Testcase, Tests.Result
FROM (Testcases LEFT JOIN Tests ON ((Testcases.ID=Tests.TestCaseID)
 AND (Tests.Release="1.1.111")))
神经暖 2024-11-08 17:39:22

有两种(微妙地)不同的方法可以做到这一点:

SELECT Testcases.Testcase
     , Tests.Result
FROM Testcases
  LEFT JOIN Tests
    ON (  ( Testcases.ID = Tests.TestCaseID )
      AND ( Tests.Release = "1.1.111" )
       )

并且:

SELECT Testcases.Testcase
     , Tests.Result
FROM Testcases
  LEFT JOIN Tests
    ON Testcases.ID = Tests.TestCaseID
WHERE Tests.Release = "1.1.111"
   OR Tests.TestCaseID IS NULL

在表 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 会出于未知原因删除一些括号。不过你仍然可以运行它。

它(第一个查询)也可以写为:

SELECT Testcases.Testcase
     , g.Result
FROM Testcases
 LEFT JOIN
   ( SELECT * 
     FROM Tests
     WHERE ( Tests.Release = "1.1.111" )
   )
   AS g
 ON ( Testcases.ID = g.TestCaseID )

或者

SELECT Testcases.TestCase
     , Tests.Result
FROM Testcases
  INNER JOIN Tests
    ON ( Testcases.ID = Tests.TestCaseID )
WHERE ( Tests.Release = "1.1.111" ) 

UNION ALL 

SELECT Testcases.TestCase, NULL
FROM Testcases 
WHERE NOT EXISTS 
  ( SELECT 1
    FROM Tests
    WHERE ( Testcases.ID = Tests.TestCaseID )
      AND ( Tests.Release = "1.1.111" )
  )

甚至更好(因为它可以在设计模式下显示):

SELECT Testcases.Testcase
     , IIf((Tests.Release="1.1.111"), Tests.Result, Null)
       AS Result
FROM Testcases
  LEFT JOIN Tests
    ON Testcases.ID = Tests.TestCaseID
GROUP BY Testcases.Testcase
       , IIf((Tests.Release="1.1.111"), Tests.Result, Null)

There are two (subtly) different ways to do that:

SELECT Testcases.Testcase
     , Tests.Result
FROM Testcases
  LEFT JOIN Tests
    ON (  ( Testcases.ID = Tests.TestCaseID )
      AND ( Tests.Release = "1.1.111" )
       )

and:

SELECT Testcases.Testcase
     , Tests.Result
FROM Testcases
  LEFT JOIN Tests
    ON Testcases.ID = Tests.TestCaseID
WHERE Tests.Release = "1.1.111"
   OR Tests.TestCaseID IS NULL

Insert one more row into table Testcases, with ID=4, Testcase=20

and a row into table Tests, with TestCaseID=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:

SELECT Testcases.Testcase
     , g.Result
FROM Testcases
 LEFT JOIN
   ( SELECT * 
     FROM Tests
     WHERE ( Tests.Release = "1.1.111" )
   )
   AS g
 ON ( Testcases.ID = g.TestCaseID )

or

SELECT Testcases.TestCase
     , Tests.Result
FROM Testcases
  INNER JOIN Tests
    ON ( Testcases.ID = Tests.TestCaseID )
WHERE ( Tests.Release = "1.1.111" ) 

UNION ALL 

SELECT Testcases.TestCase, NULL
FROM Testcases 
WHERE NOT EXISTS 
  ( SELECT 1
    FROM Tests
    WHERE ( Testcases.ID = Tests.TestCaseID )
      AND ( Tests.Release = "1.1.111" )
  )

or even better (because it can be shown in Design mode):

SELECT Testcases.Testcase
     , IIf((Tests.Release="1.1.111"), Tests.Result, Null)
       AS Result
FROM Testcases
  LEFT JOIN Tests
    ON Testcases.ID = Tests.TestCaseID
GROUP BY Testcases.Testcase
       , IIf((Tests.Release="1.1.111"), Tests.Result, Null)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文