postgresql JOIN 具有多个条件

San*_*kar 3 sql postgresql

我有两个 postgres 表:

worker_details_verification (verification_id BIGSERIAL, worker_id BIGINT, 
state TEXT, proofs TEXT[])
worker_details(worker_id BIGINT, name TEXT)
Run Code Online (Sandbox Code Playgroud)

现在我想得到

    `verification_id, worker_id, proofs FROM` the table 
    `worker_details_verification`  
Run Code Online (Sandbox Code Playgroud)

限制记录 `WHERE state = 'Initial'

现在除了上述三列之外,我worker_details还想要表中的名称列,其中worker_id可用于查询工人的姓名。

我尝试了以下查询,但没有成功。

SELECT a.verification_id, a.worker_id, a.state, a.proofs, b.Name FROM 
worker_details_verification a FULL OUTER JOIN worker_details b ON 
a.worker_id = b.worker_id AND a.state = 'Initial';
Run Code Online (Sandbox Code Playgroud)

它返回甚至 a.state 都不是的记录'Initial',并且还返回一些错误的记录,其中所有namefrom都与for列worker_detail一起返回。NULLworker_details_verification

Mik*_*e R 7

在我看来,您需要的是左/右连接,而不是完全外连接,因为您正在从 Worker_Details_Verification 中查找数据,然后对其进行过滤,同时在适用的情况下获取 Worker_Details 。

我采取了这个:

SELECT a.verification_id, a.worker_id, a.state, a.proofs, b.Name 
FROM worker_details_verification a 
     FULL OUTER JOIN worker_details b ON a.worker_id = b.worker_id AND a.state = 'Initial';
Run Code Online (Sandbox Code Playgroud)

并把它变成这样:

SELECT a.verification_id, a.worker_id, a.state, a.proofs, b.Name 
FROM worker_details_verification a 
     LEFT OUTER JOIN worker_details b ON a.worker_id = b.worker_id 
WHERE a.state = 'Initial';
Run Code Online (Sandbox Code Playgroud)