关于NVL功能

时间:2016-10-13 18:37:43

标签: sql oracle

我使用带NVL的简单SQL,但查询未正确显示结果。我的要求是,如果Shipment_refnum_qual_gid是' ULA / SAO.UL_ONU'然后获取此refnum的值,否则在结果中显示NULL

示例查询如下。

SELECT NVL (SRF.SHIPMENT_REFNUM_VALUE, 'NULL') 
FROM SHIPMENT_REFNUM SRF, SHIPMENT SH 
WHERE SRF.SHIPMENT_GID= SH.SHIPMENT_GID AND SH.SHIPMENT_GID = 'ULA/SAO.5000057288' 
AND SRF.SHIPMENT_REFNUM_QUAL_GID = 'ULA/SAO.UL_ONU

1 个答案:

答案 0 :(得分:0)

MySQL没有NVL功能。相反,请使用IFNULL

select ifnull(SRF.SHIPMENT_REFNUM_VALUE, 'NULL')
  from SHIPMENT_REFNUM SRF, SHIPMENT SH
  where SRF.SHIPMENT_GID= SH.SHIPMENT_GID
    and SH.SHIPMENT_GID = 'ULA/SAO.5000057288'
    and SRF.SHIPMENT_REFNUM_QUAL_GID = 'ULA/SAO.UL_ONU'

修改

正如您在评论中所说,SHIPMENT_REFNUM中的SHIPMENT中的一个或多个值中没有匹配的条目,但是您希望包含这些条目,您需要一个外部联接:< / p>

select ifnull(SRF.SHIPMENT_REFNUM_VALUE, 'NULL')
  from SHIPMENT_REFNUM SRF
  right outer join SHIPMENT SH on SRF.SHIPMENT_GID= SH.SHIPMENT_GID
  where SH.SHIPMENT_GID = 'ULA/SAO.5000057288'
    and SRF.SHIPMENT_REFNUM_QUAL_GID = 'ULA/SAO.UL_ONU'