从2个表中加入选择查询

时间:2020-09-28 10:43:21

标签: postgresql express

给出两个表(在同一数据库中): enter image description here

enter image description here

我想查询两个表的结果列表,其中WHERE环境= qa /按时间

排序的阶段

我正在使用Postgres DB和Express服务器。

预期结果:

build | qa | 2020-09-04 18:01:04.425261 | true 
test  | qa | 2020-09-04 22:46:50.862843 | @signUpHappyPath | 35530 | true
test  | qa | 2020-09-04 22:50:30.256647 | @passwordStrength| 6877  | true
build | qa | 2020-09-05 01:15:44.063051 | false
test  | qa | 2020-09-05 01:20:54.900635 | @shortseq        | 74450 | false

1 个答案:

答案 0 :(得分:1)

如果我正确理解了您的问题,则以下SQL应该可以有效地加入“ environment”字段中的2个表,将结果限制为环境字段为“ qa”或“ staging” ”,然后按时间升序排序:

SELECT Tab1.*, Tab2.*
FROM YourTableOne AS Tab1, YourTableTwo AS Tab2
WHERE (Tab1.environment = Tab2.environment)
AND ((Tab1.environment = 'qa') OR (Tab1.environment = 'staging'))
ORDER BY Tab1.time

或者,加入“时间”字段中的2个表,将结果限制在环境字段为“ qa”或“ staging”的位置,然后按时间升序排列:

SELECT Tab1.*, Tab2.*
FROM YourTableOne AS Tab1, YourTableTwo AS Tab2
WHERE (Tab1.time = Tab2.time)
AND ((Tab1.environment = 'qa') OR (Tab1.environment = 'staging'))
ORDER BY Tab1.time
相关问题