从隐式 JOIN 返回行,其中结果存在于两个表中或仅存在于一个表中

发布于 2024-12-19 03:04:26 字数 1570 浏览 0 评论 0原文

我有一个 PostgreSQL-9.0.x 数据库来管理自动化测试环境。有一堆表包含各种静态数据(操作系统版本、测试名称等),名为“buildlist”和“buildlist”。 'osversmap'。但是,还有两个表包含经常更改的数据。第一个是“待处理”表,它实际上是一个测试队列,其中待处理的测试由测试系统自行选择,然后在测试运行完成时删除。第二个是“结果”表,其中包含生成的测试结果(正在进行和已完成)。

待处理表中的记录与结果表中的记录具有一对多关系(待处理中的每一行在结果中可以有 0 或更多行)。例如,如果没有测试系统自行分配挂起行,则结果中的关联行将为零,然后一旦分配挂起行,结果中的行数将针对每个挂起行而增加。一个额外的问题是,我总是只想要与每个待处理表行关联的最新结果表行。我需要做的是查询“待处理”表中的待处理测试,然后从结果表中获取与每个待处理表行相对应的“logurl”。

所有这些都与这个问题非常相似,除了我有两个带有静态数据的附加表(buildlist 和 osversmap)的额外负担: PHP/SQL:仅使用一个查询,如果数据都在两个表中,则从两个表中选择行,如果没有,则仅从一个表中选择

我正在迷茫于如何将这两个表与静态数据集成到查询。只要“结果”表中至少有一行与挂起表中的每一行相对应,以下查询就可以正常工作(但是,对于仅存在于“挂起”中但尚未存在于“挂起”表中的行,它不会返回任何内容) '结果'):

SELECT 
 pending.cl,
 pending.id,
 pending.buildid,
 pending.build_type,
 pending.active,
 pending.submittracker,
 pending.os,pending.arch,
 pending.osversion,
 pending.branch,
 pending.comment,
 osversmap.osname,
 buildlist.buildname,
 results.logurl 
FROM pending ,osversmap ,buildlist ,results 
WHERE 
 pending.buildid=buildlist.id 
 AND pending.os=osversmap.os 
 AND pending.osversion=osversmap.osversion 
 AND pending.owner='$owner' 
 AND pending.completed='f' 
 AND results.hostname=pending.active 
 AND results.submittracker=pending.submittracker 
 AND pending.cl=results.cl 
 AND results.current_status!='PASSED' 
 AND results.current_status NOT LIKE '%FAILED' 
ORDER BY pending.submittracker,pending.branch,pending.os,pending.arch

提前致谢!

I've got a PostgreSQL-9.0.x database that manages an automated testing environment. There are a bunch of tables that contain assorted static data (OS versions, test names, etc) named 'buildlist' & 'osversmap'. However, there are also two tables which contain data which changes often. The first is a 'pending' table which is effectively a test queue where pending tests are self-selected by the test systems, and then deleted when the test run has completed. The second is a 'results' table which contains the test results as they are produced (in progress and completed).

The records in the pending table have a one to many relationship with the records in the results table (each row in pending can have 0 or more rows in results). For example, if no test systems have self-assigned a pending row, then there will be zero associated rows in results, and then once a pending row is assigned, the number of rows in results will increase for each pending row. An added catch is that I always want only the newest results table row associated with each pending table row. What I need to do is query the 'pending' table for pending tests, and then also get a 'logurl' from the results table that corresponds to each pending table row.

All of this is rather similar to this problem, except that I have the added burden of the two additional tables with the static data (buildlist & osversmap):
PHP/SQL: Using only one query, SELECT rows from two tables if data is in both tables, or just SELECT from one table if not

