如何删除具有所有NULL值的行

时间:2019-02-28 09:29:05

标签: sql-server

如何删除表中的空行?

enter image description here

我编写了一个查询,以连接来自不同表的几列(有效)。但是,我想删除表格底部的许多空行。

SELECT  
s.Name_of_owner, 
s.pos, 
s.Orientation,
isnull(s.DBName,z.DBName) DBName,
isnull (s.Name,z.Name) Name,
isnull (s.Refno,z.Refno) Refno,
isnull (s.Type_,z.Type_) Type_,
isnull (s.Desc_,z.Desc_) Desc_,
isnull (s.mcount, z.mcount) mcount,
z.Name_of_Site,
z.Desc_of_Site
FROM SITE s  
FULL OUTER JOIN ZONE z ON z.Name = s.Name 
FULL OUTER JOIN SITE ON z.DBName = s.DBName

2 个答案:

答案 0 :(得分:0)

使用INNER JOIN作为inner join匹配现有值的表:

SELECT  
s.Name_of_owner, 
s.pos, 
s.Orientation,
isnull(s.DBName,z.DBName) DBName,
isnull (s.Name,z.Name) Name,
isnull (s.Refno,z.Refno) Refno,
isnull (s.Type_,z.Type_) Type_,
isnull (s.Desc_,z.Desc_) Desc_,
isnull (s.mcount, z.mcount) mcount,
z.Name_of_Site,
z.Desc_of_Site
FROM SITE s  
INNER JOIN ZONE z ON z.Name = s.Name 
INNER JOIN SITE ON z.DBName = s.DBName

答案 1 :(得分:0)

只需使用WHERE过滤器:

SELECT  
    s.Name_of_owner, 
    s.pos, 
    s.Orientation,
    isnull(s.DBName,z.DBName) DBName,
    isnull (s.Name,z.Name) Name,
    isnull (s.Refno,z.Refno) Refno,
    isnull (s.Type_,z.Type_) Type_,
    isnull (s.Desc_,z.Desc_) Desc_,
    isnull (s.mcount, z.mcount) mcount,
    z.Name_of_Site,
    z.Desc_of_Site
FROM 
    SITE s  
    FULL OUTER JOIN ZONE z ON z.Name = s.Name 
    FULL OUTER JOIN SITE ON z.DBName = s.DBName
WHERE
    s.Name_of_owner IS NOT NULL

如果要确保所有列都不是NULL,则必须编写一堆AND过滤器:

SELECT  
    s.Name_of_owner, 
    s.pos, 
    s.Orientation,
    isnull(s.DBName,z.DBName) DBName,
    isnull (s.Name,z.Name) Name,
    isnull (s.Refno,z.Refno) Refno,
    isnull (s.Type_,z.Type_) Type_,
    isnull (s.Desc_,z.Desc_) Desc_,
    isnull (s.mcount, z.mcount) mcount,
    z.Name_of_Site,
    z.Desc_of_Site
FROM 
    SITE s  
    FULL OUTER JOIN ZONE z ON z.Name = s.Name 
    FULL OUTER JOIN SITE ON z.DBName = s.DBName
WHERE
    s.Name_of_owner IS NOT NULL AND
    s.pos IS NOT NULL AND
    (S.DBName IS NOT NULL OR z.DBName IS NOT NULL) AND -- isnull(s.DBName,z.DBName)
    -- ....

请记住,您正在使用FULL JOIN,因此可能会有完整的表在它们不匹配时产生NULL值。因此,强制对特定列(来自特定表)的过滤器可能从其他“完全联接”表中删除行。您可以进行OR过滤器来检查多个表中的值(如本例所示)。