我使用以下SQL语句检索工单上的各种信息。但是,他们中的一些人没有列出资产(NULL),在这种情况下,我得到0行:
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
答案 0 :(得分: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';