2015-02-23 61 views
0

我正在使用以下SQL語句檢索工作訂單上的各種信息。然而,他們中的一些不具有(NULL)中列出的資產,在這種情況下,我得到0行:BIRT SQL Null案例場景

select PM17, WORKORDER.PMNUM, WORKORDER.ASSETNUM, ASSET.DESCRIPTION, ASSET.MANUFACTURER, EQASSETNUM, WORKORDER.LOCATION, LOCATIONS.DESCRIPTION, PM.ROUTE, TARGSTARTDATE, TARGCOMPDATE, PM.NEXTDATE 
from WORKORDER 
JOIN ASSET ON WORKORDER.ASSETNUM = ASSET.ASSETNUM 
JOIN PM ON WORKORDER.PMNUM = PM.PMNUM 
JOIN LOCATIONS ON WORKORDER.LOCATION = LOCATIONS.LOCATION 
WHERE WORKORDER.WONUM='1011'; 

如何修改我的發言就這兩種情況下工作(帶或不帶ASSETNUM) ?

情況1(ASSETNUM!= NULL) - >顯示選擇的所有字段

情況2(ASSETNUM = NULL) - >顯示了不依賴於ASSETNUM字段和用於其餘

返回null

回答

1

將您的JOIN更改爲LEFT JOIN

select PM17, WORKORDER.PMNUM, WORKORDER.ASSETNUM, ASSET.DESCRIPTION, ASSET.MANUFACTURER, EQASSETNUM, WORKORDER.LOCATION, LOCATIONS.DESCRIPTION, PM.ROUTE, TARGSTARTDATE, TARGCOMPDATE, PM.NEXTDATE 
from WORKORDER 
LEFT JOIN ASSET ON WORKORDER.ASSETNUM = ASSET.ASSETNUM 
JOIN PM ON WORKORDER.PMNUM = PM.PMNUM 
JOIN LOCATIONS ON WORKORDER.LOCATION = LOCATIONS.LOCATION 
WHERE WORKORDER.WONUM='1011';