如何在postgresql中加入2个子查询?

时间:2015-05-27 02:50:37

标签: sql postgresql

我有4个表:T1,T2,T3,T4

--T1:               
    Info_Id | fname | lname
    101     | rol   | lum 

--T2:
    Acc_id | acc_info_id | acc_property
    01     | 101         |  land       

--T3:
    Bill_id | status    | amt_2_paid |  Acc_id
    123     |  UNPAID   |  200       | 01   

--T4:  
    Accnt_bill | OR      |  Acc_id
    123        | 01234   |  01

我有两个子查询,如下所示:

SELECT 
      fname,
      lname 
  FROM T1 
 WHERE Info_id IN(SELECT Acc_id FROM T2 WHERE acc_info_id = '01234')

第一个子查询仅显示fnamelname

SELECT 
      status,
      amt_2_paid 
 FROM T3 
WHERE Bill_id IN(SELECT Accnt_id FROM t4 WHERE OR = '01234')

第二个子查询仅显示statusamt_2_paid

我的问题是当我输入OR号时,如何加入两个子查询以显示fnamenamestatusamt_2_paid的值。

3 个答案:

答案 0 :(得分:2)

只需使用SELECT T1.fname, T1.lname, T3.status, T3.amt_2_paid FROM T1 INNER JOIN T2 ON T1.Info_id=T2.acc_info_id INNER JOIN T3 ON T2.Acc_id=T3.Acc_id INNER JOIN T4 ON T3. Bill_id=T4.Accnt_bill WHERE T4.[OR] ='01234' 将所有表格与一个查询相结合即可。见下文:

{{1}}

答案 1 :(得分:0)

 SELECT fname, name, status,amt_2_paid 
 FROM T3 
 INNER JOIN t4 ON t3.Bill_id = t4.Accnt_bill
 INNER JOIN t2 ON t2.Acc_id = t3.Acc_id
 INNER JOIN t1 ON t1.Info_id = t2.Info_Id
 WHERE t4."OR" = '01234'

答案 2 :(得分:0)

var article = Article()