从隐式 JOIN 返回行,其中结果存在于两个表中或仅存在于一个表中
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下内容对您有用吗?
Does the following work for you?
最简单的方法是构建一个返回正确行的视图,而不引用 buildlist 和奥斯维地图。然后将这两个表连接到视图中。
进而
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.
And then