简体   繁体   English

如何计算BOOLEAN列MySQL

[英]How to count BOOLEAN columns MySQL

I have a table with a column name "IS_QUICK", type TINYINT (0|1); 我有一个表的列名称为“ IS_QUICK”,键入TINYINT(0 | 1);

I need to count how much times he is true (1) and false (0) for a period: 我需要计算一段时间内他为真(1)和假(0)的次数:

In MySQL: 在MySQL中:

    SELECT DIA, MES, HORA, ANO, QUICK, NOT_QUICK FROM (
    SELECT * ,(
        SELECT COUNT(rq1.IS_QUICK) FROM qp1_relatorio_quickview rq1 where rq1.IS_QUICK = 1 AND rq1.created_at BETWEEN '2018-07-03 00:00:00' AND '2018-07-06 23:59:59'
    ) as QUICK,
    (
        SELECT COUNT(rq1.IS_QUICK) FROM qp1_relatorio_quickview rq1 where rq1.IS_QUICK = 0 AND rq1.created_at BETWEEN '2018-07-03 00:00:00' AND '2018-07-06 23:59:59'
    ) as NOT_QUICK
   , YEAR(rq.created_at) as ANO
   , MONTH(rq.created_at) as MES
   , DAY(rq.created_at) as DIA
   , HOUR(rq.created_at) as HORA
 FROM qp1_relatorio_quickview rq WHERE rq.created_at BETWEEN '2018-07-03 00:00:00' AND '2018-07-06 23:59:59'

) as relatorio
GROUP BY DIA

But he return count of all IS_QUICK between day 3 and 6, not for day 3 only (for example) 但是他返回第3天到第6天之间所有IS_QUICK的计数,而不仅返回第3天(例如)

Edit: table: 编辑:表:

    CREATE TABLE `qp1_relatorio_quickview` (
    `ID` INT(11) NOT NULL AUTO_INCREMENT,
    `IS_QUICK` TINYINT(1) NOT NULL DEFAULT '0',
    `PRODUTO_ID` BIGINT(20) NOT NULL DEFAULT '0',
    `PRODUTO_VARIACAO_ID` BIGINT(20) NOT NULL DEFAULT '0',
    `QUANTIDADE` INT(11) NOT NULL DEFAULT '0',
    `created_at` DATETIME NOT NULL,
    `updated_at` DATETIME NOT NULL,
    PRIMARY KEY (`ID`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=19
;

To count a boolean TINYINT field, you can simply use SUM(field) to count 1s, or SUM(NOT field) to count 0s. 要对布尔TINYINT字段进行计数,您可以简单地使用SUM(field)计数1s,或使用SUM(NOT field)计数0s。

SELECT
    SUM(IS_QUICK) AS QUICK,
    SUM(NOT IS_QUICK) AS NOT_QUICK
FROM qp1_relatorio_quickview rq
WHERE rq.created_at BETWEEN '2018-07-03 00:00:00' AND '2018-07-06 23:59:59';

The above query will select the total for the period. 上面的查询将选择该期间的总计。 If you want it summarised per day for example, make sure you select and group by all relevant fields: 例如,如果要每天汇总一次,请确保选择并按所有相关字段分组:

SELECT
    YEAR(rq.created_at) as ANO,
    MONTH(rq.created_at) as MES,
    DAY(rq.created_at) as DIA,
    SUM(IS_QUICK) AS QUICK,
    SUM(NOT IS_QUICK) AS NOT_QUICK
FROM qp1_relatorio_quickview rq
WHERE rq.created_at BETWEEN '2018-07-03 00:00:00' AND '2018-07-06 23:59:59'
GROUP BY ANO, MES, DIA;

Hi can you try to use sum 您好,您可以尝试使用sum吗

  SELECT
    sum(CASE WHEN rq1.IS_QUICK= 1 THEN 1 ELSE 0 END) as IS_QUICK,
    sum(CASE WHEN rq1.IS_QUICK= 0 THEN 1 ELSE 0 END) as NOT_QUICK,
   , YEAR(rq.created_at) as ANO
   , MONTH(rq.created_at) as MES
   , DAY(rq.created_at) as DIA
   , HOUR(rq.created_at) as HORA
 FROM qp1_relatorio_quickview rq WHERE rq.created_at BETWEEN '2018-07-03 00:00:00' AND '2018-07-06 23:59:59'

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

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