左/右连接重写 - Oracle到Postgres迁移

时间:2018-02-01 11:58:54

标签: postgresql outer-join postgresql-9.4

select info.HOUSE_HOLD_ID as HOUSE_HOLD_ID,info.HOUSE_HOLD_NAME 
             as HOUSE_HOLD_NAME,city.CITY_NAME as CITY_NAME,info.FIELD_AREA as FIELD_AREA,
             cdar.resource_name as FIELD_AUDITOR_NAME,cdar.supervisor_name as SUPERVISOR_NAME,
             info.STATUS as STATUS,con.PHONE_NO AS PHONE_NO,con.CELL_NO AS CELL_NO
             from INFORMATION info,CONTACT con,CITY city,
             resource cdar
             where cdar.productid(+)=info.field_auditor_id
             and info.country_id=170 
             and con.country_id=170 and city.country_id=170 
             and info.HOUSE_HOLD_ID=con.HOUSE_HOLD_ID 
             and info.city_id=city.city_id
             and (upper(info.HOUSE_HOLD_ID) like upper('%%') 
                  or upper(info.HOUSE_HOLD_NAME) like upper('%%') or upper(city.CITY_NAME)
                  like upper('%%') or upper(con.PHONE_NO) like upper('%%') 
                  or upper(con.CELL_NO) like upper('%%')
                  or upper(info.FIELD_AREA) like upper('%%') 
                  OR upper(info.STATUS) in ('1','2','3','4') 
                  OR upper(cdar.RESOURCE_NAME) like upper('%%'))

我们正在将上述查询从Oracle转换为Postgres,我希望在查询之外单独编写cdar.productid(+)=info.field_auditor_id,就像在这里使用LEFT / RIGHT join一样。很抱歉从我们的应用中粘贴所有这样的代码。

1 个答案:

答案 0 :(得分:0)

将其转换为标准的SQL连接,如下所示:

SELECT ...
FROM information info
   JOIN contact con ON info.house_hold_id = con.house_hold_id
   JOIN city city ON info.city_id = city.city_id
   LEFT JOIN resource cdar ON cdar.productid = info.field_auditor_id
WHERE ...

WHERE子句中删除连接条件。