我有两个表view_shipment_order_release和order_release_remark。对于给定的order_release_gid,order_release_remark中没有记录,则没有显示数据。我想在view_shipment_order_release中显示数据,即使order_release_remark中没有数据也是如此。我怎样才能做到这一点?我的查询如下所示。
select distinct
vsor.shipment_gid,
vsor.order_release_gid,
orem1.remark_text as Related_Party,
orem2.remark_text as ULTIMATE_CONSIGNEE_TYPE,
orem3.remark_text as CONSIGNEE_TYPE
from
order_release_remark orem1,
order_release_remark orem2,
order_release_remark orem3,
view_shipment_order_release vsor
--order_release_gid in('GECORP.18460727','GECORP.18435030','GECORP.18439869')
where
orem1.REMARK_QUAL_GID ='GECORP.CONSIGNEE TYPE'
and orem1.order_release_gid=vsor.order_release_gid
and orem2.REMARK_QUAL_GID ='GECORP.RELATED PARTY'
and orem2.order_release_gid=vsor.order_release_gid
and orem3.REMARK_QUAL_GID ='GECORP.ULTIMATE CONSIGNEE TYPE'
and orem3.order_release_gid=vsor.order_release_gid
and vsor.shipment_gid='GECORP.101027274'
答案 0 :(得分:1)
请改用左连接。左连接不会很严格,即使关联方没有数据也会显示。
select distinct
vsor.shipment_gid,
vsor.order_release_gid,
orem1.remark_text as Related_Party,
orem2.remark_text as ULTIMATE_CONSIGNEE_TYPE,
orem3.remark_text as CONSIGNEE_TYPE
from view_shipment_order_release vsor
LEFT JOIN order_release_remark orem1
ON orem1.REMARK_QUAL_GID ='GECORP.CONSIGNEE TYPE'
and orem1.order_release_gid=vsor.order_release_gid
LEFT JOIN order_release_remark orem2
ON orem2.REMARK_QUAL_GID ='GECORP.RELATED PARTY'
and orem2.order_release_gid=vsor.order_release_gid
LEFT JOIN order_release_remark orem3
ON orem3.REMARK_QUAL_GID ='GECORP.ULTIMATE CONSIGNEE TYPE'
and orem3.order_release_gid=vsor.order_release_gid
where vsor.shipment_gid='GECORP.101027274';
答案 1 :(得分:0)
我只能假设您实际上是指order_release_remark.remark_text - 在您的输出中称为Related_Party。
如果情况确实如此 - 此查询中没有任何内容可以解释此类行为。
但是,您的查询基于视图 - view_shipment_order_release(至少我希望它是一个视图)。视图只是select语句的名称 - 这意味着oracle将视图中记录的查询与您发布的select语句组合在一起。我的猜测是,该视图中有一些内容表明order_release_remark.remark_text不能为空。或者可能是它的空的记录在order_release_gid中没有值?
请记住,oracle认为null不等于null - 因此如果两个记录中不存在order_release_gid(为空),则加入此字段将不会返回任何行。