Return 行,如果没有返回任何行,则为空值

Return row with null values if no rows returned

如果一个值 returned 查询 return 没有行,如何 return NULL 有很多问题。但是当没有结果被 returned.

时,我没有找到任何关于多值查询 returning NULL 的信息

主查询:

SELECT UserOpinion.*, x, y, z... FROM subquery, (x) AS x, (y) AS y...
I trust you get the gist.

我的查询是一个子查询:

(SELECT TOP 1                       
        u.BADId,                        
        Date,                       
        State,                      
        Note,                       
        Comment,                        
        Alias,                      
        Title,                      
        UserId,
        o.Id AS OpinionId                   
    FROM                        
        [Opinion] o                 
    RIGHT JOIN                      
        [User] u                        
        ON                          
        o.UserId = u.Id                     
        WHERE                       
        (Date IS NULL OR (Date = (SELECT MAX(Date)                              
                                FROM [Opinion]                              
                                WHERE UserId = o.UserId )))
        AND                             
            u.BADId = 'myvalue') AS UserOpinion; 

例如我试过以下方法:

(SELECT TOP 1 * FROM (SELECT TOP 1                      
        u.BADId,                        
        Date,                       
        State,                      
        Note,                       
        Comment,                        
        Alias,                      
        Title,                      
        UserId,
        o.Id AS OpinionId                   
    FROM                        
        [Opinion] o                 
    RIGHT JOIN                      
        [User] u                        
        ON                          
        o.UserId = u.Id                     
        WHERE                       
        (Date IS NULL OR (Date = (SELECT MAX(Date)                              
                                FROM [Opinion]                              
                                WHERE UserId = o.UserId )))
        AND                             
            u.BADId = 'myvalue' 
        UNION SELECT NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL) t) AS UserOpinion; 

此 return 为 NULL,无论“myvalue”是否存在。我想要的是,如果条目存在,它 returns 查询的值,如果不存在,它在每一列上 returns null。

我还尝试了以下方法:

(COALESCE(SELECT TOP 1                      
    u.BADId,                        
    Date,                       
    State,                      
    Note,                       
    Comment,                        
    Alias,                      
    Title,                      
    UserId,
    o.Id AS OpinionId                   
FROM                        
    [Opinion] o                 
RIGHT JOIN                      
    [User] u                        
    ON                          
    o.UserId = u.Id                     
    WHERE                       
    (Date IS NULL OR (Date = (SELECT MAX(Date)                              
                            FROM [Opinion]                              
                            WHERE UserId = o.UserId )))
    AND                             
        u.BADId = 'myvalue'), NULL) AS UserOpinion; 

这里的主要问题是,如果“myvalue”没有匹配项,则整个查询 return 什么都没有。其他查询不需要 myvalue 并且进行多个独立查询不是性能限制的选项。

我远不是 SQL 方面的专家,任何正确方向的建议将不胜感激。

类似但不适用于此处的问题:

您可以使用 left outer join 来做到这一点:

with s as (<your subquery here>)
select s.*
from (select 1 as x) x left join
     s
     on x.x = 1;

这将 return 您的 table 中的值。如果没有匹配项,则查询 return 一行,所有列均为 NULL

注意:CTE 不是必需的。我只是为了强调查询的逻辑而放入它。您可以将子查询放在 s 所在的位置。