【问题标题】:Eliminating specific null values in sql select消除sql select中的特定空值
【发布时间】:2015-05-01 16:17:56
【问题描述】:

因此,我正在尝试创建一份报告,以生成最佳男性和/或最佳女性提名/获奖者。但是,当我运行我的选择时,我会得到所有没有获奖者或提名的电影。无法弄清楚如何摆脱空值。这是我的代码。顺便说一下,这是在 Oracle 中。

/*Oscar Nominations for Best Actor Report */
SELECT 
    movie.MVYEAR as "Movie Year", 
    movie.MVTITLE as "Movie Title",
    star.STARNAME as "Star Name", 
    movstar.BESTM as "Best Male", 
    movstar.BESTF as "Best Female"
FROM 
    movie, 
    star, 
    movstar
WHERE 
    star.STARNUM = movstar.STARNUM
AND 
    movie.MVNUM = movstar.MVNUM
ORDER BY movie.MVYEAR ASC;

但我的结果包含有获奖者和提名者的电影以及没有任何获奖者/提名的电影,导致查询显示空值。如何摆脱查询中出现的带有空值的结果/电影?

【问题讨论】:

    标签: sql oracle select null


    【解决方案1】:

    您需要添加 where 条件,但最好将联接与 where 子句分开。

          SELECT movie.MVYEAR as "Movie Year", 
                 movie.MVTITLE as "Movie Title", 
                 star.STARNAME as "Star Name", 
                 movstar.BESTM as "Best Male", movstar.BESTF as "Best Female"
            FROM movie
      INNER JOIN movstar 
              ON movie.MVNUM = movstar.MVNUM
      INNER JOIN star
              ON star.STARNUM = movstar.STARNUM
           WHERE movstar.BESTM IS NOT NULL 
              OR movstar.BESTF IS NOT NULL
        ORDER BY movie.MVYEAR ASC;
    

    【讨论】:

    • 非常感谢。我曾尝试在 where 子句中使用 not null,但我使用的是 AND 而不是 OR。同样从我目前所学到的知识来看,我没有将连接与 where 子句分开,但从现在开始这看起来是一个更好的主意。再次感谢您的帮助!
    猜你喜欢
    • 2016-12-23
    • 2015-02-02
    • 1970-01-01
    • 2020-08-21
    • 2014-10-07
    • 1970-01-01
    • 1970-01-01
    • 2022-06-22
    • 1970-01-01
    相关资源
    最近更新 更多