Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 获取列值不为NULL的每个列计数_Mysql_Count - Fatal编程技术网

Mysql 获取列值不为NULL的每个列计数

Mysql 获取列值不为NULL的每个列计数,mysql,count,Mysql,Count,我有一个表和表中的5列。我想要列值不为null的每列的行数 column1 column2 column3 column4 column5 1 2 2 2 2 2 2 2 NULL 2 3 NULL 2 2 NULL NULL NULL 2 2 NULL NULL NULL 2

我有一个表和表中的5列。我想要列值不为null的每列的行数

column1 column2 column3 column4 column5
1        2        2       2        2
2        2        2       NULL     2     
3        NULL     2       2        NULL
NULL     NULL     2       2        NULL
NULL     NULL     2       2        NULL

我应该得到像3,2,5,4,2这样的输出

SELECT 
COUNT(Column1),
COUNT(Column2),
COUNT(Column3),
COUNT(Column4),
COUNT(Column5)
FROM Table1

返回行中expr的非空值数的计数 由SELECT语句检索

COUNT(*)有些不同,因为它返回 检索的行数,无论它们是否包含空值


像这样的怎么样

SELECT 
COUNT(Column1),
COUNT(Column2),
COUNT(Column3),
COUNT(Column4),
COUNT(Column5)
FROM Table1

返回行中expr的非空值数的计数 由SELECT语句检索

COUNT(*)有些不同,因为它返回 检索的行数,无论它们是否包含空值


您可以使用子查询:

select column1, column2, column3, column4, column5
from ( select
  sum(column1 is not null) as column1,
  sum(column2 is not null) as column2,
  sum(column3 is not null) as column3,
  sum(column4 is not null) as column4,
  sum(column5 is not null) as column5
from mytable) x

您可以使用子查询:

select column1, column2, column3, column4, column5
from ( select
  sum(column1 is not null) as column1,
  sum(column2 is not null) as column2,
  sum(column3 is not null) as column3,
  sum(column4 is not null) as column4,
  sum(column5 is not null) as column5
from mytable) x
根据函数引用,它只统计非
NULL
项。所以

选择
将(第1列)计数为第1列,
将(第2列)计数为第2列,
将(第3列)计数为第3列,
将(第4列)计数为第4列,
将(第5列)计数为第5列
从你的桌子上;
应该返回您想要的信息。

根据函数引用,它只统计非空项。所以

选择
将(第1列)计数为第1列,
将(第2列)计数为第2列,
将(第3列)计数为第3列,
将(第4列)计数为第4列,
将(第5列)计数为第5列
从你的桌子上;

应将所需信息返回给您。

提供的解决方案对我不起作用。我不得不修改代码如下:

SELECT 
COUNT(NULLIF(Column1,'')),
COUNT(NULLIF(Column2,'')),
COUNT(NULLIF(Column3,'')),
COUNT(NULLIF(Column4,'')),
COUNT(NULLIF(Column5,''))
FROM Table1

提供的解决方案对我不起作用。我不得不修改代码如下:

SELECT 
COUNT(NULLIF(Column1,'')),
COUNT(NULLIF(Column2,'')),
COUNT(NULLIF(Column3,'')),
COUNT(NULLIF(Column4,'')),
COUNT(NULLIF(Column5,''))
FROM Table1

我支持上面的回答——只需确保字段名在两个区域中的顺序匹配即可

select column1, column2, column3, column4, column5

from ( select

  sum(column1 is not null) as column1,

  sum(column2 is not null) as column2,

  sum(column3 is not null) as column3,

  sum(column4 is not null) as column4,

  sum(column5 is not null) as column5

from mytable) x

我支持上面的回答——只需确保字段名在两个区域中的顺序匹配即可

select column1, column2, column3, column4, column5

from ( select

  sum(column1 is not null) as column1,

  sum(column2 is not null) as column2,

  sum(column3 is not null) as column3,

  sum(column4 is not null) as column4,

  sum(column5 is not null) as column5

from mytable) x

如果CalnN1是空的,但有一个默认值,即“没有”,考虑使用nulif(CulnN1,'NOn')。如果CulnN1是空的,但具有缺省值I.“NONE”,请考虑使用nulif(CulnN1,'NOn')。