简体   繁体   English

SQL:如何按日期计算布尔值?

[英]SQL: How to count boolean values by date?

I have a mysql table that looks like this:我有一个 mysql 表,看起来像这样:

Date           Status
2020-03-27     true
2020-03-27     true
2020-03-28     false
2020-03-28     true

How can I count the booleans and get a result like this:我如何计算布尔值并得到如下结果:

Date            Success        Failed
2020-03-27      2              0
2020-03-28      1              1

You can use:您可以使用:

select date, sum(status) as success, sum(not status) as failed
from t
group by date;

MySQL treats boolean "true" as "1" and boolean false as "0", so sum() works on them. MySQL 将布尔值“true”视为“1”,将布尔值 false 视为“0”,因此sum()对它们起作用。

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

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