I'm stumbling over how to integrate those two tables with static data into the query. The following query works fine as long as there's at least one row in the 'results' table that corresponds to each row in the pending table (however, it doesn't return anything for rows that only exist in 'pending' yet not yet in 'results'):

SELECT 
 pending.cl,
 pending.id,
 pending.buildid,
 pending.build_type,
 pending.active,
 pending.submittracker,
 pending.os,pending.arch,
 pending.osversion,
 pending.branch,
 pending.comment,
 osversmap.osname,
 buildlist.buildname,
 results.logurl 
FROM pending ,osversmap ,buildlist ,results 
WHERE 
 pending.buildid=buildlist.id 
 AND pending.os=osversmap.os 
 AND pending.osversion=osversmap.osversion 
 AND pending.owner='$owner' 
 AND pending.completed='f' 
 AND results.hostname=pending.active 
 AND results.submittracker=pending.submittracker 
 AND pending.cl=results.cl 
 AND results.current_status!='PASSED' 
 AND results.current_status NOT LIKE '%FAILED' 
ORDER BY pending.submittracker,pending.branch,pending.os,pending.arch

thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

夜清冷一曲。 2024-12-26 03:04:26

以下内容对您有用吗?

SELECT  pending.cl,
        pending.id,
        pending.buildid,
        pending.build_type,
        pending.active,
        pending.submittracker,
        pending.os,
        pending.arch,
        pending.osversion,
        pending.branch,
        pending.comment,
        osversmap.osname,
        buildlist.buildname,
        results.logurl
FROM pending
    JOIN osversmap
        ON ( pending.os = osversmap.os
            AND pending.osversion = osversmap.osversion )
    JOIN buildlist
        ON ( pending.buildid = buildlist.id )
    LEFT OUTER JOIN results
        ON ( pending.active = results.hostname
            AND pending.submittracker = results.submittracker
            AND pending.cl = results.cl 
            AND results.current_status != 'PASSED'
            AND results.current_status NOT LIKE '%FAILED'
            )
WHERE pending.owner = '$owner'
    AND pending.completed = 'f'
ORDER BY pending.submittracker,
    pending.branch,
    pending.os,
    pending.arch

Does the following work for you?

SELECT  pending.cl,
        pending.id,
        pending.buildid,
        pending.build_type,
        pending.active,
        pending.submittracker,
        pending.os,
        pending.arch,
        pending.osversion,
        pending.branch,
        pending.comment,
        osversmap.osname,
        buildlist.buildname,
        results.logurl
FROM pending
    JOIN osversmap
        ON ( pending.os = osversmap.os
            AND pending.osversion = osversmap.osversion )
    JOIN buildlist
        ON ( pending.buildid = buildlist.id )
    LEFT OUTER JOIN results
        ON ( pending.active = results.hostname
            AND pending.submittracker = results.submittracker
            AND pending.cl = results.cl 
            AND results.current_status != 'PASSED'
            AND results.current_status NOT LIKE '%FAILED'
            )
WHERE pending.owner = '$owner'
    AND pending.completed = 'f'
ORDER BY pending.submittracker,
    pending.branch,
    pending.os,
    pending.arch
莫相离 2024-12-26 03:04:26

所有这些都与这个问题非常相似,除了我有两个带有静态数据的附加表(buildlist 和 osversmap)的额外负担:

最简单的方法是构建一个返回正确行的视图,而不引用 buildlist 和奥斯维地图。然后将这两个表连接到视图中。

CREATE VIEW your-meaningful-view-name AS
SELECT 
 pending.cl,
 pending.id,
 pending.buildid,
 pending.build_type,
 pending.active,
 pending.submittracker,
 pending.os,pending.arch,
 pending.osversion,
 pending.branch,
 pending.comment,
 results.logurl 
FROM pending
-- No DDL or sample INSERT statements. You might need an outer join.
INNER JOIN results 
        ON (results.hostname=pending.active AND
            results.submittracker=pending.submittracker AND
            results.cl=pending.cl)
WHERE pending.owner='$owner' AND
      pending.completed='f' AND
      -- Are *both* these really necessary?
      results.current_status!='PASSED' AND
      results.current_status NOT LIKE '%FAILED' 

进而

SELECT  osversmap.osname, buildlist.buildname, ymvn.*,
FROM your-meaningful-view-name ymvn
INNER JOIN osversmap ON ymvn.os=osversmap.os  
                    AND ymvn.osversion=osversmap.osversion 
INNER JOIN buildlist ON ymvn.buildid=buildlist.id 
ORDER BY ymvn.submittracker, ymvn.branch, ymvn.os, ymvn.arch

All of this is rather similar to this problem, except that I have the added burden of the two additional tables with the static data (buildlist & osversmap):

Simplest approach would be to build a view that returns the right rows without referencing buildlist and osversmap. Then join those two tables to the view.

CREATE VIEW your-meaningful-view-name AS
SELECT 
 pending.cl,
 pending.id,
 pending.buildid,
 pending.build_type,
 pending.active,
 pending.submittracker,
 pending.os,pending.arch,
 pending.osversion,
 pending.branch,
 pending.comment,
 results.logurl 
FROM pending
-- No DDL or sample INSERT statements. You might need an outer join.
INNER JOIN results 
        ON (results.hostname=pending.active AND
            results.submittracker=pending.submittracker AND
            results.cl=pending.cl)
WHERE pending.owner='$owner' AND
      pending.completed='f' AND
      -- Are *both* these really necessary?
      results.current_status!='PASSED' AND
      results.current_status NOT LIKE '%FAILED' 

And then

SELECT  osversmap.osname, buildlist.buildname, ymvn.*,
FROM your-meaningful-view-name ymvn
INNER JOIN osversmap ON ymvn.os=osversmap.os  
                    AND ymvn.osversion=osversmap.osversion 
INNER JOIN buildlist ON ymvn.buildid=buildlist.id 
ORDER BY ymvn.submittracker, ymvn.branch, ymvn.os, ymvn.arch
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文