Home >Database >Mysql Tutorial >How to query the number of occurrences of data in mysql

How to query the number of occurrences of data in mysql

WBOY
WBOYOriginal
2022-05-16 15:09:178367browse

In mysql, you can use the select statement with group by and count to query the number of data occurrences. count can return the number of retrieved data. The syntax is "select column name, count(*) as count from table name group by column name".

How to query the number of occurrences of data in mysql

The operating environment of this tutorial: windows10 system, mysql8.0.22 version, Dell G3 computer.

How to query the number of occurrences of data in mysql

The count function is a function used to count records in a table or array. count(*) It returns the number of retrieved rows, regardless of whether it contains NULL value.

When SELECT is retrieved from a table without retrieving other columns, and there is no WHERE clause, COUNT(*) is optimized for the fastest return speed.

Examples are as follows:

Table data:

How to query the number of occurrences of data in mysql

Query the number of occurrences classified by cat_type

Query For the number of occurrences classified by cat_type, you can use the following statement:

select cat_type,count(*) as count from cat group by cat_type;

To query the number of occurrences classified by cat_type, you can use the following statement:

select cat_type,count(*) as count from cat group by cat_type having count>1;

The query results are as follows

How to query the number of occurrences of data in mysql

Recommended learning: mysql video tutorial

The above is the detailed content of How to query the number of occurrences of data in mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn