简体   繁体   English

MySQL计数零总数

[英]MySQL counting total number of zeros

I have a table that looks like 我有一张桌子,看起来像

| id | day1 | day2 | day3 | day4 | day5 |
| 1  |  4   |  0   |  5   | 0    | 0    |
| 2  |  2   |  0   |  0   | 4    | 1    |

and I want to find to total number of zero entries for each id to give 我想找到每个ID的零条目总数

| id | total_zeros |
| 1  | 3           |
| 2  | 2           |
SELECT id, (day1=0)+(day2=0)+(day3=0)+(day4=0)+(day5=0) total_zeroes
FROM table

Try this one: 试试这个:

select 
id, if(day1=0,1,0)+if(day2=0,1,0)+ if(day3=0,1,0)+if(day4=0,1,0)+if(day5=0,1,0) as total
from test

DEMO HERE 此处演示

Why do people insist on making such un-usable tables? 人们为什么坚持要制作这种无法使用的桌子?

You will have to use a case statement, and evaluate each column individually, and then add up the results. 您将必须使用case语句,并分别评估每个列,然后将结果相加。

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

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