SQL - 计数列两次 [英] SQL - Counting a column twice

查看:160
本文介绍了SQL - 计数列两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表有一个名为'status'的列。这可以设置为0或1

I have a table that has a column called 'status'. This can be set to 0 or 1

我可以在单个查询中同时计算0和1吗?

Is it possible for me to count both the 0's and 1's in a single query?

提前感谢

James

推荐答案

,只是对 status 的值进行分组:

Yes, just group on the value of status:

SELECT status, COUNT(*)
FROM yourtable
GROUP BY status

两行,因为值只能是 0 1 COUNT(*) 列将是每个状态值出现在表中的次数。

That will give you exactly two rows since the value can only be 0 or 1, and the COUNT(*) column will be the number of times each status value appears in the table.

这篇关于SQL - 计数列两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