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".
The operating environment of this tutorial: windows10 system, mysql8.0.22 version, Dell G3 computer.
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:
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
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!