0

我有一个像这样的 sql 语句:

SELECT a.status_id,
       (SELECT batch_name
        FROM   pf_req_batch
        WHERE  batch_id = (SELECT batch_id
                           FROM   pf_m_req_batch
                           WHERE  request_id = 282)) AS BATCH_NAME
FROM   pf_request
WHERE  request_id = 282; 

我需要使用 JOIN 对其进行转换。帮助,请!

4

5 回答 5

0
    SELECT PR.STATUS_ID, PRB.BATCH_NAME as BATCH_NAME 
        FROM PF_REQ_BATCH PRB, PF_M_REQ_BATCH PMRB,PF_REQUEST PR 
        WHERE PRB.BATCH_ID = PMRB.BATCH_ID 
        AND PMRB.REQUEST_ID = PR.REQUEST_ID 
        AND PR.REQUEST_ID=282;
于 2013-05-15T11:21:13.620 回答
0
SELECT A.STATUS_ID, b.BATCH_NAME  
FROM PF_REQUEST A  inner join PF_REQ_BATCH as b
on A.Batch_ID = b.Batch_Id
    WHERE A.REQUEST_ID=282;
于 2013-05-15T11:21:18.823 回答
0

你可以试试这个:

SELECT R.STATUS_ID, B.BATCH_NAME
FROM PF_REQUEST R
INNER JOIN PF_REQ_BATCH B ON B.BATCH_ID = R.PF_M_REQ_BATCH.BATCH_ID
WHERE REQUEST_ID=282;
于 2013-05-15T11:19:22.203 回答
0

假设该表pf_m_req_batch是一个联结表并且组合(request_id, batch_id)是唯一的,这将是:

SELECT r.status_id,
       b.batch_name
FROM   pf_request  r
  LEFT JOIN pf_m_req_batch  rb
    ON rb.request_id = r.request_id
  LEFT JOIN pf_req_batch  b
    ON rb.batch_id = b.batch_id 
WHERE  r.request_id = 282 ; 

如果这不是唯一的(这更有可能),那么此查询将显示许多行,与请求 282 关联的每个批次对应一个,而您的查询将引发错误。

于 2013-05-15T11:53:45.640 回答
0

您涉及三个表。但是,您似乎不需要pf_req_batch,因为request_id两者都在pf_requestpf_m_req_batch中。

此代码正在表batch_namepf_m_req_batch查找。这是进行连接的一种方法:

select r.status_id
from pf_request r left outer join
     pf_req_batch rb
     on r.request_id = rb.request_id 
where r.request_id = 282;

注意使用left outer join. 原始数据保留所有匹配的记录pf_request。此结构可能会产生重复(如果 中有多个匹配的行pf_m_req_batch。但是,根据您的原始查询,这不应该发生在您的情况下。如果发生,您会收到子查询错误(类似于返回多行的子查询) .

于 2013-05-15T11:27:04.897 回答