简体   繁体   English

如何在 MySQL 查询中使用带有 CASE 条件的计数?

[英]How to use count with CASE condition in a MySQL query?

SELECT
COUNT(
 CASE WHEN ( DATE(igs.start_date) >= DATE('2020-05-01') 
           AND DATE(igs.start_date) <= DATE('2020-05-31') 
      THEN igs.id 
      ELSE 0
 END
) AS totalSessions
FROM
`ignite_session` igs

My requirement is I want session count of a particular month.我的要求是我想要特定月份的 session 计数。 From above query, I am getting the wrong count.从上面的查询中,我得到了错误的计数。 When I checked with the below query it is 0.当我检查以下查询时,它是 0。

 SELECT
      COUNT(id)
 FROM
      ignite_session igs
 WHERE
      DATE(igs.start_date) >= DATE('2020-05-01') 
      AND DATE(igs.start_date) <= DATE('2020-05-31')

The First Query is small part of complex query.第一个查询是复杂查询的一小部分。 I do not want to use date condition in where statement.我不想在 where 语句中使用日期条件。 Please help me with this CASE STATEMENT query.请帮我处理这个 CASE STATEMENT 查询。 totalSessions should return 0. totalSessions应该返回 0。

You should use a NULL in your else case.在其他情况下,您应该使用NULL

like:喜欢:

SELECT
COUNT(
    CASE WHEN (DATE(igs.start_date) >= DATE('2020-05-01') 
        AND DATE(igs.start_date) <= DATE('2020-05-31'))
    THEN igs.id 
    ELSE NULL
    END
) AS totalSessions
FROM
`ignite_session` igs

SQL Fiddle SQL 小提琴

MySQL 5.6 Schema Setup : MySQL 5.6 架构设置

CREATE TABLE t1
    (`c1` int)
;
    
INSERT INTO t1
    (`c1`)
VALUES
    (1),
    (2),
    (3),
    (4),
    (5)
;

Query 1 :查询 1

select count(case when c1 > 2 then 1 else null end) as nb_count from t1

Results :结果

| nb_count |
|----------|
|        3 |

Here's an EXAMPLE of what your current query is giving you without COUNT :这是您当前查询在没有COUNT的情况下为您提供的示例:

SELECT
        CASE WHEN DATE(igs.start_date) >= DATE('2020-05-01') 
        AND DATE(igs.start_date) <= DATE('2020-05- 
        31') THEN igs.id ELSE 0
        END
    AS totalSessions
    FROM
    `ignite_session` igs;

+---------------+
| totalSessions |
+---------------+
| 0             |
| 0             |
| 0             |
| 0             |
+---------------+
*4 row(s) returned.

So with COUNT , it founded rows being returned thats why you get something in your totalSessions even if you didn't any data for that date range.因此,使用COUNT ,它创建了返回的行,这就是为什么即使您在该日期范围内没有任何数据,您也会在totalSessions中得到一些东西。 But with a minor change to your query, you can get the correct result.但只要对查询稍作改动,就可以获得正确的结果。 Instead of COUNT , use SUM .而不是COUNT ,使用SUM Like this example:像这个例子:

SELECT
    SUM(CASE WHEN DATE(igs.start_date) >= DATE('2020-05-01') AND DATE(igs.start_date) <= DATE('2020-05- 
    31') THEN 1 -- change to 1 instead of igs.id
        ELSE 0
    END)
AS totalSessions
FROM
`ignite_session` igs;

Or a much shorter version (similar to Akina's suggestion in the comment):或者更短的版本(类似于 Akina 在评论中的建议):

SELECT SUM(DATE(igs.start_date) >= '2020-06-01' AND DATE(igs.start_date) <= '2020-05-31')
 FROM ignite_session igs;

Here's a demo .这是一个演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM