【问题标题】:Count only null values in two different columns and show in one select statement仅计算两个不同列中的空值并显示在一个选择语句中
【发布时间】:2011-07-09 11:54:54
【问题描述】:

我只想计算特定列中的空值和另一列中的所有空值 特定列,但是我希望我的结果将这两个结果都显示在一个表中。

这是我目前所拥有的:

Select Count(*) as 'Column1Count', Count(*) as 'Column2Count'
   from table1
       Where column1 is null
     and column2 is null

请帮忙

【问题讨论】:

    标签: mysql sql database select count


    【解决方案1】:

    这应该可行:

    select
        (select count(*) from table1 where column1 is null) as 'Column1Count',
        (select count(*) from table1 where column2 is null) as 'Column2Count';
    

    【讨论】:

      【解决方案2】:

      您可以为此使用一个案例:

      select  sum(case when Column1 is null then 1 end) as Col1Count
      ,       sum(case when Column2 is null then 1 end) as Col2Count
      from    table1
      

      【讨论】:

      • 谢谢,这个查询中的情况我从未使用过,但这给了我正确的输出
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 2015-04-01
      • 2014-01-19
      相关资源
      最近更新 更多