为了账号安全,请及时绑定邮箱和手机立即绑定

MySQL COUNT函数

标签:
MySQL

Summary: in this tutorial, you will learn how to use the MySQL COUNT function to count the number rows in a table.

Introduction to the MySQL COUNT function

The COUNT function returns the number of rows in a table. The COUNT function allows you to count all rows in a table or rows that match a particular condition.

The syntax of the COUNT function is as follows.

COUNT(expression)

The return type of the COUNT function is BIGINT. The COUNT function returns zero if there was no matching row found.

There are several forms of the COUNT function: COUNT(*)COUNT(expression) and COUNT(DISTINCT expression).

MySQL COUNT(*)

The COUNT(*) function returns the number of rows in a result set returned by a SELECT statement. The COUNT(*) function counts rows that contain no-NULL and NULL values.

If you use the COUNT(*) function to count the number rows in a table without using the WHERE clause and selecting other columns, it will perform very fast.

This optimization is applied to MyISAM tables only because the number of rows of a MyISAM table is stored in the table_rows column in the tables table of the information_schema database; therefore MySQL can retrieve it very quickly.

MySQL COUNT(expression)

The COUNT(expression) returns the number of rows that do not contain NULL values.

MySQL COUNT(DISTINCT expression)

The COUNT(DISTINCT expression) returns the number of unique rows that dot not contain NULL values.

MySQL COUNT examples

Let’s create a new table named demos and insert some sample data for the demonstration.

-- create a demos table CREATE TABLE IF NOT EXISTS demos(  id int auto_increment primary key,  val int ); -- insert some sample data INSERT INTO demos(val) VALUES(1),(1),(2),(2),(NULL),(3),(4),(NULL),(5); -- select data from demos table SELECT * FROM demos;

demos table

To count all rows in the demos table, you use the COUNT(*) function as follows:

SELECT COUNT(*)  FROM demos;

MySQL COUNT all rows

You can add a WHERE clause to specify a condition to count e.g., to count only rows whose val column contains number 2, you use the following  query:

SELECT COUNT(*)  FROM demos WHERE val = 2;

MySQL COUNT with WHERE

If you specify the val column in the COUNT function, the COUNT function counts all rows whose val column contains non-NULL values only. See the following query:

SELECT COUNT(*)  FROM demos WHERE val = 2;

Two NULL values in the val column are ignored.

To count unique rows in the demos table, you add the DISTINCT operator to the COUNT function as the following query:

SELECT COUNT(DISTINCT val) FROM demos;

MySQL COUNT DISTINCT

Two duplicate values 1,2 and two NULL values are ignored in the counting.

MySQL COUNT with GROUP BY

We often use the COUNT function in conjunction with GROUP BY clause to characterize the data in various groups. See the following products table.

products table

For example, to find how many products in each product line, you use the COUNT function with the GROUP BY clause as follows:

SELECT productline, count(*) FROM products GROUP BY productline;

MySQL COUNT products in product line

To find the number of products supplied by vendors, you use the following query:

SELECT productvendor, count(*) FROM products GROUP BY productvendor;

MySQL COUNT products by vendor

To find which vendor supplies at least 9 products, you use the COUNT function in the HAVING clause as the following query:

SELECT productvendor, count(*) FROM products GROUP BY productvendor HAVING count(*) >= 9

MySQL COUNT with HAVING

MySQL COUNT IF

You can use a control flow function e.g., IFIFNULL, CASE, etc., in the COUNT function to count rows whose values match a condition.

For example, the following query finds how many orders are cancelled, on hold and disputed:

SELECT COUNT(IF(status='Cancelled',1, NULL)) 'Cancelled',        COUNT(IF(status='On Hold',1, NULL)) 'On Hold',        COUNT(IF(status='Disputed',1, NULL)) 'Disputed' FROM orders;

The IF function returns 1  if the order’s status  is cancelled, on hold or disputed, otherwise it returns NULL. The COUNTfunction only counts 1, not NULL values therefore the query returns the number of orders based on the corresponding status.

mysql count expression

In this tutorial, you have learned various techniques to count the number of rows in a table using the MySQL COUNTfunction.

原文链接:http://outofmemory.cn/mysql/function/mysql-count

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消