简体   繁体   English

计算mysql中的列项

[英]count column items in mysql

I have a column called "WasCancelled" in a MySQL DB, it's of a Boolean type. 我在MySQL DB中有一个名为“WasCancelled”的列,它是一个布尔类型。

I save the number 0,1 and 2 to it. 我将数字0,1和2保存到它。

I need a query that will count how many one's, two's and zeros are there. 我需要一个查询来计算有多少个,两个和零。

for example: my column: 例如:我的专栏:

WasCanceled
-----------
    0
    0
    1
    1
    1
    2
    0
    0   
    0
    2
    0
    0
    1

I need the query to output: 我需要查询输出:

 number | times
 -------|------
  "0"   |   7
  "1"   |   4
  "2"   |   2

please help. 请帮忙。

thank you. 谢谢。

Dave. 戴夫。

You can do this by grouping: 您可以通过分组来完成此操作:

SELECT WasCancelled, COUNT(*)
FROM <Table>
GROUP BY WasCancelled

If you need more information, look here for details on Group By and Count . 如果您需要更多信息,请查看此处了解分组依据和计数的详细信息。

Update 更新

To include the question in the comments: to restrict on special values, you can add a WHERE clause: 要在注释中包含问题:要限制特殊值,可以添加WHERE子句:

SELECT WasCancelled, COUNT(*)
FROM <Table>
WHERE WasCancelled = "1"
GROUP BY WasCancelled

In further questions, please edit your overall question to include sub-questions or open new topics. 在进一步的问题中,请编辑您的整体问题以包含子问题或打开新主题。 Please read How To Ask Good Questions . 请阅读如何提出好问题

Update 2 更新2

SQL also allows the HAVING clause, which is like WHERE but allows the comparison of aggregated values. SQL还允许使用HAVING子句,它类似于WHERE但允许比较聚合值。 See here for details (eg you want to know which value appears more than 5 times, etc.). 请参阅此处了解详细信息(例如,您想知道哪个值出现的次数超过5次,等等)。

Use GROUP BY with COUNT fuction: 使用带有COUNT功能的GROUP BY

Try this: 尝试这个:

SELECT WasCanceled, COUNT(1) as times 
FROM tableA 
GROUP BY WasCanceled;

I think this might work! 我想这可能会奏效!

select
  count(WasCanceled) as CNT, WasCancelled
from
  YourTableNameHere
group by
  WasCanceled

这适合我

SELECT WasCanceled AS number, COUNT(WasCanceled) AS times FROM tblTest GROUP BY WasCanceled

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

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