在MySQL中,计数情况和语句。

时间:2022-12-13 17:23:51

How to use COUNT CASE and WHEN statement in MySQL query, to count when data is NULL and when it is not NULL in one MySQL query?

如何在MySQL查询中使用COUNT CASE和WHEN语句,当数据为NULL时,以及在一个MySQL查询中不为NULL时如何计数?

1 个解决方案

#1


60  

Use:

使用:

SELECT SUM(CASE 
             WHEN t.your_column IS NULL THEN 1
             ELSE 0
           END) AS numNull,
       SUM(CASE 
             WHEN t.your_column IS NOT NULL THEN 1
             ELSE 0
           END) AS numNotNull
  FROM YOUR_TABLE t

That will sum up the column NULL & not NULL for the entire table. It's likely you need a GROUP BY clause, depending on needs.

这将汇总整个表的列NULL & not NULL。根据需要,您可能需要一个GROUP BY子句。

#1


60  

Use:

使用:

SELECT SUM(CASE 
             WHEN t.your_column IS NULL THEN 1
             ELSE 0
           END) AS numNull,
       SUM(CASE 
             WHEN t.your_column IS NOT NULL THEN 1
             ELSE 0
           END) AS numNotNull
  FROM YOUR_TABLE t

That will sum up the column NULL & not NULL for the entire table. It's likely you need a GROUP BY clause, depending on needs.

这将汇总整个表的列NULL & not NULL。根据需要,您可能需要一个GROUP BY子句。