即使数据库中没有结果,也选择带有结果的语句

时间:2013-07-08 18:58:12

标签: sql select case

我有这个表格,其中包含我对特定客户订单的所有附件,我想只选择一种附件,但即使没有附件,我也希望看到该行。有办法吗?

我尝试了这个,但它不起作用:

SELECT 
  Comm.No_Com, 
    CASE WHEN Attachement.EntityID IS NULL OR Attachement.ATS_CategoryOfAttachmentCode='DESSIN'
        THEN Attachement.FileName
        ELSE ' '
    END AS Dessin

    FROM Comm
    INNER JOIN Attachement
    ON Comm.COH_Id=Attachement.EntityID

1 个答案:

答案 0 :(得分:2)

使用LEFT OUTER JOIN而不是INNER JOIN。

SELECT 
    Comm.No_Com, 
    CASE WHEN Attachement.EntityID IS NULL OR Attachement.ATS_CategoryOfAttachmentCode='DESSIN'
        THEN Attachement.FileName
        ELSE ' '
    END AS Dessin
FROM Comm
LEFT OUTER JOIN Attachement
ON Comm.COH_Id=Attachement.EntityID AND Attachment.Kind = 'DesiredKind'