删除Select语句中的空字段

时间:2015-11-06 17:33:42

标签: sql select where-clause

我有一个select语句如下:

SELECT
    firstName,
    lastName, 
    CASE
        WHEN primaryEmail <> 'name@example.com' THEN primaryEmail
        WHEN secondaryEmail <> 'name@example.com' THEN secondaryEmail 
        ELSE ternaryEmail 
    END AS email
FROM participant 
WHERE 
    primaryEmail <> 'name@example.com' 
    OR secondaryEmail <> 'name@example.com' 
    OR ternaryEmail <> 'name@example.com'

问题是有些记录包含空字段,firstName,lastName或生成的电子邮件。我将此添加到WHERE子句中:

firstName IS NOT NULL
AND lastName IS NOT NULL
AND primaryEmail <> // The already existing WHERE clause code.

但这些字段仍然显示出来。我还没有想过要删除电子邮件部分,所以我没有代码可以在那里展示。

3 个答案:

答案 0 :(得分:1)

   SELECT
        firstName,
        lastName, 
        CASE
            WHEN primaryEmail <> 'name@example.com' THEN primaryEmail
            WHEN secondaryEmail <> 'name@example.com' THEN secondaryEmail 
            ELSE ternaryEmail 
        END AS email
    FROM participant 
    WHERE 
        (primaryEmail <> 'name@example.com' AND primaryEmail IS NOT NULL) OR
        (secondaryEmail <> 'name@example.com' AND secondaryEmail IS NOT NULL) OR
        (ternaryEmail <> 'name@example.com' AND ternaryEmail IS NOT NULL)

答案 1 :(得分:0)

您需要or逻辑,而不是and

where 
(firstName is not null or lastName is not null)
and other conditions

答案 2 :(得分:0)

这个问题在T-SQL中很棘手。我确实找到了这个问题的答案,并且确切地了解了编译器如何查看空格的每个“类型”或NULL。

http://weblogs.sqlteam.com/mladenp/archive/2007/09/06/How_does_SQL_Server_really_store_NULL-s.aspx