简体   繁体   中英

SQL: How to count boolean values by date?

I have a mysql table that looks like this:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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